前置读写分离配置 https://blog.csdn.net/q18771811872/article/details/88636181
springboot项目创建教程 https://blog.csdn.net/q18771811872/article/details/88126835
接创建教程里面的demo项目 ,无缝衔接
当然sharding不知读写分离这么简单
直接上代码
pom.xml
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.1.0.M1</version>
</dependency>
application.yml
sharding:
jdbc:
dataSource:
names: db-test0,db-test1,db-test2
# 配置主库
db-test0: #org.apache.tomcat.jdbc.pool.DataSource
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db-test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
password: 123456
#最大连接数
maxPoolSize: 20
db-test1: # 配置第一个从库
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db-test1?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
username: root
password: 123456
maxPoolSize: 20
db-test2: # 配置第二个从库
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db-test2?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=GMT
username: root
password: 123456
maxPoolSize: 20
config:
masterslave: # 配置读写分离
load-balance-algorithm-type: round_robin # 配置从库选择策略,提供轮询与随机,这里选择用轮询//random 随机 //round_robin 轮询
name: db1s2
master-data-source-name: db-test0
slave-data-source-names: db-test1,db-test2
props:
sql: # 开启SQL显示,默认值: false,注意:仅配置读写分离时不会打印日志!!!
show: true
一般配置完 maven包后,springboot 的默认数据库连接就会优选选择sharding的了
springboot2.1说明:
2.1比较严格,所以呢,需要在配置文件添加一个属性
spring:
main:
allow-bean-definition-overriding: true
就可以正常启动了
配置好后,直接重启
直接贴了源码, 就不贴效果图了
前置读写分离配置 https://blog.csdn.net/q18771811872/article/details/88636181
springboot项目创建教程 https://blog.csdn.net/q18771811872/article/details/88126835