ShardingSphere-JDBC 5.x+ 版本的 YAML 规则配置详解

以下是针对 ShardingSphere-JDBC 5.x+ 版本的 YAML 规则配置详解,按您要求的条目整理:


1. 数据分片 (Sharding)

作用:将单表数据水平拆分到多个库/表。
核心配置

rules:
- !SHARDING
  tables:
    t_order: # 逻辑表名
      actualDataNodes: ds_${0..1}.t_order_${0..1} # 物理表:ds0.t_order_0, ds0.t_order_1, ds1.t_order_0...
      databaseStrategy: # 分库策略
        standard:
          shardingColumn: user_id
          shardingAlgorithmName: db_hash_mod
      tableStrategy: # 分表策略
        standard:
          shardingColumn: order_id
          shardingAlgorithmName: t_mod
  shardingAlgorithms:
    db_hash_mod:
      type: HASH_MOD # 哈希取模
      props:
        sharding-count: 2 # 分片数(库)
    t_mod:
      type: MOD # 取模
      props:
        sharding-count: 2 # 分片数(表)

2. 广播表 (Broadcast Table)

作用:所有库中均存在的表(如字典表),写操作同步到所有库。
配置

rules:
- !SHARDING
  broadcastTables: t_config, t_dict # 广播表列表

3. 读写分离 (Read/Write Splitting)

作用:将读请求路由到从库,写请求到主库。
配置

rules:
- !READWRITE_SPLITTING
  dataSources:
    pr_ds: # 逻辑数据源名
      writeDataSourceName: master_ds # 主库
      readDataSourceNames: 
        - slave_ds0
        - slave_ds1
      loadBalancerName: round_robin # 读库负载均衡策略
  loadBalancers:
    round_robin:
      type: ROUND_ROBIN # 轮询

4. 分布式事务 (Distributed Transaction)

作用:支持 XA 或柔性事务(如 Seata)。
配置

rules:
- !TRANSACTION
  defaultType: XA # 可选 XA, BASE (柔性事务)
  providerType: Atomikos # XA 实现(可选 Narayana, Bitronix)
props:
  seata: # 若使用 Seata
    enabled: true
    application-id: your_app
    tx-service-group: my_tx_group

5. 数据加密 (Data Encryption)

作用:对敏感字段加密存储(如手机号)。
配置

rules:
- !ENCRYPT
  tables:
    t_user:
      columns:
        phone:
          cipherColumn: phone_cipher # 密文列
          encryptorName: aes_encryptor
  encryptors:
    aes_encryptor:
      type: AES # 加密算法
      props:
        aes-key-value: 123456abc # 密钥

6. 数据脱敏 (Data Masking)

作用:查询时动态脱敏敏感数据(如身份证后四位用****替换)。
配置

rules:
- !MASK
  tables:
    t_user:
      columns:
        id_card:
          maskColumn: id_card # 脱敏列
          maskAlgorithmName: mask_id_card
  maskAlgorithms:
    mask_id_card:
      type: MASK # 内置脱敏算法
      props:
        mask-start: 3 # 从第3位开始脱敏
        mask-length: 4 # 脱敏4位
        mask-symbol: '*' # 替换符号

7. 影子库 (Shadow DB)

作用:压测流量路由到影子库,不影响生产数据。
配置

rules:
- !SHADOW
  enable: true
  dataSources:
    shadow_ds: # 影子数据源
      sourceDataSourceName: real_ds # 生产数据源
      shadowDataSourceName: shadow_ds # 影子库
  tables:
    t_order:
      dataSourceNames: 
        - shadow_ds
      shadowAlgorithmNames:
        - simple_hint
  shadowAlgorithms:
    simple_hint:
      type: SIMPLE_HINT # 基于SQL注释的路由
      props:
        foo: bar # 自定义属性(可忽略)

8. SQL 解析 (SQL Parsing)

配置入口:全局属性控制解析行为(非独立规则)。

props:
  sql-show: true # 打印解析后的SQL
  sql-comment-parse-enabled: true # 解析注释
  parse-tree-cache: # 解析树缓存
    initial-capacity: 128
    maximum-size: 1024

9. SQL 翻译 (SQL Translation)

作用:跨异构数据库(如 MySQL -> PostgreSQL)自动转换方言。
配置

rules:
- !SQL_TRANSLATOR
  type: CONVERTER # 转换器类型
  useOriginalSQLWhenTranslatingFailed: true # 转换失败时回退原SQL
props:
  sql-translate-enabled: true # 开启翻译

10. 混合规则 (Hybrid Rules)

场景:同时启用分片+加密+读写分离等。
配置要点

rules:
- !SHARDING
  # 分片规则...
- !READWRITE_SPLITTING
  # 读写分离规则...
- !ENCRYPT
  # 加密规则...
# 注意:规则执行顺序按配置从上到下!

11. 数据分片路由缓存 (Sharding Route Cache)

作用:缓存分片路由结果,提升性能。
配置

props:
  sharding-cache-enabled: true # 启用缓存
  sharding-cache-refresh-interval: 60000 # 缓存刷新间隔(ms)

12. 单表 (Single Table)

作用:未配置分片规则的普通表,自动广播到所有数据源。
配置:无需显式声明,但需在数据源中实际存在该表


13. 联邦查询 (Federate Query)

说明

  • 实验性功能(5.3.0+),通过 FEDERATION 执行引擎跨多个逻辑表 JOIN 查询。
  • 无需特殊规则配置,依赖执行引擎优化。

完整配置示例

dataSources:
  ds_0: ...
  ds_1: ...
  master_ds: ...
  slave_ds0: ...

rules:
- !SHARDING
  tables:
    t_order: ...
  broadcastTables: t_config
- !READWRITE_SPLITTING
  dataSources: ...
- !ENCRYPT
  tables: ...
- !SHADOW
  ...

props:
  sql-show: true
  sharding-cache-enabled: true

关键注意事项

  1. 版本兼容性:确保配置语法与 ShardingSphere-JDBC 版本匹配(5.x 配置与 4.x 差异较大)。
  2. 规则优先级:混合规则时,执行顺序 = 配置顺序。
  3. 单表限制:单表需在所有真实数据源中存在且结构一致。
  4. 影子库生效:需在 SQL 中通过 Hint 或特定注释(如 /* SHARDINGSPHERE_HINT: SHADOW=true */)触发。

建议参考 官方文档 获取最新配置细节。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值