一、使用技术描述
spring-boot
1
2 org.springframework.boot
3 spring-boot-starter-parent
4 2.0.6.RELEASE
5
1
2 1.8
3 4.0.0-RC3
4 2.1.3
5 1.2.1
6
mybatis
1
2 org.mybatis.spring.boot
3 mybatis-spring-boot-starter
4 ${mybatis.version}
5
druid
1
2 com.alibaba
3 druid
4 ${druid.version}
5
sharding-jdbc
1
2 org.apache.shardingsphere
3 sharding-jdbc-spring-boot-starter
4 ${shard.jdbc.version}
5
二、数据库表建设
使用sharding-jdbc来分库分表,我们首先创建两个数据库shard_jdbc_1和shard_jdbc_2,两个库中的表结构完全相同,接下来我们在每个数据库中都创建如下表:
order表:每个库中有3张order表,分别是order_1、order_2、order_3,我们以order_1为例
1 CREATE TABLE `order_1` (2 `id` varchar(60) NOT NULL,3 `product_id` bigint(20) NOT NULL,4 `user_id` bigint(20) NOT NULL,5 `num` int(2) NOT NULL,6 `create_time` datetime NOT NULL,7 PRIMARY KEY (`id`)8 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
product表:每个库中有5张product表,分别是product_1、product_2、product_3、product_4、product_5,我们以product_1为例
1 CREATE TABLE `product_1` (2 `id` bigint(20) NOT NULL,3 `name` varchar(60) NOT NULL,4 `version` varchar(60) NOT NULL,5 `create_time` datetime NOT NULL,6 `price` decimal(10,2) NOT NULL,7 `deleted` enum('0','1') NOT NULL,8 PRIMARY KEY (`id`),9 UNIQUE KEY `idx_version` (`version`)10 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
user表:每个库中只有1张user表
1 CREATE TABLE `user` (2 `id` bigint(20) NOT NULL AUTO_INCREMENT,3 `name` varchar(60) NOT NULL,4 `age` int(3) NOT NULL,5 `sex` enum('1','2') NOT NULL,6 `create_time` datetime NOT NULL,7 PRIMARY KEY (`id`)8 ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4
三、properties配置文件
1 # 数据源名称,多数据源以逗号分隔2 spring.shardingsphere.datasource.names=shard1, shard23
4 # shard1数据源配置5 spring.shardingsphere.datasource.shard1.type=com.alibaba.druid.pool.DruidDataSource6 spring.shardingsphere.datasource.shard1.driver-class-name=com.mysql.jdbc.Driver7 spring.shardingsphere.datasource.shard1.url=jdbc:mysql://127.0.0.1:3306/shard_jdbc_1?characterEncoding=utf-8
8 spring.shardingsphere.datasource.shard1.username=root9 spring.shardingsphere.datasource.shard1.password=root10
11 # shard2数据源配置12 spring.shardingsphere.datasource.shard2.type=com.alibaba.druid.pool.DruidDataSource13 spring.shardingsphere.datasource.shard2.driver-class-name=com.mysql.jdbc.Driver14 spring.shardingsphere.datasource.shard2.url=jdbc:mysql://127.0.0.1:3306/shard_jdbc_2?characterEncoding=utf-8
15 spring.shardingsphere.datasource.shard2.username=root16 spring.shardingsphere.datasource.shard2.password=root17
18 # ================== order表配置-start ==================
19 # order表节点配置20 spring.shardingsphere.sharding.tables.order.actual-data-nodes=shard$->{1..2}.order_$->{1..3}21 spring.shardingsphere.sharding.tables.order.database-strategy.inline.sharding-column=product_id22 # 精确分片算法类名称,用于=和IN。该类需实现PreciseShardingAlgorithm接口并提供无参数的构造器23 spring.shardingsphere.sharding.tables.order.database-strategy.inline.algorithm-expression=shard$->{product_id % 2 + 1}24 # 范围分片算法类名称,用于BETWEEN,可选。该类需实现RangeShardingAlgorithm接口并提供无参数的构造器25 # spring.shardingsphere.sharding.tables.t_order.database-strategy.standard.range-algorithm-class-name=
26 spring.shardingsphere.sharding.tables.order.table-strategy.inline.sharding-column=user_id27 spring.shardingsphere.sharding.tables.order.table-strategy.inline.algorithm-expression=order_$->{user_id % 3 + 1}28 # 逻辑表名称29 spring.shardingsphere.sharding.tables.order.logic-table=order30 # 主键生成器,key-generator.column=id表示t_order_$->{1..3}表主键名称;key-generator.type=UUID表示主键生成策略为UUID,也可以使用SNOWFLAKE31 spring.shardingsphere.sharding.tables.order.key-generator.column=id32 # order表中的id字段时varchar类型,所以我们可以使用UUID33 spring.shardingsphere.sharding.tables.order.key-generator.type=UUID34 # ================== order表配置-end ==================
35
36 ================== product表配置-start ==================
37 # 节点信息38 spring.shardingsphere.sharding.tables.product.actual-data-nodes=shard$->{1..2}.product_$->{1..5}39 # 分库策略40 spring.shardingsphere.sharding.tables.product.database-strategy.standard.sharding-column=name41 spring.shardingsphere.sharding.tables.product.database-strategy.standard.precise-algorithm-class-name=com.five.monkey.shard.algorithm.ProductDataBaseAlgorithm42 # 分表策略43 spring.shardingsphere.sharding.tables.product.table-strategy.standard.sharding-column=version44 spring.shardingsphere.sharding.tables.product.table-strategy.standard.precise-algorithm-class-name=com.five.monkey.shard.algorithm.ProductTableAlgorithm45 # 基础表名46 spring.shardingsphere.sharding.tables.product.logic-table=product47 # 主键生成策略48 spring.shardingsphere.sharding.tables.product.key-generator.column=id49 spring.shardingsphere.sharding.tables.product.key-generator.type=SNOWFLAKE50 ================== product表配置-end ==================
51
52 # 未配置分片规则的表将通过默认数据源定位53 spring.shardingsphere.sharding.default-data-source-name=shard154 # 是否开启SQL显示,默认值: false
55 spring.shardingsphere.props.sql.show=true
56
57 mybatis.mapper-locations=classpath:/mapper/**/*.xml
1、配置文件order表说明
21行表示order表分库是根据product_id字段分片的
23行表示order表的数据分库算法是根据product_id字段对2取余加1来计算数据源后缀的
26行表示order表分表是根据user_id字段分片的
27行表示order表的数据分表算法是根据user_id字段对3取余加1来计算order表后缀的
31行表示order表的主键字段是id字段
33行表示order表主键id字段值的生成策略是使用UUID
2、配置文件product表说明
40行分库字段是name字段
41行分库算法是自定义的com.five.monkey.shard.algorithm包下的ProductDataBaseAlgorithm类,该类实现了PreciseShardingAlgorithm接口
43行表示分表键是version字段
44行分表算法是自定义的com.five.monkey.shard.algorithm包下的ProductTableAlgorithm类,该类实现了PreciseShardingAlgorithm接口
48行表示product表的主键字段是id字段
49行表示product表主键id字段值的生成策略是使用雪花id
3、配置文件user表说明
因为user表没有配置分片规则,所以会根据53行的配置使用shard1数据源中的user表
4、product表分库分表策略类
分库策略
1 public class ProductDataBaseAlgorithm implements PreciseShardingAlgorithm{2 @Override3 public String doSharding(Collection availableTargetNames, PreciseShardingValueshardingValue) {4 String name =shardingValue.getValue();5 int index = Math.abs(name.hashCode()) % 2 + 1;6 return "shard" +index;7 }8 }
分表策略
1 public class ProductTableAlgorithm implements PreciseShardingAlgorithm{2 @Override3 public String doSharding(Collection availableTargetNames, PreciseShardingValueshardingValue) {4 String version =shardingValue.getValue();5 int index = Math.abs(version.hashCode()) % 5 + 1;6 return "product_" +index;7 }8 }