分库分表之sharding-jdbc
一、使用技术描述
- spring-boot
1 <parent> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-parent</artifactId> 4 <version>2.0.6.RELEASE</version> 5 </parent>
1 <properties> 2 <java.version>1.8</java.version> 3 <shard.jdbc.version>4.0.0-RC3</shard.jdbc.version> 4 <mybatis.version>2.1.3</mybatis.version> 5 <druid.version>1.2.1</druid.version> 6 </properties>
- mybatis
1 <dependency> 2 <groupId>org.mybatis.spring.boot</groupId> 3 <artifactId>mybatis-spring-boot-starter</artifactId> 4 <version>${mybatis.version}</version> 5 </dependency>
- druid
1 <dependency> 2 <groupId>com.alibaba</groupId> 3 <artifactId>druid</artifactId> 4 <version>${druid.version}</version> 5 </dependency>
- sharding-jdbc
1 <dependency> 2 <groupId>org.apache.shardingsphere</groupId> 3 <artifactId>sharding-jdbc-spring-boot-starter</artifactId> 4 <version>${shard.jdbc.version}</version> 5 </dependency>
二、数据库表建设
使用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, shard2 3 4 # shard1数据源配置 5 spring.shardingsphere.datasource.shard1.type=com.alibaba.druid.pool.DruidDataSource 6 spring.shardingsphere.datasource.shard1.driver-class-name=com.mysql.jdbc.Driver 7 spring.shardingsphere.datasource.shard1.url=jdbc:mysql://127.0.0.1:3306/shard_jdbc_1?characterEncoding=utf-8 8 spring.shardingsphere.datasource.shard1.username=root 9 spring.shardingsphere.datasource.shard1.password=root 10 11 # shard2数据源配置 12 spring.shardingsphere.datasource.shard2.type=com.alibaba.druid.pool.DruidDataSource 13 spring.shardingsphere.datasource.shard2.driver-class-name=com.mysql.jdbc.Driver 14 spring.shardingsphere.datasource.shard2.url=jdbc:mysql://127.0.0.1:3306/shard_jdbc_2?characterEncoding=utf-8 15 spring.shardingsphere.datasource.shard2.username=root 16 spring.shardingsphere.datasource.shard2.password=root 17 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_id 22 # 精确分片算法类名称,用于=和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_id 27 spring.shardingsphere.sharding.tables.order.table-strategy.inline.algorithm-expression=order_$->{user_id % 3 + 1} 28 # 逻辑表名称 29 spring.shardingsphere.sharding.tables.order.logic-table=order 30 # 主键生成器,key-generator.column=id表示t_order_$->{1..3}表主键名称;key-generator.type=UUID表示主键生成策略为UUID,也可以使用SNOWFLAKE 31 spring.shardingsphere.sharding.tables.order.key-generator.column=id 32 # order表中的id字段时varchar类型,所以我们可以使用UUID 33 spring.shardingsphere.sharding.tables.order.key-generator.type=UUID 34 # ================== 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=name 41 spring.shardingsphere.sharding.tables.product.database-strategy.standard.precise-algorithm-class-name=com.five.monkey.shard.algorithm.ProductDataBaseAlgorithm 42 # 分表策略 43 spring.shardingsphere.sharding.tables.product.table-strategy.standard.sharding-column=version 44 spring.shardingsphere.sharding.tables.product.table-strategy.standard.precise-algorithm-class-name=com.five.monkey.shard.algorithm.ProductTableAlgorithm 45 # 基础表名 46 spring.shardingsphere.sharding.tables.product.logic-table=product 47 # 主键生成策略 48 spring.shardingsphere.sharding.tables.product.key-generator.column=id 49 spring.shardingsphere.sharding.tables.product.key-generator.type=SNOWFLAKE 50 ================== product表配置-end ================== 51 52 # 未配置分片规则的表将通过默认数据源定位 53 spring.shardingsphere.sharding.default-data-source-name=shard1 54 # 是否开启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<String> { 2 @Override 3 public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) { 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<String> { 2 @Override 3 public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) { 4 String version = shardingValue.getValue(); 5 int index = Math.abs(version.hashCode()) % 5 + 1; 6 return "product_" + index; 7 } 8 }
github:https://github.com/pingyuangulang/spring-boot-demo/tree/master/spring-boot-shard-jdbc

浙公网安备 33010602011771号