H__D  

  ShardingSphere-JDBC基本使用参考:【ShardingSphere】ShardingSphere-JDBC 快速入门

  主从数据库搭建参考:【Mysql】主从同步配置

读写分离

  参考官方文档:https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/java-api/readwrite-splitting/

  具体实现,代码如下:

 1 /**
 2  * 主从配置
 3  */
 4 public class ShardingMasterSlaveTest {
 5 
 6     public static void main(String[] args) throws SQLException {
 7         ShardingMasterSlaveTest.insert();
 8         ShardingMasterSlaveTest.select();
 9     }
10 
11     public static void insert() throws SQLException {
12         // 获取数据源
13         DataSource dataSource = getDataSource();
14 
15         String insertSql1 = "INSERT INTO user VALUES(1, '小白');";
16         String insertSql2 = "INSERT INTO user VALUES(2, '小黑');";
17         String insertSql3 = "INSERT INTO user VALUES(3, '小黄');";
18         String insertSql4 = "INSERT INTO user VALUES(4, '小蓝');";
19 
20         try (
21                 Connection conn = dataSource.getConnection();
22                 PreparedStatement ps = conn.prepareStatement(insertSql2)) {
23             int num = ps.executeUpdate();
24             System.out.println("num = " + num);
25         }
26 
27     }
28 
29 
30     public static void select() throws SQLException {
31         // 获取数据源
32         DataSource dataSource = getDataSource();
33 
34         String sql = "select * from user";
35 
36         try (
37                 Connection conn = dataSource.getConnection();
38                 PreparedStatement ps = conn.prepareStatement(sql)) {
39             try (ResultSet rs = ps.executeQuery()) {
40                 while(rs.next()) {
41                     int order_id = rs.getInt(1);
42                     String name = rs.getString(2);
43                     System.out.println(order_id + "\t" + name);
44                 }
45             }
46         }
47     }
48 
49     public static DataSource getDataSource() throws SQLException {
50         // 配置真实数据源
51         Map<String, DataSource> dataSourceMap = new HashMap<>();
52 
53         // 配置第 1 个数据源
54         HikariDataSource dataSource1 = new HikariDataSource();
55         dataSource1.setDriverClassName("com.mysql.cj.jdbc.Driver");
56         dataSource1.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test_master_slave?allowPublicKeyRetrieval=true&useSSL=true");
57         dataSource1.setUsername("root");
58         dataSource1.setPassword("123");
59         dataSourceMap.put("ds0", dataSource1);
60 
61         // 配置第 2 个数据源
62         HikariDataSource dataSource2 = new HikariDataSource();
63         dataSource2.setDriverClassName("com.mysql.cj.jdbc.Driver");
64         dataSource2.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test_master_slave?allowPublicKeyRetrieval=true&useSSL=true");
65         dataSource2.setUsername("root");
66         dataSource2.setPassword("123");
67         dataSourceMap.put("ds1", dataSource2);
68 
69 
70         // 读写数据源配置
71         ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfiguration = new ReadwriteSplittingDataSourceRuleConfiguration(
72                 "master_slave_db", null, "ds0", Arrays.asList("ds1"), "ROUND_ROBIN");
73 
74         // 读写分离配置
75         ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfiguration = new ReadwriteSplittingRuleConfiguration(
76                 Collections.singleton(dataSourceRuleConfiguration), new HashMap<>());
77 
78         // 创建 ShardingSphereDataSource
79         DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(readwriteSplittingRuleConfiguration), new Properties());
80 
81         return dataSource;
82     }

 

posted on 2021-06-25 18:23  H__D  阅读(396)  评论(0编辑  收藏  举报