0%

ShardingSphere-JDBC读写分离

ShardingSphere-JDBC读写分离

sharding-jdbc会通过sql语句进行语义分析,如果是insert、update、delete语句会路由到master库进行操作,如果是select语句会路由到slave库进行操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
spring:
application:
name: sharding-jdbc-test
shardingsphere:
datasource:
names: m0,s0 # 配置数据源,给数据源起名
m0: # 主库,配置数据源具体内容,连接池、驱动、地址、用户名、密码
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?useSSL=false
username: root
password: 123456
s0: # 从库,配置数据源具体内容,连接池、驱动、地址、用户名、密码
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis1?useSSL=false
username: root
password: 123456
# sharding: # 方式一
# master-slave-rules:
# m0:
# master-data-source-name: m0 #配置m0数据库的主库
# slave-data-source-names: s0 #配置m0数据库的从库
masterslave: # 方式二
name: ms
master-data-source-name: m0
slave-data-source-names: s0
load-balance-algorithm-type: round_robin
props:
sql:
show: true # 输出日志