加入我的shardingsphere分片字段是code;
配置的策略是对code计算的结果。
比如
detection-db-sharding:
type: INLINE
props:
algorithm-expression: data$->{code/5}
这种配置,最终无法找到数据库。
或者配置两个数据库 code>1也不行
加入我的shardingsphere分片字段是code;
配置的策略是对code计算的结果。
比如
detection-db-sharding:
type: INLINE
props:
algorithm-expression: data$->{code/5}
这种配置,最终无法找到数据库。
或者配置两个数据库 code>1也不行
使用分片一般是'%' 不是 ‘/’ 参考下这个
使用的包 implementation 'org.apache.shardingsphere:shardingsphere-jdbc-core-spring-boot-starter:5.0.0-alpha'
# shardingsphere 的sql 日志不显示
spring.shardingsphere.props.sql.show=true
# 解决bean重名问题,重名原因是 build 与shardingsphere中的jdbc的bean重名
spring.main.allow-bean-definition-overriding=true
#打印sql 关闭分库sql查询内容
spring.shardingsphere.props.sql-show=false
### 配置数据源
spring.shardingsphere.datasource.names=ds0,ds1
## 配置第 1 个数据源
spring.shardingsphere.datasource.common.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=
spring.shardingsphere.datasource.ds0.username=
spring.shardingsphere.datasource.ds0.password=
### 配置第 2 个数据源
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=
spring.shardingsphere.datasource.ds1.username=
spring.shardingsphere.datasource.ds1.password=
#配置雪花算法
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=123
# 总体库表配置 针对yuan_log表
spring.shardingsphere.rules.sharding.tables.yuan_log.actual-data-nodes=ds$->{0..1}.yuan_log_$->{0..1}
## 配置库,
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds$->{log_id % 2}
# 配置表
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.table-inline.props.algorithm-expression=yuan_log_$->{log_id % 2}
# 表分片键
spring.shardingsphere.rules.sharding.tables.yuan_log.table-strategy.standard.sharding-column=log_id
spring.shardingsphere.rules.sharding.tables.yuan_log.table-strategy.standard.sharding-algorithm-name=table-inline
# 库分片键
spring.shardingsphere.rules.sharding.tables.yuan_log.database-strategy.standard.sharding-column=log_id
spring.shardingsphere.rules.sharding.tables.yuan_log.database-strategy.standard.sharding-algorithm-name=database-inline
spring.shardingsphere.enabled=true