dataSources: # 省略数据源配置,请参考使用手册
rules:
- !SHARDING
tables: # 数据分片规则配置
<logic-table-name> (+): # 逻辑表名称
actualDataNodes (?): # 由数据源名 + 表名组成(参考Inline语法规则)
databaseStrategy (?): # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
standard: # 用于单分片键的标准分片场景
shardingColumn: # 分片列名称
shardingAlgorithmName: # 分片算法名称
complex: # 用于多分片键的复合分片场景
shardingColumns: #分片列名称,多个列以逗号分隔
shardingAlgorithmName: # 分片算法名称
hint: # Hint 分片策略
shardingAlgorithmName: # 分片算法名称
none: # 不分片
tableStrategy: # 分表策略,同分库策略
keyGenerateStrategy: # 分布式序列策略
column: # 自增列名称,缺省表示不使用自增主键生成器
keyGeneratorName: # 分布式序列算法名称
autoTables: # 自动分片表规则配置
t_order_auto: # 逻辑表名称
actualDataSources (?): # 数据源名称
shardingStrategy: # 切分策略
standard: # 用于单分片键的标准分片场景
shardingColumn: # 分片列名称
shardingAlgorithmName: # 自动分片算法名称
bindingTables (+): # 绑定表规则列表
- <logic_table_name_1, logic_table_name_2, ...>
- <logic_table_name_1, logic_table_name_2, ...>
broadcastTables (+): # 广播表规则列表
- <table-name>
- <table-name>
defaultDatabaseStrategy: # 默认数据库分片策略
defaultTableStrategy: # 默认表分片策略
defaultKeyGenerateStrategy: # 默认的分布式序列策略
# 分片算法配置
shardingAlgorithms:
<sharding-algorithm-name> (+): # 分片算法名称
type: # 分片算法类型
props: # 分片算法属性配置
# ...
# 分布式序列算法配置
keyGenerators:
<key-generate-algorithm-name> (+): # 分布式序列算法名称
type: # 分布式序列算法类型
props: # 分布式序列算法属性配置
# ...
props:
# ...
dataSources: # 省略数据源配置,请参考使用手册
rules:
- !REPLICA_QUERY
dataSources:
<data-source-name> (+): # 读写分离逻辑数据源名称
primaryDataSourceName: # 主库数据源名称
replicaDataSourceNames:
- <replica-data_source-name> (+) # 从库数据源名称
loadBalancerName: # 负载均衡算法名称
# 负载均衡算法配置
loadBalancers:
<load-balancer-name> (+): # 负载均衡算法名称
type: # 负载均衡算法类型
props: # 负载均衡算法属性配置
# ...
props:
# ...
算法类型的详情,请参见内置负载均衡算法列表。
dataSource: # 省略数据源配置,请参考使用手册
rules:
- !ENCRYPT
tables:
<table-name> (+): # 加密表名称
columns:
<column-name> (+): # 加密列名称
cipherColumn: # 密文列名称
assistedQueryColumn (?): # 查询辅助列名称
encryptorName: # 加密算法名称
# 加密算法配置
encryptors:
<encrypt-algorithm-name> (+): # 加解密算法名称
type: # 加解密算法类型
props: # 加解密算法属性配置
# ...
算法类型的详情,请参见内置加密算法列表。
dataSources: # 省略数据源配置,请参考使用手册
rules:
- !SHADOW
column: # 影子字段名
sourceDataSourceNames: # 影子前数据库名
# ...
shadowDataSourceNames: # 对应的影子库名
# ...
props:
# ...
governance:
name: # 治理名称
registryCenter: # 注册中心
type: # 治理持久化类型。如:Zookeeper, etcd
serverLists: # 治理服务列表。包括 IP 地址和端口号。多个地址用逗号分隔。如: host1:2181,host2:2181
overwrite: # 本地配置是否覆盖配置中心配置。如果可覆盖,每次启动都以本地配置为准
dataSources:
ds0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds0
username: root
password:
ds1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds1
username: root
password:
shardingRule:
tables:
t_order:
actualDataNodes: ds${0..1}.t_order${0..1}
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds${user_id % 2}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order${order_id % 2}
keyGenerator:
type: SNOWFLAKE
column: order_id
t_order_item:
actualDataNodes: ds${0..1}.t_order_item${0..1}
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds${user_id % 2}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_item${order_id % 2}
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_config
defaultDataSourceName: ds0
defaultTableStrategy:
none:
defaultKeyGenerator:
type: SNOWFLAKE
column: order_id
props:
sql.show: true
dataSources:
ds_master: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_master
username: root
password:
ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_slave0
username: root
password:
ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_slave1
username: root
password:
masterSlaveRule:
name: ds_ms
masterDataSourceName: ds_master
slaveDataSourceNames: [ds_slave0, ds_slave1]
props:
sql.show: true
通过 YamlMasterSlaveDataSourceFactory
工厂类创建 DataSource
:
DataSource dataSource = YamlMasterSlaveDataSourceFactory.createDataSource(yamlFile);
dataSource: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/encrypt?serverTimezone=UTC&useSSL=false
username: root
password:
encryptRule:
encryptors:
encryptor_aes:
type: aes
props:
aes.key.value: 123456abc
encryptor_md5:
type: md5
tables:
t_encrypt:
columns:
user_id:
cipherColumn: user_cipher
encryptor: encryptor_aes
order_id:
cipherColumn: order_cipher
encryptor: encryptor_md5
props:
query.with.cipher.column: true # 是否使用密文列查询
# 省略数据分片、读写分离和数据脱敏配置
orchestration:
name: orchestration_ds
overwrite: true
registry:
type: zookeeper
namespace: orchestration
serverLists: localhost:2181
dataSources:
ds0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds0
username: root
password:
ds1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds1
username: root
password:
shardingRule:
tables:
t_order:
actualDataNodes: ds${0..1}.t_order${0..1}
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds${user_id % 2}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order${order_id % 2}
keyGeneratorColumnName: order_id
t_order_item:
actualDataNodes: ds${0..1}.t_order_item${0..1}
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds${user_id % 2}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_item${order_id % 2}
bindingTables:
- t_order,t_order_item
broadcastTables:
- t_config
defaultDataSourceName: ds0
defaultTableStrategy:
none:
defaultKeyGeneratorClassName: io.shardingsphere.core.keygen.DefaultKeyGenerator
props:
sql.show: true
dataSources:
ds_master: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_master
username: root
password:
ds_slave0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_slave0
username: root
password:
ds_slave1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_slave1
username: root
password:
masterSlaveRule:
name: ds_ms
masterDataSourceName: ds_master
slaveDataSourceNames: [ds_slave0, ds_slave1]
props:
sql.show: true
configMap:
key1: value1
通过 MasterSlaveDataSourceFactory
工厂类创建 DataSource
:
DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(yamlFile);
# 省略数据分片和读写分离配置
orchestration:
name: orchestration_ds
overwrite: true
registry:
namespace: orchestration
serverLists: localhost:2181
dataSources:
db0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100
db1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100
shardingRule:
tables:
config:
actualDataNodes: db${0..1}.t_config
t_order:
actualDataNodes: db${0..1}.t_order_${0..1}
databaseStrategy:
standard:
shardingColumn: user_id
preciseAlgorithmClassName: io.shardingjdbc.core.yaml.fixture.SingleAlgorithm
tableStrategy:
inline:
shardingColumn: order_id
algorithmInlineExpression: t_order_${order_id % 2}
keyGeneratorColumnName: order_id
keyGeneratorClass: io.shardingjdbc.core.yaml.fixture.IncrementKeyGenerator
t_order_item:
actualDataNodes: db${0..1}.t_order_item_${0..1}
# 绑定表中其余的表的策略与第一张表的策略相同
databaseStrategy:
standard:
shardingColumn: user_id
preciseAlgorithmClassName: io.shardingjdbc.core.yaml.fixture.SingleAlgorithm
tableStrategy:
inline:
shardingColumn: order_id
algorithmInlineExpression: t_order_item_${order_id % 2}
bindingTables:
- t_order,t_order_item
# 默认数据库分片策略
defaultDatabaseStrategy:
none:
defaultTableStrategy:
complex:
shardingColumns: id, order_id
algorithmClassName: io.shardingjdbc.core.yaml.fixture.MultiAlgorithm
props:
sql.show: true
为了缓解数据库压力,将写入和读取操作分离为不同数据源,写库称为主库,读库称为从库,一主库可配置多从库。
dataSources:
db_master: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db_master;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100
db_slave_0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100
db_slave_1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db_slave_1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
maxActive: 100
masterSlaveRule:
name: db_ms
masterDataSourceName: db_master
slaveDataSourceNames: [db_slave_0, db_slave_1]
configMap:
key1: value1
通过 MasterSlaveDataSourceFactory
工厂类创建 DataSource
:
DataSource dataSource = MasterSlaveDataSourceFactory.createDataSource(yamlFile);
Zookeeper分库分表编排配置项说明
dataSources: 数据源配置
shardingRule: 分片规则配置
orchestration: Zookeeper编排配置
name: 编排服务节点名称
overwrite: 本地配置是否可覆盖注册中心配置。如果可覆盖,每次启动都以本地配置为准
zookeeper: Zookeeper注册中心配置
namespace: Zookeeper的命名空间
serverLists: 连接Zookeeper服务器的列表。包括IP地址和端口号。多个地址用逗号分隔。如: host1:2181,host2:2181
baseSleepTimeMilliseconds: 等待重试的间隔时间的初始值。单位:毫秒
maxSleepTimeMilliseconds: 等待重试的间隔时间的最大值。单位:毫秒
maxRetries: 最大重试次数
sessionTimeoutMilliseconds: 会话超时时间。单位:毫秒
connectionTimeoutMilliseconds: 连接超时时间。单位:毫秒
digest: 连接Zookeeper的权限令牌。缺省为不需要权限验证
Etcd分库分表编排配置项说明
dataSources: 数据源配置
shardingRule: 分片规则配置
orchestration: Etcd编排配置
name: 编排服务节点名称
overwrite: 本地配置是否可覆盖注册中心配置。如果可覆盖,每次启动都以本地配置为准
etcd: Etcd注册中心配置
serverLists: 连接Etcd服务器的列表。包括IP地址和端口号。多个地址用逗号分隔。如: http://host1:2379,http://host2:2379
timeToLiveSeconds: 临时节点存活时间。单位:秒
timeoutMilliseconds: 每次请求的超时时间。单位:毫秒
maxRetries: 每次请求的最大重试次数
retryIntervalMilliseconds: 重试间隔时间。单位:毫秒
分库分表编排数据源构建方式
DataSource dataSource = OrchestrationShardingDataSourceFactory.createDataSource(yamlFile);
读写分离数据源构建方式
DataSource dataSource = OrchestrationMasterSlaveDataSourceFactory.createDataSource(yamlFile);