SpringBoot 3.x.x整合ShardingSphere-JDBC 5.5.0 实现读写分离,数据分片,数据脱敏功能

一、前言

  • 随着业务的迭代发展,并发量逐渐变大,在不考虑缓存等其他方案的前提下,数据库性能就可能出现瓶颈,为了解决并发性能,就需要进行读写分离,将流量进行分流,写操作走主库,读操作走从库,同时伴随着数据的增多,单库单表也会出现性能问题,通过分库分表对数据进行分治,从而提高性能. 不过读写分离和分库分表也会带来其他问题,比如主从延迟,分布式事务,跨库联表查询等等,所以要根据实际情况确定详细的方案.这里就不进行阐述.

二、集成ShardingSphere-JDBC

  • ShardingSphere-JDBC 定位为轻量级 Java 框架,在 Java 的 JDBC 层提供的额外服务。ShardingSphere官网
  • 在实际的集成过程中,会遇到各种各样的问题,网上的文章大多数版本比较低,ShardingSphere版本更新变化也比较大,会出现各种依赖问题,配置格式不对,功能不生效等等.
  • 版本号:spring 3.2.4 + ShardingSphere-JDBC 5.5.0
  • pom的引入,shardingsphere-jdbc-core-spring-boot-starter作者不进行维护了,所以按照官网介绍引入指定的依赖.
  • application 配置文件
  • sharding 配置文件(重点)
yaml 复制代码
dataSources:
  ds0:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://localhost:3306/demo_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: admin123
    poolName: test0
    minPoolSize: 2
    maxPoolSize: 20
  ds1:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: admin123
    poolName: test1
    minPoolSize: 5
    maxPoolSize: 20
  ds2:
    dataSourceClassName: com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
    username: root
    password: admin123
    poolName: test2
    minPoolSize: 5
    maxPoolSize: 20
rules:
  #  - !SINGLE
  #    tables:
  #      - "*.order_*"
  - !READWRITE_SPLITTING
    dataSources:
      readwrite_ds:
        writeDataSourceName: ds0
        readDataSourceNames:
          - ds1
          - ds2
        loadBalancerName: ROUND_ROBIN
    loadBalancers:
      ROUND_ROBIN:
        type: ROUND_ROBIN
  - !SHARDING
    #    autoTables:
    #      order:
    #        actualDataSources: ds1,ds2
    #        shardingStrategy:
    #          standard:
    #            shardingColumn: order_id
    #            shardingAlgorithmName: auto_mod
    tables:
      order:
        actualDataNodes: readwrite_ds.order_${0..1}
        tableStrategy:
          standard:
            shardingColumn: order_id
            shardingAlgorithmName: order_inline
    #          complex:
    #            shardingColumns: customer_id,order_id
    #            shardingAlgorithmName: customer
    defaultTableStrategy:
      none:
    shardingAlgorithms:
      auto_mod:
        type: MOD
        props:
          sharding-count: 2
      order_inline:
        type: INLINE
        props:
          algorithm-expression: order_${order_id % 2}
      customer:
        type: CLASS_BASED
        props:
          strategy: COMPLEX
          algorithmClassName: com.example.shardingsphere.service.CustomerShardingAlgorithm
  - !MASK
    tables:
      order:
        columns:
          delivery_address:
            maskAlgorithm: keep_first_n_last_m_mask

    maskAlgorithms:
      keep_first_n_last_m_mask:
        type: KEEP_FIRST_N_LAST_M
        props:
          first-n: 1
          last-m: 2
          replace-char: '*'
props:
  sql-show: true
#  sql-simple: true
  • 注意点:
  • 图中画线的要注意,使用内置的分片策略要配合自动分片标签配置,官网只有简单场景的配置示例,所以在实际业务配置过程中容易出现问题,看了github的issues大多数都是配置不正确导致的,当时觉得官网文档不能覆盖各种场景,配置过于复杂,后来发现作者在examples模块做了说明,并且可以生成各种配置示例.
  • demo链接:github.com/huang-xuan/...
相关推荐
武子康3 小时前
大数据-165 Apache Kylin Cube7 实战:聚合组/RowKey/编码与体积精度对比
大数据·后端·apache kylin
qinyia3 小时前
WisdomSSH解决因未使用Docker资源导致的磁盘空间不足问题
运维·服务器·人工智能·后端·docker·ssh·github
庄宿正3 小时前
【Vue2+SpringBoot+SM2】Vue2 + Spring Boot 实现 SM2 双向非对称加密完整实战
java·spring boot·后端
A***F1573 小时前
使用 Spring Boot 实现图片上传
spring boot·后端·状态模式
间彧4 小时前
分享一些ServBay和Docker混合使用的最佳实践?
后端
间彧4 小时前
一个典型的SpringBoot Web项目在ServBay和Docker中分别的完整开发部署流程
后端
间彧4 小时前
ServBay如何与IDE(如IntelliJ IDEA)深度集成,实现一键调试和热部署?
后端
万少4 小时前
流碧卡片 6 小时闪电开发 AI gemini-3-pro-preview ! 秒出小红书爆款图,免下载直接用
前端·后端·ai编程
间彧4 小时前
ServBay与Docker在具体使用场景和性能表现上有哪些详细对比?
后端
间彧4 小时前
ServBay 详解:一站式本地 Web 开发环境
后端