使用 ShardingSphere 的分库分表配置文件详解
1. dataSources
部分
dataSources:
ds_0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/damai_user_0?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: 123456
ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/damai_user_1?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: 123456
作用:
dataSources
是配置多个数据源的部分。在分库分表场景中,可能会涉及到多个数据库实例(即多个数据源)。- 这里定义了两个数据源,
ds_0
和ds_1
,分别对应两个不同的数据库damai_user_0
和damai_user_1
。这两个数据库的表结构相同,但用于存储不同的数据片(分片)。 - 使用 Hikari 连接池(
com.zaxxer.hikari.HikariDataSource
)来管理数据库连接。 - 通过
jdbcUrl
、username
和password
来配置数据库的连接地址和认证信息。
2. rules
部分
rules
部分定义了 ShardingSphere 的分库分表规则、加密规则等,整体上负责定义数据的分布策略和安全策略。
2.1 !SHARDING
分库分表部分
- !SHARDING
tables:
d_user_mobile:
actualDataNodes: ds_${0..1}.d_user_mobile_${0..1}
databaseStrategy:
standard:
shardingColumn: mobile
shardingAlgorithmName: databaseUserMobileHashModModel
tableStrategy:
standard:
shardingColumn: mobile
shardingAlgorithmName: tableUserMobileHashMod
分库分表规则的核心:
!SHARDING
表示启用分片(Sharding)规则。tables
下列出了需要进行分片的表(例如d_user_mobile
)。
字段解释:
actualDataNodes
:表示实际的数据节点,ds_${0..1}.d_user_mobile_${0..1}
代表ds_0
和ds_1
数据源中的d_user_mobile_0
和d_user_mobile_1
4 张表。这种形式表示 ShardingSphere 会根据分片规则自动路由到这些表。databaseStrategy
:定义了如何将数据路由到不同的数据库(即ds_0
或ds_1
)。standard
表示标准分片策略,这里使用mobile
字段作为分片键,且使用名为databaseUserMobileHashModModel
的分片算法。tableStrategy
:定义了如何将数据路由到不同的表(即d_user_mobile_0
或d_user_mobile_1
)。与databaseStrategy
类似,tableStrategy
也使用mobile
字段作为分片键,使用tableUserMobileHashMod
算法。
其他表(如 d_user_email
、d_user
、d_ticket_user
)的分片配置类似,只是分片键不同(如 email
、id
、user_id
)。
2.2 分片算法部分
shardingAlgorithms:
databaseUserMobileHashModModel:
type: HASH_MOD
props:
sharding-count: 2
tableUserMobileHashMod:
type: HASH_MOD
props:
sharding-count: 2
作用:
shardingAlgorithms
定义了具体的分片算法。databaseUserMobileHashModModel
使用了HASH_MOD
算法(哈希取模)来对数据库进行分片,sharding-count: 2
表示会将数据分散到两个数据库(ds_0
和ds_1
)。tableUserMobileHashMod
同样使用HASH_MOD
算法来对表进行分片,sharding-count: 2
表示会将数据分散到两个表(如d_user_mobile_0
和d_user_mobile_1
)。- 对于其他表,也定义了类似的分片算法(如
databaseUserEmailHashModModel
、tableUserEmailHashMod
等)。
2.3 !ENCRYPT
数据加密部分
- !ENCRYPT
tables:
d_user:
columns:
mobile:
cipherColumn: mobile
encryptorName: user_encryption_algorithm
password:
cipherColumn: password
encryptorName: user_encryption_algorithm
id_number:
cipherColumn: id_number
encryptorName: user_encryption_algorithm
作用:
!ENCRYPT
表示启用了数据加密功能,用于对表的特定列进行加密。tables
中列出了需要加密的表和对应的字段。
字段解释:
cipherColumn
:指定实际存储加密数据的列名,这里直接与原列名相同,意味着加密后的数据会覆盖原数据。encryptorName
:引用了在encryptors
部分定义的加密算法,表示对该字段进行加密。
2.4 加密算法部分
encryptors:
user_encryption_algorithm:
type: SM4
props:
sm4-key: d3ecdaa11d6ab89e1987870186073eaa
sm4-mode: CBC
sm4-iv: 1afc7fdce9ebc393f693cd3d23e35ed2
sm4-padding: PKCS7Padding
作用:
encryptors
定义了具体的加密算法。user_encryption_algorithm
使用了 SM4 算法(中国国家密码标准),并使用了 CBC 模式。- 配置中的
sm4-key
是加密密钥,sm4-iv
是初始化向量(IV),sm4-padding
为填充方式(PKCS7Padding)。
这个加密算法会对 d_user
表中的 mobile
、password
、id_number
以及 d_user_mobile
表中的 mobile
字段进行加密,保证在数据库中存储的都是加密后的数据。
3. props
部分
props:
sql-show: true
作用:
sql-show: true
用于调试,开启后 ShardingSphere 会在日志中打印实际执行的 SQL 语句,方便开发人员查看分库分表后的 SQL 路由情况。
总结
这份 ShardingSphere 配置文件做了以下几件事:
- 定义了两个数据源(
ds_0
和ds_1
),用于实现数据库的分库。 - 使用了标准分片策略对表
d_user_mobile
、d_user_email
、d_user
、d_ticket_user
进行分库分表,确保数据能够分散到不同的数据库实例和表中。 - 定义了 HASH_MOD 和 MOD 两种分片算法,基于字段如
mobile
、email
、id
进行数据分片。 - 启用了加密功能,对特定表的敏感字段(如
mobile
、password
、id_number
)进行了加密,确保数据安全性。 - 配置了加密算法 SM4,并开启了 SQL 打印功能,方便调试
完整配置文件
dataSources:
# 第一个用户库
ds_0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/damai_user_0?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: xxx
password: xxx
# 第二个用户库
ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/damai_user_1?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: xxx
password: xxx
rules:
# 分库分表规则
- !SHARDING
tables:
# 对d_user_mobile表进行分库分表
d_user_mobile:
# 库为damai_user_0 damai_user_1 表为d_user_mobile_0 至 d_user_mobile_1
actualDataNodes: ds_${0..1}.d_user_mobile_${0..1}
# 分库策略
databaseStrategy:
standard:
# 使用mobile作为分片键
shardingColumn: mobile
# 用user_mobile列使用hash取模作为分库算法
shardingAlgorithmName: databaseUserMobileHashModModel
# 分表策略
tableStrategy:
standard:
# 使用mobile作为分片键
shardingColumn: mobile
# 用user_mobile列使用hash取模作为分表算法
shardingAlgorithmName: tableUserMobileHashMod
# 对d_user_email表进行分库分表
d_user_email:
# 库为damai_user_0 damai_user_1 表为d_user_email_0 至 d_user_email_1
actualDataNodes: ds_${0..1}.d_user_email_${0..1}
# 分库策略
databaseStrategy:
standard:
# 使用email作为分片键
shardingColumn: email
# 用user_mobile列使用hash取模作为分库算法
shardingAlgorithmName: databaseUserEmailHashModModel
# 分表策略
tableStrategy:
standard:
# 使用email作为分片键
shardingColumn: email
# 用user_mobile列使用hash取模作为分表算法
shardingAlgorithmName: tableUserEmailHashMod
# 对d_user表进行分库分表
d_user:
# 库为damai_user_0 damai_user_1 表为d_user_0 至 d_user_1
actualDataNodes: ds_${0..1}.d_user_${0..1}
# 分库策略
databaseStrategy:
standard:
# 使用id作为分片键
shardingColumn: id
# 用user_mobile列使用hash取模作为分库算法
shardingAlgorithmName: databaseUserModModel
# 分表策略
tableStrategy:
standard:
# 使用id作为分片键
shardingColumn: id
# 用user_mobile列使用hash取模作为分表算法
shardingAlgorithmName: tableUserModModel
# 对d_ticket_user表进行分库分表
d_ticket_user:
# 库为damai_user_0 damai_user_1 表为d_ticket_user_0 至 d_ticket_user_1
actualDataNodes: ds_${0..1}.d_ticket_user_${0..1}
# 分库策略
databaseStrategy:
standard:
# 使用user_id作为分片键
shardingColumn: user_id
# 用user_id列使用hash取模作为分库算法
shardingAlgorithmName: databaseTicketUserModModel
# 分表策略
tableStrategy:
standard:
# 使用user_id作为分片键
shardingColumn: user_id
# 用user_id列使用hash取模作为分表算法
shardingAlgorithmName: tableTicketUserModModel
# 具体的算法
shardingAlgorithms:
# d_user_mobile表分库算法
databaseUserMobileHashModModel:
type: HASH_MOD
props:
# 分库数量
sharding-count: 2
# d_user_mobile表分表算法
tableUserMobileHashMod:
type: HASH_MOD
props:
# 分表数量
sharding-count: 2
# d_user_email表分库算法
databaseUserEmailHashModModel:
type: HASH_MOD
props:
# 分库数量
sharding-count: 2
# d_user_email表分表算法
tableUserEmailHashMod:
type: HASH_MOD
props:
# 分表数量
sharding-count: 2
# d_user表分库算法
databaseUserModModel:
type: MOD
props:
# 分库数量
sharding-count: 2
# d_user表分表算法
tableUserModModel:
type: MOD
props:
# 分表数量
sharding-count: 2
# d_ticket_user表分库算法
databaseTicketUserModModel:
type: MOD
props:
# 分库数量
sharding-count: 2
# d_ticket_user表分表算法
tableTicketUserModModel:
type: MOD
props:
# 分表数量
sharding-count: 2
# 加密规则
- !ENCRYPT
tables:
# d_user表
d_user:
columns:
# 对mobile列进行加密
mobile:
# 密文列mobile
cipherColumn: mobile
# 自定义的加密算法
encryptorName: user_encryption_algorithm
# 对password列进行加密
password:
# 密文列password
cipherColumn: password
# 自定义的加密算法
encryptorName: user_encryption_algorithm
# 对id_number列进行加密
id_number:
# 密文列id_number
cipherColumn: id_number
# 自定义的加密算法
encryptorName: user_encryption_algorithm
# d_user_mobile表
d_user_mobile:
columns:
# 对mobile列进行加密
mobile:
# 密文列id_number
cipherColumn: mobile
# 自定义的加密算法
encryptorName: user_encryption_algorithm
props:
# 打印真实sql
sql-show: true