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/db0
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
      ds0_slave:
        url: jdbc:mysql://localhost:3307/db0
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
      ds1:
        url: jdbc:mysql://localhost:3306/db1
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver
      ds1_slave:
        url: jdbc:mysql://localhost:3307/db1
        username: root
        password: root
        driver-class-name: com.mysql.cj.jdbc.Driver

    rules:
      sharding:
        tables: # 分库分表的配置
          order: # 逻辑表order,因为你分库分表后可能order表实际上有多个,比如ds0.order_0、ds0.order_1、ds1.order_0、ds1.order_1
            actual-data-nodes: ds$->{0..1}.order_$->{0..1} # 指定它的真实物理表集合
            table-strategy: # 定义分表策略是按照order_id
              standard:
                sharding-column: order_id # 逻辑表 order_item
                sharding-algorithm-name: order-inline # 定义一个名为 order-inline 的分片算法,用于分库
          order_item: # 逻辑表order_item
            actual-data-nodes: ds$->{0..1}.order_item_$->{0..1} # 分布在两个库的两张表上
            table-strategy: 
              standard:
                sharding-column: order_id
                sharding-algorithm-name: order-inline  
        default-database-strategy: # 定义分库策略是按照user_id
          standard:
            sharding-column: user_id
            sharding-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: INLINE
            props:
              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: ds0
              read-data-source-names:
                - ds0_slave
            load-balancer-name: round-robin
          rw_ds1:
            static-strategy:
              write-data-source-name: ds1
              read-data-source-names:
                - ds1_slave
            load-balancer-name: round-robin
        load-balancers:
          round-robin:
            type: ROUND_ROBIN

    props: # 开启 SQL 打印,调试时非常有用,能看到 SQL 是如何被改写和路由的
      sql:
        show: true

  main: # 假设项目用了druid等,需要开启下面这个重写,否则报错
    allow-bean-definition-overriding: true
相关推荐
一只鹿鹿鹿4 小时前
政务外网整体设计方案(PPT)
大数据·数据库·web安全·系统安全·流程图·政务
哦虎!4 小时前
【JavaEE】网络初识
java
2603_965148115 小时前
社区与学习资源:从入门到精通的API选品学习路径
数据库·人工智能·python·学习·数据挖掘
for_ever_love__5 小时前
MySQL ORDER BY 排序优化:从 Using filesort 到索引有序扫描(含双路 / 单路排序原理)
java·mysql·优化·存储·by·order·filesort
AI砖家5 小时前
安全进入 Agent Loop:2027 年 Coding Agent 的决胜点
数据库·安全·codex·vibecoding
wangqiaowq5 小时前
SQL server C盘空间占满的处理
数据库·sql
文人sec5 小时前
SQL基础入门笔记 | 从零基础到掌握核心查询语句
数据库·笔记·sql
数字新视界5 小时前
机房动环监控提升运维效率
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
我是人✓6 小时前
Java文件上传三方式对比:从传统到SpringBoot
java·开发语言
SelectDB技术团队6 小时前
冷热分层从建到验:Storage Policy 创建、表级与分区级绑定、SHOW TABLETS 验证的完整命令
大数据·数据库·elasticsearch·搜索引擎·数据分析·apache doris·冷热分层