文章目录 一、shardingjdbc3.x 二、shardingjdbc4.x 2.1. 依赖 2.2. yml 2.3. 自定义算法
一、shardingjdbc3.x
1. 依赖
< ! --分表库分表中间件--> < dependency> < groupId> io.shardingsphere< /groupId> < artifactId> sharding-jdbc-spring-boot-starter< /artifactId> < version> 3.1 .0 < /version> < /dependency> < dependency> < groupId> io.shardingsphere< /groupId> < artifactId> sharding-jdbc-spring-namespace< /artifactId> < version> 3.1 .0 < /version> < /dependency>
2. yml
spring:main:allow-bean-definition-overriding: true application:name: mayikt-admin
sharding:jdbc:datasource:names: sys-adminsys-admin:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/sys-admin?useUnicode= true& characterEncoding = UTF-8& serverTimezone = GMT%2B8username: rootpassword: 123456 config:sharding:tables:sys_user:actual-data-nodes: sys-admin.sys_user_$-> { 1 .. 2 } table-strategy:standard:precise-algorithm-class-name: com.mayikt.main.api.impl.config.MayiktPreciseShardingAlgorithmsharding-column: id
3. 自定义的算法
package com.mayikt.main.api.impl.config; import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import lombok.extern.slf4j.Slf4j; import java.util.Collection; @Slf4j
public class MayiktPreciseShardingAlgorithm implements PreciseShardingAlgorithm< Integer> { /*** 真实表容量建议是为500万,测试暂未为一张表存储5条数据*/private Long tableSize = 5l; // 逻辑表名称public static final String TABLE_NAME = "sys_user_" ; /*** 插入数据 改写表的名称* 查询 改写表的名称** @param collection* @param preciseShardingValue* @return*/@Overridepublic String doSharding( Collection< String> collection, PreciseShardingValue< Integer> preciseShardingValue) { Double temp = Double.valueOf( preciseShardingValue.getValue( )) / tableSize; String tableName = TABLE_NAME + ( int) Math.ceil( temp) ; // 分表分库 userid == == mysql自带的自增 序列 雪花算法log.info( "<tableName->{}>" , tableName) ; return tableName; }
}
二、shardingjdbc4.x
2.1. 依赖
< ! --分表库分表中间件--> < dependency> < groupId> org.apache.shardingsphere< /groupId> < artifactId> sharding-jdbc-spring-boot-starter< /artifactId> < version> 4.1 .1 < /version> < /dependency>
2.2. yml
spring:main:allow-bean-definition-overriding: true application:name: mayikt-admin
shardingsphere:datasource:names: sys-adminsys-admin:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://127.0.0.1:3306/sys-admin?useUnicode= true& characterEncoding = UTF-8& serverTimezone = GMT%2B8username: rootpassword: 123456 sharding:tables:sys_user:actual-data-nodes: sys-admin.sys_user_$-> { 1 .. 2 } table-strategy:standard:precise-algorithm-class-name: com.mayikt.main.api.impl.config.MayiktPreciseShardingAlgorithmsharding-column: id
2.3. 自定义算法
package com.mayikt.main.api.impl.config; import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue; import java.util.Collection; @Slf4j
public class MayiktPreciseShardingAlgorithm implements PreciseShardingAlgorithm< Integer> { /*** 真实表容量建议是为500万,测试暂未为一张表存储5条数据*/private Long tableSize = 5l; // 逻辑表名称public static final String TABLE_NAME = "sys_user_" ; /*** 插入数据 改写表的名称* 查询 改写表的名称** @param collection* @param preciseShardingValue* @return*/@Overridepublic String doSharding( Collection< String> collection, PreciseShardingValue< Integer> preciseShardingValue) { Double temp = Double.valueOf( preciseShardingValue.getValue( )) / tableSize; String tableName = TABLE_NAME + ( int) Math.ceil( temp) ; // 分表分库 userid == == mysql自带的自增 序列 雪花算法log.info( "<tableName->{}>" , tableName) ; return tableName; }
}