shardingSphere broadcast table settings

broadcast table

Refers to the tables that exist in all sharded data sources, and the table structure and its data are completely consistent in each database. It is suitable for scenarios where the amount of data is not large and needs to be associated with tables of massive data, such as dictionary tables.

  • To use the broadcast table, the following conditions must be met:
	1. 数据量不大
	2. 每个数据源中的数据保持一致
  • configuration
spring.shardingsphere.datasource.names=ds0,ds1

#分库0
spring.shardingsphere.datasource.ds0.name=mysql
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jmysql://localhost:3306/dev0?characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=123456

#分库1
spring.shardingsphere.datasource.ds1.name=mysql
spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/dev1?characterEncoding=utf-8&useSSL=false
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=123456

#t_org 表
spring.shardingsphere.rules.sharding.tables.t_org.actual-data-nodes=ds$->{
    
    0..1}.t_org
spring.shardingsphere.rules.sharding.tables.t_org.database-strategy.standard.sharding-column=com_id
spring.shardingsphere.rules.sharding.tables.t_org.database-strategy.standard.sharding-algorithm-name=preciseShardingDatabaseAlgorithm
#广播表
spring.shardingsphere.rules.sharding.broadcast-tables=t_org
  • It is enough to encode sql normally, and shardingSphere will automatically help us drop the library broadcast table.

  • verify

2023-02-02 16:43:47.328  INFO 46604 --- [main] ShardingSphere-SQL: Logic SQL: insert into t_org (com_id,organization_name) VALUE('11','测试广播')
2023-02-02 16:43:47.329  INFO 46604 --- [main] ShardingSphere-SQL: SQLStatement: MySQLInsertStatement(setAssignment=Optional.empty, onDuplicateKeyColumns=Optional.empty)
2023-02-02 16:43:47.329  INFO 46604 --- [main] ShardingSphere-SQL: Actual SQL: ds0 ::: insert into t_org (com_id,organization_name) VALUE('11', '测试广播')
2023-02-02 16:43:47.329  INFO 46604 --- [main] ShardingSphere-SQL: Actual SQL: ds1 ::: insert into t_org (com_id,organization_name) VALUE('11', '测试广播')
2023-02-02 16:43:47.371 DEBUG 46604 --- [main] c.s.r.mapper.GoodsStoreMapper.insertOrg  : <==    Updates: 1

Check the log and find: ds0 and ds1 are dropped .

Guess you like

Origin blog.csdn.net/qq_40319804/article/details/128852888