sharding-jdbc5系列教程(一)springboot配置shardingjdbc+mybatis-plus+druid+dynamic-datasource
前言,本系列教程都是基于shardingjdbc5.0+版本以上的本片讲解如何自定义分片算法
一、实现自己分片算法类 看文档可以知道
分片策略有三种类型,支持 STANDARD、COMPLEX 或 HINT(不区分大小写)
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) { //这里写具体的分片方法 shardingjdbcValue就是传过来的分片值,经过自己处理后返回相应的字符串即为所选片名 return "max_temp_log_1"; } @Override public Collection这个就是分片的核心方法,具体的方式根据需求来修改 二、配置使用自定义的算法类 修改ymldoSharding(Collection availableTargetNames, RangeShardingValue rangeShardingValue) { return availableTargetNames; } @Override public void init() { } @Override public String getType() { return "CLASS_baseD"; } }
配置文件
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 ##这里填写自定义类的完整包路径这里就是自定义核心配置 总结
修改完成就可以自己打断点测试了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)