ShardingJdbc:集成ShardingSphere后,像MasterDataTable这样不需要分库分表,该怎么实现的一个方案
如题
采用了shardingSphere后,分库策略,分表策略都可以灵活使用了。
反过来会遇到一个新的问题,比如有个表不需要分库,也不需要分表,那么该怎么处理呢?
这里就要ShardingStrategy中的none配置了。
下面把配置贴出来:见配置中红色字体。
关键点:
default-data-source-name设置为主库数据源
actual-nodes:设置主库数据源和物理表名
database-strategy:设置none
table-strategy:设置none
sharding: jdbc: datasource: names: master0,master0salve0,master0slave1,master1,master1slave0,master1slave1 master0: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding0?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master0salve0: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding0s0?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master0slave1: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding0s1?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master1: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding1?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master1slave0: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding1s0?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root master1slave1: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/mcspcsharding1s1?useUnicode=true&character_set_server=utf8mb4&useSSL=false&serverTimezone=GMT%2B8 username: root password: root config: sharding: tables: mc_member: actual-nodes: mcspcsharding$->{0..1}.mc_member$->{0..1} database-strategy: standard: sharding-column: gender precise-algorithm-class-name: com.chong.mcspcshardingdbtable.sharding.DbShardingAlgorithm table-strategy: complex: sharding-columns: id algorithm-class-name: com.chong.mcspcshardingdbtable.sharding.MemberTblComplexKeySharding mc_master: # 主数据放置表,无需分库分表 actual-nodes: mcspcsharding0.mc_master # 指定主库中唯一的表节点 database-strategy: none: # 分片策略为none sharding-column: none # 分片键为none table-strategy: none: # 分片策略为none sharding-column: none # 分片键为none binding-tables: mc_member,mc_master default-data-source-name: master0 # 库分片策略为none时一定要设置默认数据源,否则应用无法启动 master-slave-rules: ms0: master-data-source-name: master0 slave-data-source-names: master0salve0,master0slave1 ms1: master-data-source-name: master1 slave-data-source-names: master1slave0,master1slave1 props: sql: show: true