1、定义数据源
#定义数据源
# 数据源 ds0 第一个数据库
shardingsphere:
datasource:
#数据源名称
names: ds0
#第一个库
ds0:
connectionTimeoutMilliseconds: 30000
driver-class-name: com.mysql.cj.jdbc.Driver
idleTimeoutMilliseconds: 60000
jdbc-url: jdbc:mysql://xxxx:3306/xxx?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
maintenanceIntervalMilliseconds: 30000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 50
password: xxxx
type: com.zaxxer.hikari.HikariDataSource
username: root
2、#分库策略
#分库策略
sharding:
tables:
product_order:#表名称
#分库
database-strategy:
inline:
sharding-column: account_no #分库对应的键
algorithm-expression: ds$->{account_no % 2} #分库的策略
3、分表策略
#分表
actual-data-nodes: ds0.product_order_$->{0..1} #指定表的分布情况
#水平分表策略+行表达式分片
table-strategy:
inline:
sharding-column: account_no #分库对应的键
algorithm-expression: product_order_$->{ account_no % 2 } #分表的策略
4、id生成策略
#id生成策略
key-generator:
column: id
props:
worker:
id: ${workId}#需要不同的workId,不然Id会重读
#id生成策略
type: SNOWFLAKE
5、如果有的表不需要分库,配置默认的数据源
#----------配置默认数据库,⽐如用户,不分库分表--------
default-data-source-name: ds0