为了解决数据库的性能问题。前提条件是数据库同步。
导包:
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.22version>
dependency>
<dependency>
<groupId>org.apache.shardingspheregroupId>
<artifactId>sharding-jdbc-spring-boot-starterartifactId>
<version>4.0.0-RC1version>
dependency>
配置文件
# 3.sharding-jdbc读写分离
shardingsphere:
# 数据源配置
datasource:
names: master,slave1,slave2
# master数据源
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx:3306/bks_user?serverTimezone=UTC?useUnicode=true&useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf-8
username: root
password: xxx
# slave1数据源
slave1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx:3306/bks_user?serverTimezone=UTC?useUnicode=true&useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf-8
username: root
password: xxx
# slave2数据源
slave2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://xxx:3306/bks_user?serverTimezone=UTC?useUnicode=true&useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf-8
username: root
password: xxx
# 读写分离配置
masterslave:
# 从库负载均衡类型:随机
load-balance-algorithm-type: round_robin
name: ms
master-data-source-name: master
slave-data-source-names: slave1,slave2
# 显示SQL
props:
sql:
show: true