Springboot 集成 Shardingsphere-JDBC

Springboot 集成 Shardingsphere-JDBC

  • Shardingsphere系列目录:
  • 背景
  • 前提
  • 新增依赖
  • 分表策略
    • 简单分库分表策略
      • 垂直分库
      • 广播表
      • 水平分库(单表)
      • 水平分库(多表)
      • 水平分表
    • HINT
      • 配置
      • 逻辑代码
    • 自定义分库分表(精准定位+范围查询)
      • 配置
      • 代码
        • 精准定位数据库
        • 精准定位+范围查询表
  • 特殊情况
  • 代码仓库地址

Shardingsphere系列目录:

【Springboot 集成 Shardingsphere-JDBC】
【Shardingsphere-Proxy 5.5.0部署】

背景

项目中,某数据库单表数据量已达到4000w+,查询和插入数据性能越来越差,跟踪发现这个表一个月有8w的新增数据量,所以需要进行分库分表减轻单库单表的压力。

前提

Shardingsphere不会为你自动建库建表,需要自行将分开的表以及对应数据库全部创建出来。

新增依赖

有两个依赖提供给大家

<dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>4.1.1</version>
</dependency><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId><version>5.2.1</version>
</dependency>

以上两个依赖只能加一个,建议使用后者,因为毕竟差着1个大版本,并且,前者最高版本就是4.1.1,看样子好像是不会再维护了。

分表策略

简单分库分表策略

垂直分库

# 垂直分库
spring:application:name: sharding-jdbcshardingsphere:datasource:# 数据源别名db1:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverpassword: 1234type: com.zaxxer.hikari.HikariDataSourceurl: jdbc:mysql://localhost:3306/sharding_payorder_db?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=trueusername: root# 数据源别名db2:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverpassword: 1234type: com.zaxxer.hikari.HikariDataSourceurl: jdbc:mysql://localhost:3306/sharding_user_db?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=trueusername: root# 数据源别名列表names: db1,db2props:# 展示执行sqlsql-show: truerules:sharding:# 标准分片表配置,由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持 inline 表达式。tables:pay_order:actual-data-nodes: db1.pay_orderusers:actual-data-nodes: db2.users

广播表

# 广播表
spring:application:name: sharding-jdbcshardingsphere:datasource:# 数据源别名db:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sharding_course_db?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名db0:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sharding_course_db0?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名db1:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sharding_course_db1?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名列表names: db0,db1,dbprops:sql-show: truerules:sharding:# 广播表配置broadcast-tables:- t_districttables:t_district:# 广播表设置,定位广播表所在数据源actual-data-nodes: db$->{0..1}.t_district,db.t_district

水平分库(单表)

# 水平分库(单表)
spring:application:name: sharding-jdbcshardingsphere:datasource:# 数据源别名db0:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sharding_course_db0?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名db1:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sharding_course_db1?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名列表names: db0,db1props:sql-show: truerules:sharding:# 主键生成算法定义key-generators:# 主键生成算法定义名称alg-snowflake:# 对应主键生成算法type: SNOWFLAKE# 分片算法定义sharding-algorithms:# 分片算法定义名称inline-hash-mod:props:# 分片算法表达式algorithm-expression: t_course_$->{Math.abs(cid.hashCode()) % 2}# 分片算法类型,行表达式分片算法type: INLINE# 分片算法定义名称table-inline:props:# 分片算法表达式algorithm-expression: db$->{user_id % 2}# 分片算法类型,行表达式分片算法type: INLINEtables:t_course:# 配置表节点actual-data-nodes: db$->{0..1}.t_course_$->{0..1}# 数据库分片策略database-strategy:standard:# 数据库分片策略名称,在上面的 sharding-algorithms 内容中配置sharding-algorithm-name: table-inline# 数据库分片列名称sharding-column: user_id# t_course表主键生成策略key-generate-strategy:# 主键列名称column: cid# 主键生成算法名称,在上面的 key-generators 内容中配置key-generator-name: alg-snowflake# 表分片策略table-strategy:standard:# 表分片策略名称,在上面的 sharding-algorithms 内容中配置sharding-algorithm-name: inline-hash-mod# 表分片列名称sharding-column: cid

水平分库(多表)

# 水平分库(多表)
spring:application:name: sharding-jdbcshardingsphere:datasource:# 数据源别名db0:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sharding_course_db0?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名db1:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sharding_course_db1?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名列表names: db0,db1props:sql-show: truerules:sharding:# 绑定表配置binding-tables:- t_course,t_course_section# 主键生成算法定义key-generators:# 主键生成算法定义名称alg-snowflake:# 对应主键生成算法type: SNOWFLAKE# 分片算法定义sharding-algorithms:# 分片算法定义名称table-hash-mod:props:# 指定分片数量sharding-count: 2# 哈希取模分片算法type: HASH_MOD# 分片算法类型,分片算法定义名称table-mod:props:# 指定分片数量sharding-count: 2# 片算法类型,取模分片算法type: MODtables:t_course:# 配置表节点actual-data-nodes: db$->{0..1}.t_course_$->{0..1}# 数据库分片策略database-strategy:standard:# 数据库分片策略名称,在上面的 sharding-algorithms 内容中配置sharding-algorithm-name: table-mod# 数据库分片列名称sharding-column: user_id# t_course表主键生成策略key-generate-strategy:# 主键列名称column: cid# 主键生成算法名称,在上面的 key-generators 内容中配置key-generator-name: alg-snowflake# 表分片策略table-strategy:standard:# 表分片策略名称,在上面的 sharding-algorithms 内容中配置sharding-algorithm-name: table-mod# 表分片列名称sharding-column: corder_not_course_section:# 配置表节点actual-data-nodes: db$->{0..1}.t_course_section_$->{0..1}# 数据库分片策略database-strategy:standard:# 数据库分片策略名称,在上面的 sharding-algorithms 内容中配置sharding-algorithm-name: table-mod# 数据库分片列名称sharding-column: user_id# t_course_section表主键生成策略key-generate-strategy:# 主键列名称column: id# 主键生成算法名称,在上面的 key-generators 内容中配置key-generator-name: alg-snowflake# 表分片策略table-strategy:standard:# 表分片策略名称,在上面的 sharding-algorithms 内容中配置sharding-algorithm-name: table-mod# 表分片列名称sharding-column: corder_no

水平分表

# 水平分表
spring:application:name: sharding-jdbcshardingsphere:datasource:# 数据源别名db1:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/sharding_course_db?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: roottdms:driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourceurl: jdbc:mysql://localhost:3306/jvs_tdms?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=trueusername: rootpassword: 1234# 数据源别名列表names: db1,tdms# 未分片数据库,如果分片数据库也有未分片的表,会优先查分片数据库的这个表,否则去查默认的未分片数据库sharding:default-data-source-name: tdmsprops:sql-show: truerules:sharding:# 主键生成算法定义key-generators:# 主键生成算法定义名称alg-snowflake:# 对应主键生成算法type: SNOWFLAKE# 分片算法定义sharding-algorithms:# 分片算法定义名称table-inline:props:# 分片算法表达式algorithm-expression: t_course_$->{cid % 2 + 1}# 分片算法类型,行表达式分片算法type: INLINEtables:t_course:# 配置表节点actual-data-nodes: db1.t_course_$->{1..2}# t_course表主键生成策略key-generate-strategy:# 主键列名称column: cid# 主键生成算法名称,在上面的 key-generators 内容中配置key-generator-name: alg-snowflaketable-strategy:standard:# 表分片策略名称,在上面的 sharding-algorithms 内容中配置sharding-algorithm-name: table-inline# 表分片列名称sharding-column: cid

以上这些配置较简单,最后我会附上工程,大家可以拿去借鉴,只修改yml配置即可,不用修改逻辑代码:
在这里插入图片描述

HINT

该方式我单独拎出来,是因为这个是能实现精准定位表来更新数据的方式,但是数据库的分片逻辑需要配置。

配置

spring:application:name: sharding-jdbcshardingsphere:datasource:# 数据源别名db1:# 数据库连接信息driver-class-name: com.mysql.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/tss_db?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名列表names: db1
#    props:
#      sql-show: truerules:sharding:# 分片算法定义sharding-algorithms:# 分片算法定义名称table-inline:type: HINT_INLINEprops:algorithm-expression: tss_table_$->{value}# 分片算法定义名称table-tenant-inline:type: HINT_INLINEprops:algorithm-expression: tss_tenant_table_$->{value}tables:tss_table:# 配置表节点actual-data-nodes: db1.tss_table_$->{1..31}table-strategy:hint:# 表分片策略名称,在上面的 sharding-algorithms 内容中配置sharding-algorithm-name: table-inlinetss_tenant_table:# 配置表节点actual-data-nodes: db1.tss_tenant_table_$->{1..31}table-strategy:hint:# 表分片策略名称,在上面的 sharding-algorithms 内容中配置sharding-algorithm-name: table-tenant-inline

最后我附上的工程中也有相关配置。

逻辑代码

@SpringBootTest
class TssTests {@Resourceprivate TssTableMapper tssTableMapper;/*** tss数据库batch insert语句测试*/@Testpublic void testTssBatchInsert() {HintManager hintManager = HintManager.getInstance();// 指定插到的表名及配置里写明的后缀hintManager.addTableShardingValue("tss_table", 7);List<TssTable> tssTableList = tssTableMapper.selectList(null);hintManager.addTableShardingValue("tss_table", 6);for(TssTable item : tssTableList){tssTableMapper.insert(item.setId(null));}hintManager.close();}
}

自定义分库分表(精准定位+范围查询)

这个是最灵活的一种方式,既需要配置也需要修改逻辑代码,我以上面提供的5.2.1版本的依赖方式实现:

配置

# tss水平分库分表精准分片
spring:application:name: sharding-jdbcshardingsphere:datasource:# 数据源别名db0:# 数据库连接信息driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/tss_db?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名db1:# 数据库连接信息driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/tss_db_2?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名列表names: db0,db1rules:sharding:# 算法配置sharding-algorithms:# 数据库分片算法my-db-strategy:# 算法类型:自定义算法type: CLASS_BASEDprops:# 算法策略:标准算法strategy: standard# 自定义算法类路径algorithmClassName: com.shardingjdbc.config.upgrade.MyDbShardingAlgorithm# 表分片算法my-tss-table-strategy:# 算法类型:自定义算法type: CLASS_BASEDprops:# 算法策略:标准算法strategy: standard# 自定义算法类路径algorithmClassName: com.shardingjdbc.config.upgrade.TssTableShardingAlgorithm# 表分片算法my-tss-tenant-table-strategy:# 算法类型:自定义算法type: CLASS_BASEDprops:# 算法策略:标准算法strategy: standard# 自定义算法类路径algorithmClassName: com.shardingjdbc.config.upgrade.TssTenantTableShardingAlgorithmtables:tss_table:# 配置表节点actual-data-nodes: db$->{0..1}.tss_table_$->{1..31}database-strategy:standard:sharding-algorithm-name: my-db-strategysharding-column: tenant_idtable-strategy:standard:sharding-column: opt_datesharding-algorithm-name: my-tss-table-strategytss_tenant_table:# 配置表节点actual-data-nodes: db$->{0..1}.tss_tenant_table_$->{1..31}database-strategy:standard:sharding-algorithm-name: my-db-strategysharding-column: tenant_idtable-strategy:standard:sharding-column: opt_datesharding-algorithm-name: my-tss-tenant-table-strategy

配置中一定要注意别忘了rules关键字。

代码

精准定位数据库

在插入数据的时候,这个类【有PreciseShardingValue传参的doSharding方法】可以根据配置中的数据库策略传来的字段值,然后进行逻辑操作之后【返回数据库名】直接定位插入的数据库。

package com.shardingjdbc.config.upgrade;import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;@Slf4j
public class MyDbShardingAlgorithm implements StandardShardingAlgorithm<String> {@Overridepublic String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {// 获取分片键的值if(collection.isEmpty()){log.error("------- shardingsphere log database list is empty !!!");return "";}String value = preciseShardingValue.getValue();String logicTableName = preciseShardingValue.getLogicTableName();log.info("------- shardingsphere log database list:{}, value: {}, logicTableName: {}",collection,value,logicTableName);// 计算hashString dataSourceIndex = String.valueOf(Math.abs(value.hashCode()) % collection.size());log.info("------- shardingsphere log dataSourceIndex: {}",dataSourceIndex);// 排序,怕之后根据后缀定表会出现问题,2后缀有2,12,22,排序之后endWith筛选后选第一个就没有问题了List<String> sortedDbName = collection.stream().sorted().collect(Collectors.toList());// 查询对应数据库名String result = sortedDbName.stream().filter(item -> item.endsWith(dataSourceIndex)).findFirst().orElse(null);if(StringUtils.isBlank(result)){result = sortedDbName.get(0);}return result;}@Overridepublic Collection<String> doSharding(Collection<String> collection, RangeShardingValue<String> rangeShardingValue) {return null;}@Overridepublic Properties getProps() {return null;}@Overridepublic void init(Properties properties) {}
}
精准定位+范围查询表
  1. 可以根据自己的逻辑确定要插入数据的表名
  2. 可以根据查询的范围来指定筛选的表范围,避免全库全表查询,仅查询部分表即可,提高查询效率。
package com.shardingjdbc.config.upgrade;import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.collect.Range;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;import java.util.*;@Slf4j
public class TssTenantTableShardingAlgorithm implements StandardShardingAlgorithm<Date> {@Overridepublic String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {log.info("------- shardingsphere log table name get in TssTenantTableShardingAlgorithm collection = {} , preciseShardingValue = {}",collection, preciseShardingValue);//可以获取三个值,也就是course逻辑表名,columnName id,value 获取的值String logicTableName = preciseShardingValue.getLogicTableName();Date value = preciseShardingValue.getValue();Calendar cal = Calendar.getInstance();cal.setTime(value);String key = logicTableName + "_" + cal.get(Calendar.DAY_OF_MONTH);log.info("------- shardingsphere log table name = {}",key);if (StringUtils.isNotBlank(key)) {return key;}return null;}@Overridepublic Collection<String> doSharding(Collection<String> collection, RangeShardingValue<Date> rangeShardingValue) {log.info("------- shardingsphere log table name get in TssTenantTableShardingAlgorithm collection = {} , rangeShardingValue = {}",collection, rangeShardingValue);// 获取分片键值,时间类型的值(LocalDateTime等)会自动转为java.sql.Timestamp,可以直接用java.util.Date接收Range<Date> valueRange = rangeShardingValue.getValueRange();// 获取范围小值Date lowerEndpoint = valueRange.lowerEndpoint();// 获取范围大值Date upperEndpoint = valueRange.upperEndpoint();Calendar startTime = Calendar.getInstance();Calendar endTime = Calendar.getInstance();startTime.setTime(lowerEndpoint);endTime.setTime(upperEndpoint);// 逻辑表名String logicTableName = rangeShardingValue.getLogicTableName();Set<String> tables = new HashSet<>();// 比较两个时间while (startTime.compareTo(endTime) <= 0) {// 添加到集合tables.add(buildTable(logicTableName, startTime));// 往后加一个月startTime.add(Calendar.DAY_OF_MONTH,1);}if (tables.isEmpty()) {log.error("------- shardingsphere log table name is empty");}log.info("------- shardingsphere log table name list = {}", tables);return tables;}private String buildTable(String logicTableName, Calendar dateTime) {return logicTableName + "_" + dateTime.get(Calendar.DAY_OF_MONTH);}@Overridepublic Properties getProps() {return null;}@Overridepublic void init(Properties properties) {}
}

特殊情况

如果你的这个工程需要连接三个数据库,一个数据库中放的全是未分片的单表另两个数据库有实现分库的分片表,这种情况需要进行要怎么配置呢?配置如下:

spring:application:name: sharding-jdbcshardingsphere:datasource:# 单表数据源别名db:# 数据库连接信息driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/tss_db?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 分库数据源别名db0:# 数据库连接信息driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/tss_db1?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 分库数据源别名db1:# 数据库连接信息driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://localhost:3306/tss_db_2?allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&useSSL=false&maxReconnects=666&failOverReadOnly=false&initialTimeout=10&zeroDateTimeBehavior=convertToNull&allowMultiQueries=truepassword: 1234type: com.zaxxer.hikari.HikariDataSourceusername: root# 数据源别名列表names: db,db0,db1rules:sharding:# 算法配置sharding-algorithms:# 数据库分片算法my-db-strategy:# 算法类型:自定义算法type: CLASS_BASEDprops:# 算法策略:标准算法strategy: standard# 自定义算法类路径algorithmClassName: com.shardingjdbc.config.upgrade.MyDbShardingAlgorithm# 表分片算法my-tss-table-strategy:# 算法类型:自定义算法type: CLASS_BASEDprops:# 算法策略:标准算法strategy: standard# 自定义算法类路径algorithmClassName: com.shardingjdbc.config.upgrade.TssTableShardingAlgorithm# 表分片算法my-tss-tenant-table-strategy:# 算法类型:自定义算法type: CLASS_BASEDprops:# 算法策略:标准算法strategy: standard# 自定义算法类路径algorithmClassName: com.shardingjdbc.config.upgrade.TssTenantTableShardingAlgorithmtables:tss_table:# 配置表节点actual-data-nodes: db$->{0..1}.tss_table_$->{1..31}database-strategy:standard:sharding-algorithm-name: my-db-strategysharding-column: tenant_idtable-strategy:standard:sharding-column: opt_datesharding-algorithm-name: my-tss-table-strategytss_tenant_table:# 配置表节点actual-data-nodes: db$->{0..1}.tss_tenant_table_$->{1..31}database-strategy:standard:sharding-algorithm-name: my-db-strategysharding-column: tenant_idtable-strategy:standard:sharding-column: opt_datesharding-algorithm-name: my-tss-tenant-table-strategy

与上面的【自定义分库分表】配置相比,差别就两个:

  1. 新增spring.application.shardingsphere.datasource.db单表数据源配置。
  2. 修改spring.application.shardingsphere.datasource.names数据源别名配置。

代码仓库地址

https://gitee.com/hepai123/shardingjdbc-demo.git

[develop-tss-upgrade]分支是上面说的【自定义分库分表(精准定位+范围查询)】源码,除此之外,其他的源码全在 [develop-tss]分支

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/30494.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

什么是本地启动?

今天在进行接口测试的时候&#xff0c;我刚开始傻乎乎的&#xff0c;不会测试嘛&#xff0c;那个 postman 里面叫你填那个URL&#xff0c;我就把设备管理系统的地址填了上去&#xff1a;http://192.168.0.237.27000/。 睿哥看了之后&#xff0c;跟我说&#xff1a;“你填这个地…

Linux DNS配置文档

一、问题描述 1. 无法在浏览器通过域名访问百度&#xff1b; 2. 无法在终端 ping 通百度&#xff0c;例如&#xff1a;ping www.baidu.com 3. 可以 ping 通公网地址&#xff0c;例如&#xff1a;ping 114.114.114.114 或 ping 8.8.8.8 二、问题原因 域名解析 DNS 配置错误&am…

数据结构历年考研真题对应知识点(单链表、双链表、循环链表)

目录 2.3线性表的链式表示 2.3.1单链表的定义 【单链表的应用(2009、2012、2013、2015、2016、2019)】 2.3.2单链表上基本操作的实现 【单链表插入操作后地址或指针的变化(2016)】 2.3.3双链表 【双链表中插入操作的实现(2023)】 【循环双链表中删除操作的实现(2016)】 …

如何抓取 GitHub:实用教程 2024

GitHub 是互联网上最重要的技术知识来源之一&#xff0c;对于构建复杂应用程序的开发人员来说尤其如此。跟随本指南学习如何提取这些宝贵的数据&#xff0c;毫不费力地紧跟最新技术趋势。 了解 GitHub 数据 开源项目文化为开发人员提供了许多分享、贡献和合作的机会&#xff…

多个类下所有方法AOP

微服务下有两个类&#xff0c;需要做异常捕获再抛出&#xff0c;笔者立马想到了AOP 非微服务&#xff0c;只能使用代理 Slf4j Aspect Component public class SdkAspect {Pointcut("execution(* com.aspire.service.impl.XxxEncryption.*(..))")public void enPointc…

android的surface

相信很多Android开发者都知道Canvas类是UI的画布&#xff08;虽然这种说法并不严谨&#xff09;&#xff0c;因为我们在Canvas上完成各种图形的绘制&#xff0c;那么我们Activity上的各种交互控件又是如何展示并渲染到屏幕上的呢&#xff0c;所以在另一个层面上也有一个“画布”…

安卓开发拉起其他应用的常用方式

在安卓开发中&#xff0c;拉起其他应用&#xff08;即启动其他应用&#xff09;有几种常见的方式&#xff1a; 通过显式 Intent&#xff1a; 这种方式需要知道目标应用的包名和具体的 Activity 名称。 Intent intent new Intent(); intent.setComponent(new ComponentName(…

地图上绘制地铁线路

需求背景 不管是之前的pms 地铁还是location都会有需求涉及到地图上绘制地铁线路&#xff0c;来查看当前位置是否靠近地铁口&#xff0c;常规的交互可以看下高德地图&#xff0c;如图所示&#xff1a; 需求分析 不管是高德地图还是百度地图都提供了简易版的地铁线路图&#x…

Excel如何设置自动更新的固定选项

日常工作中你是否想要某数据列设置固定选项&#xff0c;如人力组、财务组、综合组、业务组等&#xff0c;可用“数据验证”实现&#xff0c;如后期新增选项“党建组”&#xff0c;该如何快速处理&#xff1f; 今天刘小生分享“超级表数据验证”方式&#xff0c;只实现固定选项…

pytorch笔记:清理GPU内存

1 Control-C 中止运行GPU 存储没有及时释放 在使用 GPU 进行深度学习训练时&#xff0c;通过 Control-C 中止程序后&#xff0c;有时会发现 GPU 内存没有及时释放这主要是因为以下几个原因 进程未完全终止&#xff1a; 当我们按下 Control-C 时&#xff0c;只是发送了一个中断…

若依RuoYi-Vue分离版—富文本Quill的图片支持伸缩大小及布局

若依RuoYi-Vue分离版—富文本Quill的图片支持伸缩大小及布局、工具栏带中文提示 1.在vue.config.js 文件中添加 一下内容2.下载安装插件3.在Editor组件中引入插件4.使用Editor组件&#xff08;特别注意要的加 v-if &#xff09;5.bug 之 imageResize的 img的style丢失1.先创建一…

不是所有洗碗机都能空气除菌 友嘉灵晶空气除菌洗碗机评测

精致的三餐让你以为生活是“享受”&#xff0c;可饭后那些油腻的锅碗瓢盆却成了你我美好生活的最大障碍。想要只吃美食不洗碗&#xff0c;那一台优秀的洗碗机就必不可少了&#xff01;今天&#xff0c;ZOL中关村在线要评测的就是这样一台不光洗得干净更能有效除菌抑菌的洗碗机—…

SpringBoo+vue3+vite整合讯飞星火3.5通过webscoket实现聊天功能(前端代码)附带展示效果

访问地址&#xff1a; 天梦星服务平台 (tmxkj.top)https://tmxkj.top/#/site 后端文档&#xff1a; SpringBoovue3整合讯飞星火3.5通过webscoket实现聊天功能&#xff08;全网首发&#xff09;附带展示效果_springboot websocket vue3-CSDN博客https://blog.csdn.net/qq_53722…

SAP MIGO 050 BADI:字段 GOITEM-XXXXX 未准备好输出

背景&#xff1a; MIGO过账时候需要根据某些条件更改某些字段的值&#xff0c;当要改的字段在前台不显示时&#xff0c;通过MB_MIGO_BADI~LINE_MODIFY去更改时&#xff0c;则会出现以下报错&#xff1a;MIGO050 解决方案1&#xff1a; 通过配置将该字段配置显示出来即可&…

【文末附gpt升级秘笈】关于“登月游戏”的详细内容介绍

当然可以。以下是关于“登月游戏”的详细内容介绍&#xff1a; 一、游戏背景与目标 “登月游戏”是一款基于1969年人类首次登陆月球事件而开发的计算机游戏。其背景设定在月球表面&#xff0c;玩家需要扮演宇航员&#xff0c;操控登月器在月球上实现软着陆。游戏的目标是在确…

【教师资格证考试综合素质——法律专项】未成年人保护法笔记以及练习题

《中华人民共和国未成年人保护法》 目录 第一章 总 则 第二章 家庭保护 第三章 学校保护 第四章 社会保护 第五章 网络保护 第六章 政府保护 第七章 司法保护 第八章 法律责任 第九章 附 则 介一&#xff0e;首次颁布&#xff1a;第一部《中华人民共和国未成年人保护法…

电影美学复古胶片特效视频转场模板 | Premiere Pro 项目工程文件

这个Premiere Pro项目工程文件是一个电影美学胶片特效视频转场模板&#xff0c;每个过渡效果都散发出一种有机的怀旧魅力&#xff0c;让人回忆起经典电影卷轴和模拟摄影的独特美感。 项目特点&#xff1a; 胶片烧伤过渡效果&#xff1a;包括从微妙的闪烁到大胆的爆发&#xff…

学习总结报告模板

学习总结报告模板1 --年10月15日进入--公司至今已近两周时间&#xff0c;通过这段时间的工作和学习&#xff0c;已经适应了新的工作环境&#xff0c;了解了公司的发展历史及企业文化、认清了公司的组织结构及配置&#xff0c;熟识了大部分的同事&#xff0c;掌握了公司的大部分…

pom.xml文件里面各个标签的作用

在 Maven 项目中&#xff0c;POM&#xff08;Project Object Model&#xff09;文件用于定义项目的基本信息、依赖、插件和其他构建设置。理解 POM 文件中的各个标签的作用非常重要。以下是常见标签及其作用的详细解释&#xff1a; 顶层元素 <project> 这个元素是 POM…

南充文化旅游职业学院领导一行莅临泰迪智能科技参观交流

6月18日&#xff0c;南充文化旅游职业学院旅游系副书记刘周、教务处教学运行与质量保障科科长及智慧旅游技术应用专业教研室主任李月娴、大数据技术专业负责人 龙群才、大数据技术专业专任教师 李昱洁莅临泰迪智能科技产教融合实训中心参观交流。泰迪智能科技董事长张良均、副总…