ShardingJdbc5.0.0 hint分片策略配置简化+如何继续集成读写分离

使用痛点:
1、现系统存在60个库 100张表 配置hint 分片策略,配置起来非常繁琐,是否有简化的配置
2、如何再此分片基础继续配置读写分离
配置如下:
spring.shardingsphere.datasource.names=ds1,ds2,ds3,ds4,ds5,ds6,ds7,ds8,ds9,ds10,ds11,ds12,ds13,ds14,ds15,ds16,ds17,ds18,ds19,ds20,ds21,ds22,ds23,ds24,ds25,ds26,ds27,ds28,ds29,ds30,ds31,ds32,ds33,ds34,ds35,ds36,ds37,ds38,ds39,ds40,ds41,ds42,ds43,ds44,ds45,ds46,ds47,ds48,ds49,ds50,ds51,ds52,ds53,ds54,ds55,ds56,ds57,ds58,ds59,ds60
spring.shardingsphere.datasource.common.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver‐class‐name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://xxx.db.com:3306/MY_DB1?serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds1.username=admin
spring.shardingsphere.datasource.ds1.password=123456
spring.shardingsphere.datasource.ds2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds2.driver‐class‐name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds2.url=jdbc:mysql://xxx.db.com:3306/MY_DB2?serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds2.username=admin
spring.shardingsphere.datasource.ds2.password=123456
#等等 60个数据源配置

标准分片表配置

spring.shardingsphere.rules.sharding.tables.my_table1.actual-data-nodes=ds$->{1…60}.my_table1
spring.shardingsphere.rules.sharding.tables.my_table2.actual-data-nodes=ds$->{1…60}.my_table2
spring.shardingsphere.rules.sharding.tables.my_table3.actual-data-nodes=ds$->{1…60}.my_table3
#等等 100个配置
spring.shardingsphere.rules.sharding.tables.my_table100.database-strategy.hint.sharding-algorithm-name=my-hint
#默认库分片策略
spring.shardingsphere.sharding.default-database-strategy.hint.sharding-algorithm-name=my-hint

分片算法配置

spring.shardingsphere.rules.sharding.sharding-algorithms.my-hint.type=CLASS_BASED
spring.shardingsphere.rules.sharding.sharding-algorithms.my-hint.props.strategy=HINT
spring.shardingsphere.rules.sharding.sharding-algorithms.my-hint.props.algorithmClassName=com.sharding.shardingjdbc.DatabaseHintShardingKeyAlgorithm
spring.shardingsphere.props.sql-show=true

#注:DatabaseHintShardingKeyAlgorithm 实现按分片建取模hint 指定具体的 ds 如:3456%60+1=50 则数据源指向 ds50
问题:配置读写分离后 hint 该如何指向ds ?

1 个赞

1、现系统存在60个库 100张表 配置hint 分片策略,配置起来非常繁琐,是否有简化的配置——这个问题可以考虑开启治理功能,然后部署 Proxy 接入端,通过 DistSQL 方式来配置分片规则,然后 JDBC 端通过 schemaName 引用对应的配置即可。

2、如何再此分片基础继续配置读写分离——分片配合读写分离,可以参考下文档 混合规则,读写分离规则会聚合出一个逻辑数据源,提供给分片功能使用,Hint 是分片的一种路由策略,需要指向读写分离聚合之后的逻辑数据源。这样读写分离在路由处理时,会将逻辑数据源,根据读写规则路由到对应的主库或从库上。

大家好,我这里想做一个通过Hint来实现基于分库的多租户模式,想通过拦截器拦截到SQL,然后解析Hint,自动选择要连接的数据源节点。这个目前可以做吗?如果不可以,是否有支持这个功能的计划?
以前用mycat是有这个功能的,但SS貌似没找到这样的实现。
谢谢

解析Hint 是指 SQL 中写 Hint 吗?另外 mycat 支持的方式可否透露一下?

你看看这个功能是否是你需要的,目前正在规划做

可以了,感谢 !

京ICP备2021015875号