Java充电社
专辑
博文
联系我
本人继续续收门徒,亲手指导
ShardingSphere第8篇:广播表
相关专辑:
分库分表ShardingSphere
<div style="display:none"></div> ## 8.1、是什么? 有时候,某些表需要在所有库中都有一个,且数据是一样的,比如字典表,这种表,插入表,所有表都会写入数据,而查询时,选择一个就可以了,这种场景需要用到shardingsphere中的广播表。 ## 8.2、如何使用? 需要广播的表,需要调用`shardingRuleConfig.setBroadcastTable`进行设置。 代码如下,广播的表,插入数据会同时落到所有的库,查询只会落到一个库。 ```java ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); // 这里需要指定t_dict为广播模式 shardingRuleConfig.setBroadcastTables(Arrays.asList("t_dict")); ``` ## 8.3、示例 ### 1)需求 准备2个db:sj_ds0,sj_ds1 2个db中都包含字典表:t_dict 要求向t_dict写入数据的时候,将数据同时写2个库中的t_dict表,查询的时候随便选择一个即可 ### 2)sql脚本 ```java drop database if exists sj_ds0; create database sj_ds0; use sj_ds0; drop table if exists t_dict; create table t_dict( id bigint not null primary key auto_increment, code varchar(64), k varchar(64) not null, v varchar(16) not null ); drop database if exists sj_ds1; create database sj_ds1; use sj_ds1; drop table if exists t_dict; create table t_dict( id bigint not null primary key auto_increment, code varchar(64), k varchar(64) not null, v varchar(16) not null ); ``` ### 3)java代码 ```java import com.zaxxer.hikari.HikariDataSource; import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration; import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory; import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationPropertyKey; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Arrays; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; public class Demo5 { public static void main(String[] args) throws SQLException { /** * 1.配置真实数据源 */ Map<String, DataSource> dataSourceMap = new LinkedHashMap<>(); dataSourceMap.put("ds0", dataSource1()); dataSourceMap.put("ds1", dataSource2()); /** * 2、无分片规则 */ ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); // 这里需要指定t_dict为广播模式 shardingRuleConfig.setBroadcastTables(Arrays.asList("t_dict")); /** * 4、配置一些属性 */ Properties props = new Properties(); //输出sql props.put(ConfigurationPropertyKey.SQL_SHOW.getKey(), true); /** * 5、创建数据源 */ props.put(ConfigurationPropertyKey.SQL_SHOW.getKey(), true); DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, props); Connection connection = dataSource.getConnection(); System.out.println("测试插入数据:"); String sql = "insert into t_dict (code,k,v) values ('gender','男','1'),('gender','女','2')"; PreparedStatement ps = connection.prepareStatement(sql); System.out.println("插入记录数:" + ps.executeUpdate()); System.out.println("测试查询数据:"); ps = connection.prepareStatement("select count(*) from t_dict"); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println("count:" + rs.getInt(1)); } ps.close(); connection.close(); } private static DataSource dataSource1() { HikariDataSource dataSource1 = new HikariDataSource(); dataSource1.setDriverClassName("com.mysql.jdbc.Driver"); dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/sj_ds0?characterEncoding=UTF-8"); dataSource1.setUsername("root"); dataSource1.setPassword("root123"); return dataSource1; } private static DataSource dataSource2() { HikariDataSource dataSource1 = new HikariDataSource(); dataSource1.setDriverClassName("com.mysql.jdbc.Driver"); dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/sj_ds1?characterEncoding=UTF-8"); dataSource1.setUsername("root"); dataSource1.setPassword("root123"); return dataSource1; } } ``` ### 4)运行输出 > 广播的表,插入数据会同时落到所有的库,查询只会落到一个库 ```java 测试插入数据: Logic SQL: insert into t_dict (code,k,v) values ('gender','男','1'),('gender','女','2') Actual SQL: ds0 ::: insert into t_dict (code,k,v) values ('gender', '男', '1'), ('gender', '女', '2') Actual SQL: ds1 ::: insert into t_dict (code,k,v) values ('gender', '男', '1'), ('gender', '女', '2') 插入记录数:2 测试查询数据: Logic SQL: select count(*) from t_dict Actual SQL: ds0 ::: select count(*) from t_dict count:2 ``` ## 8.4、本章案例完整代码 ```java https://gitee.com/javacode2018/shardingsphere-demo/blob/master/java-api-demo/src/test/java/com/itsoku/javaapidemo/Demo5.java ``` <a style="display:none" target="_blank" href="https://mp.weixin.qq.com/s/_S1DD2JADnXvpexxaBwLLg" style="color:red; font-size:20px; font-weight:bold">继续收门徒,亲手带,月薪 4W 以下的可以来找我</a> ## 最新资料 1. <a href="https://mp.weixin.qq.com/s?__biz=MzkzOTI3Nzc0Mg==&mid=2247484964&idx=2&sn=c81bce2f26015ee0f9632ddc6c67df03&scene=21#wechat_redirect" target="_blank">尚硅谷 Java 学科全套教程(总 207.77GB)</a> 2. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484192&idx=1&sn=505f2faaa4cc911f553850667749bcbb&scene=21#wechat_redirect" target="_blank">2021 最新版 Java 微服务学习线路图 + 视频</a> 3. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484573&idx=1&sn=7f3d83892186c16c57bc0b99f03f1ffd&scene=21#wechat_redirect" target="_blank">阿里技术大佬整理的《Spring 学习笔记.pdf》</a> 4. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484544&idx=2&sn=c1dfe907cfaa5b9ae8e66fc247ccbe84&scene=21#wechat_redirect" target="_blank">阿里大佬的《MySQL 学习笔记高清.pdf》</a> 5. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247485167&idx=1&sn=48d75c8e93e748235a3547f34921dfb7&scene=21#wechat_redirect" target="_blank">2021 版 java 高并发常见面试题汇总.pdf</a> 6. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247485664&idx=1&sn=435f9f515a8f881642820d7790ad20ce&scene=21#wechat_redirect" target="_blank">Idea 快捷键大全.pdf</a> ![](https://itsoku.oss-cn-hangzhou.aliyuncs.com/itsoku/blog/article/1/2883e86e-3eff-404a-8943-0066e5e2b454.png)
相关专辑:
分库分表ShardingSphere