No Data

springboot3 shardingsphere5 mysql 主从配置 读写分离

原创  作者:斩雪碎光阴  发布于:2024年04月10日  阅读量:121
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
  分类:  标签:

1.官方文档


https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/rules/readwrite-splitting/


2.建三个数据库


建三个数据库mysql数据库,配置mysql主从同步,一主二从

可参考: https://sgybk.cn/article94


shardingsphereconf.yaml中添加:

...

dataSources:

 write_ds:

   dataSourceClassName: com.zaxxer.hikari.HikariDataSource

   driverClassName: com.mysql.cj.jdbc.Driver

   jdbcUrl: jdbc:mysql://192.168.83.130:3307/shardingSphereTest?useUnicode=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai

   username: root

   password: root

 read_ds_0:

   dataSourceClassName: com.zaxxer.hikari.HikariDataSource

   driverClassName: com.mysql.cj.jdbc.Driver

   jdbcUrl: jdbc:mysql://192.168.83.130:3308/shardingSphereTest?useUnicode=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai

   username: root

   password: root

 read_ds_1:

   dataSourceClassName: com.zaxxer.hikari.HikariDataSource

   driverClassName: com.mysql.cj.jdbc.Driver

   jdbcUrl: jdbc:mysql://192.168.83.130:3309/shardingSphereTest?useUnicode=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai

   username: root

   password: root

...


3.配置读写分离数据源


写数据源write_ds对应主库、读数据源read_ds_0对应从库、读数据源read_ds_1对应从库

配置读写分离数据源readwrite_ds


shardingsphereconf.yaml中添加:

...

rules:

 - !SINGLE

   tables:

     - readwrite_ds.* # 加载指定数据源中的全部单表

 - !READWRITE_SPLITTING

   dataSources:

     readwrite_ds:

       writeDataSourceName: write_ds

       readDataSourceNames:

         - read_ds_0

         - read_ds_1

       transactionalReadQueryStrategy: PRIMARY

       loadBalancerName: roundRobin

   loadBalancers:

     roundRobin:

       type: ROUND_ROBIN

...


4.打印sql


shardingsphereconf.yaml中添加:

...

props:

   sql-show: true

...

官方文档:

https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/props/


5.关于事务的处理


使用限制

不处理主库和从库的数据同步

不处理主库和从库的数据同步延迟导致的数据不一致

不支持主库多写

不处理主从库间的事务一致性。主从模型中,事务中的数据读写均用主库。


官方文档:

https://shardingsphere.apache.org/document/current/cn/features/readwrite-splitting/limitations/


shardingsphereconf.yaml中添加:

...

transactionalReadQueryStrategy: PRIMARY

...


路由至主库,事务中的数据读写均用主库。


官方文档:

https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/rules/readwrite-splitting/


测试事务:

http://localhost:3010/shardingSphereTest/user/saveAndList


注:在JUnit环境下的@Transactional注解,默认情况下会对事务进行回滚(即使在没加注解@Rollback,也会对事务回滚)


6.负载均衡算法


shardingsphereconf.yaml中添加:

...

dataSources:

    ...

       loadBalancerName: roundRobin

   loadBalancers:

     roundRobin:

       type: ROUND_ROBIN

...


配置轮询负载均衡算法


官方文档:

https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/algorithm/


其他算法

官方文档:

https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/load-balance/


7.参考代码

链接: https://pan.baidu.com/s/1MMMT-NtINl37pYZkjcOsIQ?pwd=1111

提取码:1111

需要用init.sql中语句建表,更换数据库链接和账号密码

相关文章