方便更快捷的说明问题,可以按需填写(可删除)
使用环境:
spring boot 2.1.5
shardingSphere 5.1.0
mybatis-plus 3.5.1
场景、问题:
目前有三个数据库 shard_one,shard_two,shard_three 三个库。每个库都分别由 sys_user_0, sys_user_1, sys_user2 三个表。
我期望新增的时候,可以平均新增到3个库的9个表内。可是目前新增结果是插入到了 shard_one.sys_user0,shard_one.sys_user1,shard_one.sys_user2
已进行操作:
# 基于mybatis-plus的多数据源
datasource:
dynamic:
datasource:
# 数仓数据源
datawarehouse:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/zt_test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
# 添加其他数据源
# test:
# type: com.alibaba.druid.pool.DruidDataSource
# driver-class-name: com.mysql.jdbc.Driver
# url: jdbc:mysql://localhost:3306/zt_test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
# username: root
# password: 123456
# 指定默认数据源名称 此处指定 shardingSphere 为默认数据源。
primary: shardingSphere
# 基于 sharding sphere 进行分表
shardingsphere:
mode:
type: Standalone
repository:
type: File
overwrite: true
datasource:
# 连接名称(下面要用这个名称来区分库)
names: ds0,ds1,ds2
ds0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shard_one?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
ds1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shard_two?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
ds2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shard_three?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
# 是否开启 SQL 显示,默认值: false
props:
sql-show: true
# 数据分片策略
rules:
sharding:
# 未配置分片规则的表将通过默认数据源定位
default-data-source-name: ds0,ds1,ds2
# 按表来区分
tables:
# 表名
sys_user:
# 配置数据节点 由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持 inline 表达式。
# 缺省表示使用已知数据源与逻辑表名称生成数据节点,
# 用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
# actual-data-nodes: ds$->{0..2}.sys_user_$->{0..2}
actual-data-nodes: ds0.sys_user_$->{0..2},ds1.sys_user_$->{0..2},ds1.sys_user_$->{0..2}
# 分库策略
# databaseStrategy:
# 分库的规则 用id这个字段来分库 总共有三个库 ds0(sys_user)与ds1(sys_user)
# shardingColumn: id
# algorithmExpression: ds${id % 3}
# 分表策略
table-strategy:
standard:
sharding-column: id
sharding-algorithm-name: sys-user-inline
key-generate-strategy:
column: id
key-generator-name: snowflake
sharding-algorithms:
database-inline:
type: INLINE
props:
# algorithm-expression: ds$->{id % 3}
algorithm-expression: ds$->{id % 3}
allow-range-query-with-inline-sharding: true
sys-user-inline:
type: INLINE
props:
# 分片算法行表达式,需符合groovy语法 '& Integer.MAX_VALUE' 位运算使hash值为正数
algorithm-expression: sys_user_$->{id % 3}
default-database-strategy:
standard:
sharding-column: id
sharding-algorithm-name: database-inline
# 分布式序列算法配置
key-generators:
# 分布式序列列名称
snowflake:
# 分布式序列算法类型
type: SNOWFLAKE
现状:
目前有三个数据库 shard_one,shard_two,shard_three 三个库。每个库都分别由 sys_user_0, sys_user_1, sys_user2 三个表。
我期望新增的时候,可以平均新增到3个库的9个表内。可是目前新增结果是插入到了 shard_one.sys_user0,shard_one.sys_user1,shard_one.sys_user2