server.port=6666
spring.application.name=demo
# 内存模式
spring.shardingsphere.mode.type=Memory
# 配置真实数据源
spring.shardingsphere.datasource.names=master,slave1,slave2
# 配置第 1 个数据源
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://192.168.208.131:3306/demo?useUnicode=true&characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=root
# 配置第 2 个数据源
spring.shardingsphere.datasource.slave1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave1.jdbc-url=jdbc:mysql://192.168.208.131:3307/demo?useUnicode=true&characterEncoding=utf-8
spring.shardingsphere.datasource.slave1.username=root
spring.shardingsphere.datasource.slave1.password=root
# 配置第 3 个数据源
spring.shardingsphere.datasource.slave2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave2.jdbc-url=jdbc:mysql://192.168.208.131:3308/demo?useUnicode=true&characterEncoding=utf-8
spring.shardingsphere.datasource.slave2.username=root
spring.shardingsphere.datasource.slave2.password=root
# 读写分离配置
spring.shardingsphere.rules.readwrite-splitting.data-sources.readwrite_ds.type=Static
spring.shardingsphere.rules.readwrite-splitting.data-sources.readwrite_ds.props.write-data-source-name=master
spring.shardingsphere.rules.readwrite-splitting.data-sources.readwrite_ds.props.read-data-source-names=slave1,slave2
# 根据自定义名称来选择轮询负载均衡算法
spring.shardingsphere.rules.readwrite-splitting.data-sources.readwrite_ds.load-balancer-name=algorithm--round-robin
####### 供选负载均衡算法,3选1 #######
# 1.轮询
spring.shardingsphere.rules.readwrite-splitting.load-balancers.algorithm-round-robin.type=ROUND_ROBIN
# 2.随机
spring.shardingsphere.rules.readwrite-splitting.load-balancers.algorithm-random.type=RANDOM
# 3.权重算法(从库读访问比例)
spring.shardingsphere.rules.readwrite-splitting.load-balancers.algorithm-weight.type=WEIGHT
spring.shardingsphere.rules.readwrite-splitting.load-balancers.algorithm-weight.props.slave1=1.0
spring.shardingsphere.rules.readwrite-splitting.load-balancers.algorithm-weight.props.slave2=2.0
# 打印sql
spring.shardingsphere.props.sql-show=true
Shardingsphere5.1.2读写分离配置
于 2024-05-16 17:53:12 首次发布