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


2899

被折叠的 条评论
为什么被折叠?



