ShardingSphere读写分离实战
背景说明
这是一篇简易入门实战文章,就是将ShardingSphere接入到我们的业务,完成读写分离改造
实战操作
配置文件
server.port=9999
mybatis-plus.global-config.db-config.table-prefix=t_
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.hkshardsphere.entity
# 数据源内容
spring.shardingsphere.datasource.names=ds0,ds1,ds2
spring.shardingsphere.datasource.common.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.jdbc.Driver
# 主数据库
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://192.168.0.101:3306/sharddata?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=UTC
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
# 从数据库1
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://192.168.0.101:3307/sharddata?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=UTC
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root
# 从数据库2
spring.shardingsphere.datasource.ds2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds2.jdbc-url=jdbc:mysql://192.168.0.101:3308/sharddata?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=UTC
spring.shardingsphere.datasource.ds2.username=root
spring.shardingsphere.datasource.ds2.password=root
# Sharding 统一配置
# 展示执行sql信息
spring.shardingsphere.props.sql-show=true
spring.shardingsphere.props.sql-simple=tue
spring.main.allowBeanDefinitionOverriding=true
spring.shardingsphere.rules.readwrite-splitting.data-sources.ms.type=Static
#负载均衡
spring.shardingsphere.rules.readwrite-splitting.load-balancers.round-robin.type=ROUND_ROBIN
spring.shardingsphere.rules.readwrite-splitting.data-sources.ms.loadBalancerName=round-robin
spring.shardingsphere.rules.readwrite-splitting.data-sources.ms.props.write-data-source-name=ds0
spring.shardingsphere.rules.readwrite-splitting.data-sources.ms.props.read-data-source-names=ds1,ds2
spring.shardingsphere.rules.readwrite-splitting.data-sources.ms.props.transactionalReadQueryStrategy=PRIMARY
代码地址
求文推荐
觉得文章对你有帮助,记得点赞收藏关注,一键三连