应实际业务需求,实现一主多从读写分离。
sharding-jdbc
框架能够实现此功能,集成此框架到项目中去。
集成sharding-jdbc
前建议仔细阅读官网相关文档
shardingsphere 官网
shardingsphere github地址
步骤
- 引入相关依赖
<sharding-sphere.version>4.0.0-RC2</sharding-sphere.version>
<!-- sharding-jdbc -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
- 进行一主多从读写分离配置
spring:
shardingsphere:
datasource:
names: master,slave0,slave1
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://0.0.0.0:3306/master?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: pwd
initial-size: 10
max-active: 10
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
slave0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://0.0.0.0:3306/slave0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: pwd
initial-size: 10
max-active: 10
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
slave1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://0.0.0.0:3306/slave1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
username: root
password: pwd
initial-size: 10
max-active: 10
min-idle: 10
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
filter:
stat:
log-slow-sql: true
slow-sql-millis: 1000
merge-sql: true
wall:
config:
multi-statement-allow: true
sharding: # 实现 一主多从 读写分离 与 主数据库的分片,但目前 sharding 4.0.0-RC2 版本与MP整合存在 Long 转 int 异常暂时不可使用
binding-tables: z_sys_log_operation
tables:
z_sys_log_operation:
actual-data-nodes: ds0.z_sys_log_operation$->{2019..2020}
table-strategy:
inline:
sharding-column: sys_year
algorithm-expression: z_sys_log_operation$->{sys_year}
master-slave-rules:
ds0:
master-data-source-name: master
slave-data-source-names: slave0,slave1
load-balance-algorithm-type: round_robin
props:
sql:
show:true
说明
sharding-jdbc 的官方文档在我看来是比较绕的,尤其是配置文件,看着相当头大,仔细看绝对是看得懂的。上面配置一主两从的情况,此框架支持一主,这点是需要注意的。而且框架不复制主从数据的同步。在主库中配置数据分配,当前使用版本是4.0.0-RC2
在这个版本中是存在bug 的,与MP整合存在 Long 转 int 异常谨慎使用。此bug 已在github issue中反馈,据说将在下个版本4.0.0-RC3
中修复。