系列文章目录
sharding-jdbc5系列教程(一)springboot配置shardingjdbc+mybatis-plus+druid+dynamic-datasource
前言,本系列教程都是基于shardingjdbc5.0+版本以上的
本片讲解如何自定义分片算法
一、实现自己分片算法类
看文档可以知道
分片策略有三种类型,支持 STANDARD、COMPLEX 或 HINT(不区分大小写)
这里我们选择STANDARD类型的,也就是标准分片类型
看源码现有框架中的分片算法都实现了StandardShardingAlgorithm接口,
所以我们自己定义的类也实现这个接口,代码如下。我这里参照了框架自带的InlineShardingAlgorithm分片算法类
public class MyStandardShardingAlgorithm implements StandardShardingAlgorithm {
// private static final String ALGORITHM_EXPRESSION_KEY = "algorithm-expression";
// private static final String ALLOW_RANGE_QUERY_KEY = "allow-range-query-with-inline-sharding";
// private String algorithmExpression;
// private boolean allowRangeQuery;
// private Properties props = new Properties();
public MyStandardShardingAlgorithm() {
}
@Override
public String doSharding(Collection collection, PreciseShardingValue shardingValue) {
/* Closure<?> closure = this.createClosure();
closure.setProperty(shardingValue.getColumnName(), shardingValue.getValue());
System.out.println("-------------------------------------------------------");
return closure.call().toString();*/
//这里写具体的分片方法 shardingjdbcValue就是传过来的分片值,经过自己处理后返回相应的字符串即为所选片名
return "max_temp_log_1";
}
@Override
public Collection<String> doSharding(Collection availableTargetNames, RangeShardingValue rangeShardingValue) {
return availableTargetNames;
/* if (this.allowRangeQuery) {
return availableTargetNames;
} else {
throw new UnsupportedOperationException("Since the property of `allow-range-query-with-inline-sharding` is false, inline sharding algorithm can not tackle with range query.");
}*/
}
/* private Closure<?> createClosure() {
Closure<?> result = (new InlineExpressionParser(this.algorithmExpression)).evaluateClosure().rehydrate(new Expando(), (Object)null, (Object)null);
result.setResolveStrategy(3);
return result;
}*/
@Override
public void init() {
/* this.algorithmExpression = this.getAlgorithmExpression();
this.allowRangeQuery = this.isAllowRangeQuery();*/
}
/*
private boolean isAllowRangeQuery() {
return Boolean.parseBoolean(this.props.getOrDefault("allow-range-query-with-inline-sharding", Boolean.FALSE.toString()).toString());
}
private String getAlgorithmExpression() {
String expression = "max_temp_log_$->{equipment_id % 2}";
Preconditions.checkNotNull(expression, "Inline sharding algorithm expression cannot be null.");
return InlineExpressionParser.handlePlaceHolder(expression.trim());
}
*/
@Override
public String getType() {
return "CLASS_BASED";
}
}
这个就是分片的核心方法,具体的方式根据需求来修改
二、配置使用自定义的算法类
修改yml
配置文件
spring:
shardingsphere:
datasource:
names: db0
db0:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/nbserverdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: root
filters: stat,wall,log4j2
# 初始连接数
initialSize: 5
# 最小连接池数量
minIdle: 10
# 最大连接池数量
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
# 配置一个连接在池中最大生存的时间,单位是毫秒
maxEvictableIdleTimeMillis: 900000
# 配置检测连接是否有效
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
webStatFilter:
enabled: true
filter:
stat:
enabled: true
# 慢SQL记录
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
# db1:
# driver-class-name: com.mysql.cj.jdbc.Driver
# type: com.alibaba.druid.pool.DruidDataSource
# url: jdbc:mysql://192.168.3.155:30002/nbserverdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
# username: root
# password: root
rules:
# 配置分片规则
sharding:
keyGenerators:
snowflake:
type: SNOWFLAKE
props:
workerId: 123
tables:
# 配置 maxtemlog 表规则
max_temp_log:
actualDataNodes: db0.max_temp_log_$->{0..1}
# 配置分库策略
# databaseStrategy:
# standard:
# shardingColumn: equipment_id
# shardingAlgorithmName: auto-mod-4
# 配置分表策略
tableStrategy:
standard:
shardingColumn: equipment_id
shardingAlgorithmName: table-inline
keyGenerateStrategy:
column: max_temp_log_id
keyGeneratorName: snowflake
# 配置分片算法
bindingTables: max_temp_log
autoTables: # 自动分片表规则配置
user: # 逻辑表名称
actualDataSources: db0 # 数据源名称
shardingStrategy: # 切分策略
standard: # 用于单分片键的标准分片场景
shardingColumn: user_id # 分片列名称
shardingAlgorithmName: auto-mod-1 # 自动分片算法名称
sharding-algorithms:
auto-mod-4:
type: mod
props:
sharding-count: 1
auto-mod-1:
type: mod
props:
sharding-count: 1
database-inline:
type: INLINE
props:
algorithm-expression: db$->{equipment_id % 2}
table-inline:
type: CLASS_BASED #自定义type
props:
strategy: STANDARD #标准分片类型
algorithmClassName: com.haiwei.springadmin.config.MyStandardShardingAlgorithm ##这里填写自定义类的完整包路径
这里就是自定义核心配置
总结
修改完成就可以自己打断点测试了