- 1. ShardingSphere-JDBC 读写分离
- 1.1 环境搭建
- 1.1.1 pom.xml
- 1.1.2 创建数据库和表
- 1.1.3 编写业务代码
- 1.1.4 配置文件
- 1.1.5 测试结果
- 2. 说明
- 3.坑
读写分离:写数据往主库写,读数据从从库读。
环境说明:SpringBoot 2.5.7
+ MyBatisPlus
+ ShardingSphere-JDBC 5.0.0-alpha
+ Druid
+ MySQL 8.0
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
com.alibaba
druid
1.1.13
com.baomidou
mybatis-plus-boot-starter
3.4.3
mysql
mysql-connector-java
8.0.27
org.apache.shardingsphere
shardingsphere-jdbc-core-spring-boot-starter
5.0.0-alpha
org.springframework.boot
spring-boot-starter-test
test
1.1.2 创建数据库和表
按照水平分表的方式,创建数据库和数据库表
- 创建数据库:
ss_course_db_1
、ss_course_db_2
、ss_dict_db
- 具体操作,可以参考前面两篇文章
对三个数据库都进行了主从配置,但此处只用读写分离,还不涉及分库分表,所有只涉及ss_dict_db数据库
对于配置多个数据库的主从配置,需要在MySQL的my.ini文件中,配置同步多个数据库即可,配置如下,主从配置文件都要加。
binlog-do-db=ss_dict_db binlog-do-db=ss_course_db_1 binlog-do-db=ss_course_db_2
此处编写业务代码略,具体代码可以下面的源码地址,经过本人调试过。代码里集成了Swagger
,用于方便测试。
server.port=8004
spring.shardingsphere.enabled=true
# 打开sql输出日志
spring.shardingsphere.props.sql-show=true
# 配置数据源,给数据源起名称
spring.shardingsphere.datasource.names=m1,m2
# 配置第一个数据源具,主数据库
spring.shardingsphere.datasource.common.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.common.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.url=jdbc:mysql://127.0.0.1:3306/ss_dict_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=123456
# 配置第二个数据源,从数据库
spring.shardingsphere.datasource.m2.url=jdbc:mysql://127.0.0.1:3307/ss_dict_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root
# 负载均衡算法
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.type=ROUND_ROBIN
# 不配置此项会报错
spring.shardingsphere.rules.replica-query.load-balancers.round-robin.props.default=0
# 主库
spring.shardingsphere.rules.replica-query.data-sources.prds.primary-data-source-name=m1
# 从库
spring.shardingsphere.rules.replica-query.data-sources.prds.replica-data-source-names=m2
spring.shardingsphere.rules.replica-query.data-sources.prds.load-balancer-name=round_robin
1.1.5 测试结果
启动程序,在浏览器输入:http://localhost:8004/swagger-ui.html
初始时数据:3306为主库,3307为从库
添加公共数据
日志打印只向m1数据源中插入数据,即3306主数据库。
查看数据库表数据
查看所有数据
日志显示,只从m2数据源查询,即3307从数据库。
源码地址:https://github.com/Hofanking/springboot-shardingsphere-example
源代码目录结构说明:
springboot-shardingsphere-example
|— shardingsphere-database (分库分表)
|— shardingsphere-database-table-write-read (分库分表读写分离)
|— shardingsphere-proxy-table (使用proxy分表)
|— shardingsphere-public (公共表)
|— shardingsphere-table (分表)
|— shardingsphere-write-read (读写分离)
3.坑下面是官方文档读写分离的配置说明:
https://shardingsphere.apache.org/document/5.0.0-alpha/cn/user-manual/shardingsphere-jdbc/configuration/spring-boot-starter/replica-query/
按照上面的配置,根本不会成功,对应的地方要写成
prds
。具体可以查看对应版本的github
仓库代码。
https://github.com/apache/shardingsphere/blob/5.0.0-alpha/examples/shardingsphere-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-replica-query.properties