当前位置: 首页 > news >正文

sharding jdbc的使用,如何在Spring中实现数据库的主从分离、分库分表等功能

使用Sharding-JDBC就可以了,它是一个 轻量级的 Java JDBC 层中间件,用于实现分库分表、读写分离、分布式事务等功能,而且对于我们经常使用的mybatis之类的是兼容的。
以下用一个两主两从数据库作样例,因为这样既能分库分表,又能读写分离
不过当数据量和读写流量较小的时候,请谨慎评估是否需要分库分表和读写分离,然后再决定具体要采用下面这个application.yml里的哪些策略
只要在application.yml里配置好如下内容即可:

application.yml 配置说明:spring:shardingsphere:datasource:names: ds0, ds1, ds0_slave, ds1_slave # 声明所有数据源的逻辑名称(主库和从库)ds0:url: jdbc:mysql://localhost:3306/db0username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverds0_slave:url: jdbc:mysql://localhost:3307/db0username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverds1:url: jdbc:mysql://localhost:3306/db1username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverds1_slave:url: jdbc:mysql://localhost:3307/db1username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverrules:sharding:tables: # 分库分表的配置order: # 逻辑表order,因为你分库分表后可能order表实际上有多个,比如ds0.order_0、ds0.order_1、ds1.order_0、ds1.order_1actual-data-nodes: ds$->{0..1}.order_$->{0..1} # 指定它的真实物理表集合table-strategy: # 定义分表策略是按照order_idstandard:sharding-column: order_id # 逻辑表 order_itemsharding-algorithm-name: order-inline # 定义一个名为 order-inline 的分片算法,用于分库order_item: # 逻辑表order_itemactual-data-nodes: ds$->{0..1}.order_item_$->{0..1} # 分布在两个库的两张表上table-strategy: standard:sharding-column: order_idsharding-algorithm-name: order-inline  default-database-strategy: # 定义分库策略是按照user_idstandard:sharding-column: user_idsharding-algorithm-name: db-inline # 这里定义了一个算法,算法的实现在下面sharding-algorithms: # 实现db-inline: # 定义一个名为 db-inline 的分片算法,用于分库type: INLINE # 算法类型为内联表达式(INLINE),适用于简单取模类规则props:algorithm-expression: ds$->{user_id % 2} # 表达式:user_id 对 2 取模,结果为 0 或 1,映射到 ds0 或 ds1 两个数据库order-inline: # 定义一个名为 order-inline 的分片算法,用于分表type: INLINEprops:algorithm-expression: order_$->{order_id % 2} # 表达式:order_id 对 2 取模,映射到 order_0 或 order_1 两张物理表readwrite-splitting: # 读写分离配置data-sources:rw_ds0:static-strategy:write-data-source-name: ds0read-data-source-names:- ds0_slaveload-balancer-name: round-robinrw_ds1:static-strategy:write-data-source-name: ds1read-data-source-names:- ds1_slaveload-balancer-name: round-robinload-balancers:round-robin:type: ROUND_ROBINprops: # 开启 SQL 打印,调试时非常有用,能看到 SQL 是如何被改写和路由的sql:show: truemain: # 假设项目用了druid等,需要开启下面这个重写,否则报错allow-bean-definition-overriding: true
http://www.xdnf.cn/news/666127.html

相关文章:

  • Java· swing 小demo
  • EasyDarwin的配置与使用
  • MMAction2重要的几个配置参数
  • 《算法笔记》13.2小节——专题扩展->树状数组(BIT) 问题 C: Count Inversions
  • C++面试题:虚函数表(vtable)的底层实现机制与应用解析
  • 守护手部稳定,手抖健康护理全攻略
  • 【关于C++跨平台开发的挑战】
  • 【C++】内存管理,深入解析new、delete
  • 【DAY30】模块和库的导入
  • Docker Volume(存储卷)
  • 动态库版本不配问题排查步骤
  • 牛客round94D
  • java使用https协议访问(自签名证书,运行时指定信任库(不修改系统证书))
  • 城市污水管网流量在线监测方案
  • VPet虚拟桌宠,一款桌宠软件,支持各种互动投喂等. 开源免费并且支持创意工坊
  • 如何搭建perfino监控(分析java服务性能)
  • 从姿势到心态:痉挛性斜颈的多维护理方案
  • old语音识别科大讯飞+deepseek api
  • SOC-ESP32S3部分:13-定时器
  • 删掉省市区的市辖区
  • 推理模型 vs 非推理模型:核心区别及优劣势解析
  • 3.微服务架构编码Base工程模块构建
  • 【stm32开发板】产品设计流程及元件选型
  • 创业团队建设与管理(一)
  • 牛客round94E
  • 「Unity3D」TextMeshPro的TMP_InputField在改变高度时,其中textComponent移动的问题解决
  • VMware Live Recovery 和 VMware Data Recovery区别
  • python 报错记录-Linux 退出python环境
  • Python Day34
  • 聚合CPA/CPS拉新分销平台开发:2025年核心功能与未来趋势解析