【ShardingSphere】shardingjdbc入门案例-springboot整合shardingjdbc

news/2024/5/18 22:59:31 标签: spring boot, sharding, jdbc, 分库分表, 分片

该教程仅仅适用于4.x版本,在ShardingSphere的迭代历史中很多的配置和兼容问题很大,这里入手一定要注意版本。


构建一个SpringBoot项目

SpringBoot项目的构建这里不再赘述,这里要提及的一点就是我们构建的时候,基本不需要引入依赖,后面会一步一步加入

数据库准备

  • 构建两个库,库名安装ds0,ds1来定义
  • 数据库内建立t_order1,t_order2两个表,表结构一致,只是名字用数字排序

对应SQL如下:

DROP TABLE IF EXISTS `t_order1`;
CREATE TABLE `t_order1` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `order_id` int(11) DEFAULT NULL,
  `cloumn` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

DROP TABLE IF EXISTS `t_order2`;
CREATE TABLE `t_order2` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `order_id` int(11) DEFAULT NULL,
  `cloumn` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

完成基本的数据库和工程搭建之后,我们就可以来完成我们的整合sharding jdbc

shardingsphereHikariCP_34">引入shardingsphere和HikariCP连接池

这里引入的sharding sphere是4.1.1,版本问题一定要注意,不然后面可能没有办法成功。除了引入sharding sphere这里还引入了web,方便编写接口来调用。
具体POM文件如下:

<properties>
    <java.version>1.8</java.version>
    <sharding-sphere.version>4.1.1</sharding-sphere.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>4.0.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.25</version>
    </dependency>

    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>${sharding-sphere.version}</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>

</dependencies>

配置表和库的策略

这里主要的功能是完成数据库的分片分表,而且由于是示例,这里也仅仅只写一个表
具体的application.properties配置文件内容如下:

server.port=10080

spring.shardingsphere.datasource.names=ds0,ds1

# 配置第一个数据库
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/ds0
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=

# 配置第二个数据库
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/ds1
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=

# 配置分库策略
spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}

# 配置分表策略
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}

编写测试接口

到这里基本的操作都已经完成了, 我们接下来就能使用sharding jdbc来完成开发了。所以接下来我们写点代码看看能不能获取到对应的数据库了解吧

package com.echo.shardingjdbc;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author tang.sl
 */
@RestController
@RequestMapping(value = "/test")
public class Test {

    @Resource
    private DataSource dataSource;

    @GetMapping(value = "/test")
    public String test() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        return "success";
    }

}

测试

启动我们的项目,然后访问我们写得接口,我们可以在控制台看到如下内容
在这里插入图片描述

总结

  • 整合sharding sphere其实并不难,难的地方在于对于各版本不同的了解
  • 碰到报错,先看看版本问题

http://www.niftyadmin.cn/n/1638652.html

相关文章

HttpClient和jsoup基础API使用

准备配置 pom <dependencies><!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>…

属性参数 - 位置参数和命名参数

如前一节所述&#xff0c;RegKeyAttribute属性构造器为&#xff1a;public RegKeyAttribute(RegHives Hive, String ValueName)根据这个属性构造器原型&#xff0c;我们把这个属性附着给一个字段&#xff1a;[RegKey(RegHives.HKEY_CURRENT_USER, "Foo")]public int …

传统LPC声码器的语音模型

1、先了解LPC和声码器这两个概念 &#xff08;1&#xff09;LPC LPC&#xff08;linear predictive coding&#xff0c;LPC&#xff09;&#xff0c;也就是线性预测编码。原理在"线性预测编码"这篇文章有讲。 &#xff08;2&#xff09;声码器 在发送端对语音信号…

SpringDataJpa idea自动生成实体类

第一步 第二步 第三步 第四步 第五步 第六步 第七步 第九步 第十步 package com.hikktn.entity;import javax.persistence.*; import java.util.Objects;/*** ClassName PersonEntity* Description TODO* Author lisonglin* Date 2021/5/3 20:15* Version 1.0*/ Entity Ta…

【ShardingSphere】springboot整合shardingjdbc+mybatis进行增删改查

上文我们已经完成了springboot整合shardingjdbc并且拿到了有效的DataSource&#xff0c;那证明我们已经可以通过shardingjdbc的连接来操作数据库。本文我们将使用springboot整合shardingjdbcmybatis&#xff0c;真正的实现分库分表的操作。大佬请略过 整合mybatis 添加mybatis…

周末没来

家里没有装宽带&#xff0c;所以周末没有上来&#xff0c;今天周一&#xff0c;有点疲倦。转载于:https://www.cnblogs.com/liang_jumps/archive/2004/10/18/53851.html

摄像机的工作原理

摄像机是一种把景物光像转变为电信号的装置。其结构大致可分为三部分&#xff1a;光学系统&#xff08;主要指镜头&#xff09;、光电转换系统&#xff08;主要指摄像管或固体摄像器件&#xff09;以及电路系统&#xff08;主要指视频处理电路&#xff09;。光学系统的主要部件…

有趣

今天发现一个很有趣的事情&#xff1a;<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />两个座位相邻的同事&#xff0c;居然一个英文名叫Gump&#xff0c;另一个叫Jenny&#xff0c;我汗死了...转载于:https://www.cnblogs.com/…