Sharding-Jdbc配置示例
#应用名称
spring:
application:
name: sharding-jdbc-test
#开发环境设置
profiles:
active: dev
#配置为内存模式
shardingsphere:
mode:
type: Memory
# 配置真实数据源
datasource:
names: server-order00,server-order01
# 配置第 1 个数据源
server-order00:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/sharding_jdbc_order01
username: root
password: 123456
# 配置第 2 个数据源
server-order01:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/sharding_jdbc_order02
username: root
password: 123456
#--------------------------------------标准分片的配置(数据节点配置)
#由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持 inline 表达式。
#缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
#https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/rules/sharding/
rules:
sharding:
tables: # 数据分片规则配置
t_order: # 逻辑表名称
actualDataNodes: server-order0$->{0..1}.t_order$->{0..1} # 由数据源名 + 表名组成(参考 Inline 语法规则)
databaseStrategy: # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
standard: # 用于单分片键的标准分片场景
shardingColumn: user_id # 分片列名称(数据库列字段,用户名称)
shardingAlgorithmName: t-order-inline # 分片算法名称
tableStrategy: # 分表策略,同分库策略
standard: # 用于单分片键的标准分片场景
shardingColumn: order_no # 分片列名称(数据库列字段,订单号)
shardingAlgorithmName: t-order-hash-mod # 分片算法名称
keyGenerateStrategy: # 分布式序列策略
column: id # 自增列名称,缺省表示不使用自增主键生成器
keyGeneratorName: kgan-snowflake # 分布式序列算法名称
t_order_item: # 逻辑表名称
actualDataNodes: server-order0$->{0..1}.t_order_item$->{0..1} # 由数据源名 + 表名组成(参考 Inline 语法规则)
databaseStrategy: # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
standard: # 用于单分片键的标准分片场景
shardingColumn: user_id # 分片列名称(数据库列字段,用户名称)
shardingAlgorithmName: t-order-inline # 分片算法名称
tableStrategy: # 分表策略,同分库策略
standard: # 用于单分片键的标准分片场景
shardingColumn: order_no # 分片列名称(数据库列字段,订单号)
shardingAlgorithmName: t-order-hash-mod # 分片算法名称
keyGenerateStrategy: # 分布式序列策略
column: id # 自增列名称,缺省表示不使用自增主键生成器
keyGeneratorName: kgan-snowflake # 分布式序列算法名称
t_dict: # 逻辑表名称
actualDataNodes: server-order0$->{0..1}.t_dict # 由数据源名 + 表名组成(参考 Inline 语法规则)
# ---------------------------------------分片算法配置
#https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/algorithm/
shardingAlgorithms:
#行表达式分片算法
t-order-inline: # 分片算法名称
# type 和 props,请参考分片内置算法:https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/
type: INLINE # 分片算法类型
props: # 分片算法属性配置
#t_order0$->{user_id % 2} 表示 t_order 表根据 user_id 取模(取余) 2,而分成 2 张表,表名称为 t_order01 到 t_order02
algorithm-expression: server-order0$->{user_id % 2}
#取模分片算法
t-order-mod: # 分片算法名称
# type 和 props,请参考分片内置算法:https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/
type: MOD # 分片算法类型
props: # 分片算法属性配置
#分片数量
sharding-count: 2
#哈希取模分片算法
t-order-hash-mod: # 分片算法名称
# type 和 props,请参考分片内置算法:https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/
type: HASH_MOD # 分片算法类型
props: # 分片算法属性配置
#分片数量
sharding-count: 2
# 分布式序列算法配置
#---------------------------------------分布式序列算法
#https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/keygen/
keyGenerators:
kgan-snowflake: # 分布式序列算法名称
type: SNOWFLAKE # 分布式序列算法类型
#props: # 分布式序列算法属性配置
#指分片规则一致的一组分片表。 使用绑定表进行多表关联查询时,必须使用分片键进行关联,否则会出现笛卡尔积关联或跨库关联,从而影响查询效率。
#例如:t_order 表和 t_order_item 表,均按照 order_id 分片,并且使用 order_id 进行关联,则此两张表互为绑定表关系。
#绑定表之间的多表关联查询不会出现笛卡尔积关联,关联查询效率将大大提升
#https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/
bindingTables: # 绑定表规则列表
- t_order,t_order_item #解决笛卡尔积查询
#指所有的分片数据源中都存在的表,表结构及其数据在每个数据库中均完全一致。 适用于数据量不大且需要与海量数据的表进行关联查询的场景,例如:字典表。
#https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/
broadcastTables: # 广播表规则列表
- t_dict
props:
#是否在日志中打印 SQL 打印 SQL 默认值 false
sql-show: true

表结构:
编辑
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构