sharding-sphere读写分离,主从配置,权重配置。

1.读写分离

pom文件引入依赖

<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.1.0</version>
</dependency>

配置文件加上主、从数据库的配置。给从库增加一个只读账号,用只读账号进行读取数据。

# 配置真实数据源 
spring.shardingsphere.datasource.names=master,slave1 

# 配置第1个数据源 
# type必填
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
# 数据连接名称是jdbcUrl。用url会报错
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/my?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
spring.shardingsphere.datasource.master.hikari.maximum-pool-size=150
spring.shardingsphere.datasource.master.hikari.minimum-idle=100

# 配置第2个数据源
spring.shardingsphere.datasource.slave1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave1.jdbc-url=jdbc:mysql://localhost:3306/my?useUnicode=true&characterEnc
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值