sharding-proxy利用字符串hash取模的方式分表
如果是数值类型的字段,可以直接取模来分表,如果是字符串,往往需要先求出hash值再取模
修改 config-sharding.yaml
schemaName: activity
dataSources:
ds_1:
url: jdbc:mysql://192.168.0.1:3306/activity?serverTimezone=UTC&useSSL=false
username: root
password: 123456
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
rules:
- !SHARDING
tables:
plat_activity_product_detail:
actualDataNodes: ds_${1}.plat_activity_product_detail_${0..199}
tableStrategy:
standard:
shardingColumn: activity_id
shardingAlgorithmName: plat_activity_product_mod
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
shardingAlgorithms:
plat_activity_product_mod:
type: HASH_MOD
props:
sharding-count: '200'
keyGenerators:
snowflake:
type: SNOWFLAKE
props:
worker-id: 123
配置一个模式命名为:plat_activity_product_mod,类型指定为hash取模:HASH_MOD,props:sharding-count: '200’意为分200张表。
tableStrategy:shardingAlgorithmName下配置自定义的模式名plat_activity_product_mod
此时sharding会按照activity_id字段hash取模,分配到分表中