时间:2021-07-01 10:21:17 帮助过:29人阅读
sharding官网
https://shardingsphere.apache.org/document/legacy/3.x/document/cn/overview/
maven依赖配置
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.0.0.M3</version>
</dependency>
数据源配置
#数据源名称,多数据源以逗号分隔
sharding.jdbc.datasource.names=ds-0,ds-1,ds-2
#未配置分片规则的表将通过默认数据源定位-适用于单库单表,该表无需配置分片规则
spring.shardingsphere.sharding.default-data-source-name=ds0
#ds-0
sharding.jdbc.datasource.ds-0.type=org.apache.commons.dbcp2.BasicDataSource
#sharding.jdbc.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-0.url=jdbc:mysql://192.168.237.162:3306/license?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
sharding.jdbc.datasource.ds-0.username=root
sharding.jdbc.datasource.ds-0.password=admindev123$%^
#ds-1
sharding.jdbc.datasource.ds-1.type=org.apache.commons.dbcp2.BasicDataSource
#sharding.jdbc.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-1.url=jdbc:mysql://192.168.237.162:3307/license?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
sharding.jdbc.datasource.ds-1.username=root
sharding.jdbc.datasource.ds-1.password=admindev123$%^
#ds-2
sharding.jdbc.datasource.ds-2.type=org.apache.commons.dbcp2.BasicDataSource
#sharding.jdbc.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.ds-2.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-2.url=jdbc:mysql://192.168.237.162:3308/license?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
sharding.jdbc.datasource.ds-2.username=root
sharding.jdbc.datasource.ds-2.password=admindev123$%^
sharding.jdbc.config.masterslave.load-balance-algorithm-type=round_robin
sharding.jdbc.config.masterslave.name=ms
sharding.jdbc.config.masterslave.master-data-source-name=ds-0
sharding.jdbc.config.masterslave.slave-data-source-names=ds-2,ds-1
#是否开启SQL显示,默认值: false
spring.shardingsphere.props.sql.show= true
1主2从数据库测试
查询
新增
于ShardingJDBC的单库分片
#数据源名称,多数据源以逗号分隔
#这里是给取的别名,下面配置要使用
sharding.jdbc.datasource.names=ds-0
#未配置分片规则的表将通过默认数据源定位-适用于单库单表,该表无需配置分片规则
#spring.shardingsphere.sharding.default-data-source-name=ds0
#ds-0
sharding.jdbc.datasource.ds-0.type=org.apache.commons.dbcp2.BasicDataSource
sharding.jdbc.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds-0.url=jdbc:mysql://192.168.237.162:3306/license?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
sharding.jdbc.datasource.ds-0.username=root
sharding.jdbc.datasource.ds-0.password=admindev123$%^
#分表相关配置 note是我数据库要分表的表前缀,ds.f_licensecreatorparams_000$->{0..1}这里表示ds数据库中的f_licensecreatorparams_0000,f_licensecreatorparams_0001两个表
sharding.jdbc.config.sharding.tables.f_licensecreatorparams.actual-data-nodes=ds-0.f_licensecreatorparams_000$->{0..2}
## 配置分表策略
## 配置分片算法
#指定f_licensecreatorparams表中主键的生成策略 SNOWFLAKE:雪花算法,随机生成主键Id
sharding.jdbc.config.sharding.tables.f_licensecreatorparams.key-generator-column-name=id
spring.shardingsphere.sharding.tables.f_licensecreatorparams.key-generator.type=SNOWFLAKE
# 指定分片的策略
# 分片健
sharding.jdbc.config.sharding.tables.f_licensecreatorparams.table-strategy.inline.sharding-column=id
# 分片算法(分了3张表 id % 3)
sharding.jdbc.config.sharding.tables.f_licensecreatorparams.table-strategy.inline.algorithm-expression=f_licensecreatorparams_000$->{id % 3}
#sharding.jdbc.config.sharding.binding-tables=f_licensecreatorparams
出现的问题解决
Springboot 的 autoconfiguration 中 datasource 与 shardingjdbc 的datasource 冲突,导致sharding jdbc 的datasource 注入失败
解决办法:
在配置文件(application.yml)中设置允许 重复名称 bean 覆盖,这样让后面配置的sharding jdbc 的datasource 覆盖掉 springboot 的 autoconfiguration的datasource
spring.main.allow-bean-definition-overriding: true
ShardingJDBC
标签:查询 dbcp hang timezone 分片 开启 info jdbc 数据库