前言
市面上分库分表的工具有很多,大致分为两种:
- 一种是在代码层面实现的分库分表,典型的像Sharding-JDBC;
- 另一种是基于代理实现的,比如:MyCat、Sharding-proxy;
代理实现的分库分表由于增加了一个代理层,所以会带来网络传输的开销;而代码层面的分库分表相对来说性能会好一点;这里咱们来实践一下Sharding-JDBC,集成到Spring Boot项目中;
创建数据库
我们创建两个数据库分别是mall_tb1、mall_tb2,两个库中创建相同的表,这里需要注意分库的两个表数据结构和类型要完全一致,否则后面启动时会报错;
#订单表
CREATE TABLE t_order (
`id` BIGINT ( 36 ) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
`order_num` VARCHAR ( 36 ) NOT NULL UNIQUE COMMENT '订单号',
`triple_num` VARCHAR ( 36 ) NOT NULL UNIQUE COMMENT '三方单号',
`state` TINYINT ( 1 ) NOT NULL COMMENT '订单状态:0:待支付;1:已支付;2:已取消;3:部分退回;4:全部退回',
`order_time` DATETIME NOT NULL COMMENT '下单时间',
`total_price` DECIMAL ( 11, 2 ) NOT NULL COMMENT '总金额',
`address` VARCHAR ( 255 ) NOT NULL COMMENT '地址',
`phone` VARCHAR ( 30 ) NOT NULL COMMENT '电话',
`user_num` VARCHAR ( 30 ) NOT NULL COMMENT '用户编码',
`payment_time` DATETIME COMMENT '支付时间',
`payment` TINYINT ( 2 ) COMMENT '支付方式',
`version` INT ( 10 ) NOT NULL COMMENT '版本号',
`freight` DECIMAL ( 11, 2 ) NOT NULL COMMENT '运费',
`dr` TINYINT ( 1 ) NOT NULL DEFAULT 0 COMMENT '删除标记:0:未删除;1:已删除',
PRIMARY KEY ( id )
);
#订单明细表
CREATE TABLE t_order_item (
`id` BIGINT ( 36 ) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
`order_num` VARCHAR ( 36 ) NOT NULL UNIQUE COMMENT '订单号',
`order_item_num` VARCHAR ( 36 ) NOT NULL UNIQUE COMMENT '订单明细单号',
`state` TINYINT ( 1 ) NOT NULL COMMENT '订单状态:0:待支付;1:已支付;2:已取消;3:部分退回;4:全部退回',
`sku_code` bigint ( 36 ) NOT NULL COMMENT '商品sku编码',
`name` varchar(64) comment '商品名称',
`user_num` VARCHAR ( 30 ) NOT NULL COMMENT '用户编码',
`amount` INT ( 10 ) UNSIGNED NOT NULL COMMENT '数量',
`unit_price` DECIMAL ( 11, 2 ) NOT NULL COMMENT '单价',
`item_total_price` DECIMAL ( 11, 2 ) NOT NULL COMMENT '明细总价',
`seller_code` VARCHAR ( 36 ) NOT NULL COMMENT '卖家编码',
`remark` VARCHAR ( 255 ) COMMENT '备注',
PRIMARY KEY ( id )
);
在数据库中如下图:
添加依赖
然后,我们引入相关的依赖包,这里我们使用spring boot的版本是2.5.3版本,Sharding-JDBC我们使用boot-starter的版本,具体如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>order</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>order</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- sharding-sphere -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置文件
因为我们使用的是Sharding-JDBC是boot-starter的版,无需手动配置,我们只需要添加配置文件的配置项即可,具体配置如下:
#数据库
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mall_tb1
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis
mybatis.mapper-locations=classpath:mapping/*Mapper.xml
server.port=8080
#是否允许覆盖定义,默认是false,这里我们设置成true
spring.main.allow-bean-definition-overriding=true
#这里数据库的配置我们要注意type项,我们使用的是Druid链接池,如果type不设置会报空指针
#ds0
sharding.jdbc.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds0.url=jdbc:mysql://localhost:3306/mall_tb1
sharding.jdbc.datasource.ds0.username=root
sharding.jdbc.datasource.ds0.password=root
#ds1
sharding.jdbc.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds1.url=jdbc:mysql://localhost:3306/mall_tb2
sharding.jdbc.datasource.ds1.username=root
sharding.jdbc.datasource.ds1.password=root
#配置
sharding.jdbc.datasource.names=ds0,ds1
#默认配置
sharding.jdbc.config.sharding.default-data-source-name=ds0
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_num
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=ds${user_num}%2
#分库分表的设置,可以设置多张表
sharding.jdbc.config.sharding.tables.t_order.key-generator-column-name=id
sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds${0..1}.t_order
sharding.jdbc.config.sharding.tables.t_order.database-strategy.inline.sharding-column=user_num
sharding.jdbc.config.sharding.tables.t_order.database-strategy.inline.algorithm-expression=ds${user_num%2}
#这两行是分表的设置,如果需要可以放开注释
#sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.sharding-column=order_num
#sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order${order_num%2}
sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=id
sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds${0..1}.t_order_item
sharding.jdbc.config.sharding.tables.t_order_item.database-strategy.inline.sharding-column=user_num
sharding.jdbc.config.sharding.tables.t_order_item.database-strategy.inline.algorithm-expression=ds${user_num%2}
#sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_num
#sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order${order_num%2}
代码
完成上面的配置后,就已经可以使用sharding-jdbc的分库分表了,接下来我们写个测试代码试验一下;
@SpringBootTest
class OrderApplicationTests {
@Autowired
private OrderMapper orderMapper;
@Test
public void insertOrder(){
OrderPO orderPO = new OrderPO();
orderPO.setOrderNum("123");
orderPO.setTripleNum("123");
orderPO.setState(0);
orderPO.setOrderTime(new Date());
orderPO.setTotalPrice(new BigDecimal(100));
orderPO.setAddress("北京市丰台区");
orderPO.setPhone("13588886666");
orderPO.setUserNum(2);
orderPO.setPaymentTime(new Date());
orderPO.setPayment(1);
orderPO.setVersion(0);
orderPO.setFreight(new BigDecimal(10));
orderMapper.insertOrder(orderPO);
}
}
由于是使用的user_num作为分片键,我们修改userNum的值,可以看到数据被路由到了不同的库里面。
总结
整体来说Sharding-JDBC的使用方式还是比较方便的,最后我会把官方的文档贴在末尾;分库分表的技术方案已经发展的很成熟,有很多类似Shrding-JDBC的开源方案可以使用,所以分库分表更应该关注的是使用场景,在什么情况下才适合分库分表,还有使用分库分表所带来的问题,比如分布式事物等等;
https://shardingsphere.apache.org/document/current/cn/overview/