ShardingSphere每一张分表都需要单据配置yml吗?
ShardingSphere批量配置表
在Apache ShardingSphere中
并不是每一张分表都必须单独配置YAML规则
可通过通配符匹配、INLINE 表达式、算法复用 等方式实现批量配置分表规则
从而避免为每张表手动编写重复的配置
| 场景 | 是否需要单独配置 | 说明 |
| 分片逻辑一致(如按ID取模) | ❌ 不需要 | 可使用 `INLINE` 表达式统一配置 |
| 分片逻辑不同(如按时间、地区分片) | ✅ 需要 | 应单独定义分片策略和算法 |
| 使用通配符或正则表达式 | ❌ 不需要 | 可以批量匹配多个逻辑表 |
| 读写分离/加密等全局规则 | ❌ 不需要 | 对所有表生效 |
批量配置多张分表
1.使用 `INLINE` 表达式(推荐)
适用于分片逻辑一致的情况(如按用户ID取模):
```yaml
rules:
- !SHARDING
tables:
t_order: # 逻辑表名
actual-data-nodes: ds$->{0..1}.t_order_$->{0..1}
table-strategy:
standard:
sharding-column: order_id
sharding-algorithm-name: order_table_inline
sharding-algorithms:
order_table_inline:
type: INLINE
props:
algorithm-expression: t_order_$->{order_id % 2}
> 上述配置表示:无论插入哪个订单记录,
都会根据 `order_id % 2` 自动路由到 `t_order_0` 或 `t_order_1`。
2.使用通配符配置多个逻辑表
当有多个类似结构的表(如 `t_user`, `t_log`, `t_order`)
可使用通配符进行统一分片配置
```yaml
rules:
- !SHARDING
default-table-strategy:
standard:
sharding-column: user_id
sharding-algorithm-name: common_table_inline
sharding-algorithms:
common_table_inline:
type: INLINE
props:
algorithm-expression: $->logicTable_$->{user_id % 4}
注意:
ShardingSphere目前不完全支持“通配逻辑表”的自动映射
通常需要结合Spring Boot中Java配置动态生成规则
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


