4,项目分库分表实现-广播表配置

4、广播表配置

对于stock_business业务表作为广播表处理:

在application-sharding.properties配置其它数据源:  

spring.shardingsphere.datasource.names=defdb,ds-2021,ds-2022,ds-2023
spring.shardingsphere.datasource.defdb.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.defdb.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.defdb.url=jdbc:mysql://127.0.0.1:3306/stock_sys_db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.defdb.username=root
spring.shardingsphere.datasource.defdb.password=root123

spring.shardingsphere.datasource.ds-2021.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-2021.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2021.url=jdbc:mysql://127.0.0.1:3306/stock_db_2021?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds-2021.username=root
spring.shardingsphere.datasource.ds-2021.password=root123

spring.shardingsphere.datasource.ds-2022.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-2022.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2022.url=jdbc:mysql://127.0.0.1:3306/stock_db_2022?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds-2022.username=root
spring.shardingsphere.datasource.ds-2022.password=root123

spring.shardingsphere.datasource.ds-2023.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds-2023.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds-2023.url=jdbc:mysql://127.0.0.1:3306/stock_db_2023?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.ds-2023.username=root
spring.shardingsphere.datasource.ds-2023.password=root123

#配置默认数据源
spring.shardingsphere.sharding.default-data-source-name=defdb

#配置广播表
spring.shardingsphere.sharding.broadcast-tables=stock_business

#配置开启sql显示
spring.shardingsphere.props.sql.show=true

 测试广播表:

  @Autowired
    private StockBusinessMapper stockBusinessMapper;

    
    /**
     * @Description 测试广播表
     */
    @Test
    public void testBroadCast(){
        StockBusiness pojo = StockBusiness.builder().stockCode("90000")
                .stockName("900000")
                .blockLabel("900000")
                .blockName("900000")
                .business("900000")
                .updateTime(new Date())
                .build();
        stockBusinessMapper.insert(pojo);

//        stockBusinessMapper.deleteByPrimaryKey("90000");
    }

运行完查看控制台

2024-01-12 22:44:10.293  INFO 16356 --- [           main] ShardingSphere-SQL                       : Actual SQL: defdb ::: insert into stock_business
         (stock_code, stock_name, block_label, block_name, business, update_time) VALUES (?, ?, ?, ?, ?, ?) ::: [90000, 900000, 900000, 900000, 900000, 2024-01-12 22:44:08.952]
2024-01-12 22:44:10.294  INFO 16356 --- [           main] ShardingSphere-SQL                       : Actual SQL: ds-2021 ::: insert into stock_business
         (stock_code, stock_name, block_label, block_name, business, update_time) VALUES (?, ?, ?, ?, ?, ?) ::: [90000, 900000, 900000, 900000, 900000, 2024-01-12 22:44:08.952]
2024-01-12 22:44:10.296  INFO 16356 --- [           main] ShardingSphere-SQL                       : Actual SQL: ds-2022 ::: insert into stock_business
         (stock_code, stock_name, block_label, block_name, business, update_time) VALUES (?, ?, ?, ?, ?, ?) ::: [90000, 900000, 900000, 900000, 900000, 2024-01-12 22:44:08.952]
2024-01-12 22:44:10.308  INFO 16356 --- [           main] ShardingSphere-SQL                       : Actual SQL: ds-2023 ::: insert into stock_business
         (stock_code, stock_name, block_label, block_name, business, update_time) VALUES (?, ?, ?, ?, ?, ?) ::: [90000, 900000, 900000, 900000, 900000, 2024-01-12 22:44:08.952]
2024-01-12 22:44:10.640  INFO 16356 --- [ionShutdownHook] o.s.a.r.l.SimpleMessageListenerContainer : Waiting for workers to finish.
2024-01-12 22:44:11.070  INFO 16356 --- [ionShutdownHook] o.s.a.r.l.SimpleMessageListenerContainer : Successfully waited for workers to finish.

四张表都插入了数据

  • 13
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码的翠花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值