shardingsphere autoTables 不能使用auto算法的巨坑

228 阅读1分钟
  • 当你设置了下面的内容,启动shardingsphere的时候,会提示你无法使用auto算法
autoTables:
  tb_order:
    actualDataSources: ds_${0..1}
    shardingStrategy:
      standard:
        shardingColumn: id
        shardingAlgorithmName: mod
shardingAlgorithms:
  mod:
    #指定算法的类型,这个很重要,去官方文档看
    type: MOD
    props:
      sharding-count: 2
  • 这是因为autoTables字面意思就是自动表,是针对表的,当我们指定了datasources之后,此时它会自动识别sharding-count和actualDataSources
  • 上面actual是ds_${0..1},sharding-count为2,那么它就能匹配到下面几种可能,也就意味着我们的数据库表名也要变成mytable_0,之所以无法使用auto算法,就是我们的实际数据库的表名没有加后面的后缀
  ds_0.mytable_0  ds_1.mytable_1
  • 而且 sharding-count应该改为4
sharding-count: 4

//如果改为了4,那么shard的匹配规则就是下面
ds_0.mytable_0  ds_0.mytable_1   ds_1.mytable_0  ds_1.mytable_1

//那么ds0数据库里应该存储mytable_0,mytable_2表,shard 的规则就是这样
//而ds1数据库应该存储mytable_1,mytable_3表

  • 当我们使用shard去创建表的时候,也是如此,创建mytable_3就会放到数据库ds_1里