1.简介

ShardingSphere-JDBC目前是5.0.0-alpha版本了,我这里以4.x版本演示其入门级的使用方法。此篇文章演示一个纯Java Api方式的使用Demo和整合SpringBoot的使用Demo。Demo是参考官方文档编写的。

2.基于Java Api方式的使用

ShardingSphere-JDBC的jar包版本很多,考虑到官方文档在写的时候也是基于 4.0.x版本写的文档,所以,我这里使用 4.0.1 版本(4.0版本的最后一个小升级版本)

1.依赖引入

  1. <sharding-sphere.version>4.0.1</sharding-sphere.version>
  2. <dependency>
  3. <groupId>org.apache.shardingsphere</groupId>
  4. <artifactId>sharding-jdbc-core</artifactId>
  5. <version>${sharding-sphere.version}</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>mysql</groupId>
  9. <artifactId>mysql-connector-java</artifactId>
  10. <version>5.1.39</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.zaxxer</groupId>
  14. <artifactId>HikariCP</artifactId>
  15. <version>3.4.5</version>
  16. </dependency>

2.数据库与表的准备
创建2个数据库ds0,ds1。在数据库中各创建2张表,创表语句如下:

  1. create table t_order(
  2. user_id int(11),
  3. order_id int(11) PRIMARY KEY
  4. );

最后创建的结果是这样:
image.png

3.编写代码

  1. package org.example;
  2. import com.zaxxer.hikari.HikariDataSource;
  3. import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
  4. import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
  5. import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
  6. import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
  7. import javax.sql.DataSource;
  8. import java.sql.Connection;
  9. import java.sql.PreparedStatement;
  10. import java.sql.SQLException;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.Properties;
  14. /**
  15. * Hello world!
  16. */
  17. public class App {
  18. public static void main(String[] args) throws SQLException {
  19. // 配置真实数据源
  20. Map<String, DataSource> dataSourceMap = new HashMap<>();
  21. // 配置第一个数据源
  22. HikariDataSource dataSource1= new HikariDataSource();
  23. dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
  24. dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/ds0");
  25. dataSource1.setUsername("root");
  26. dataSource1.setPassword("888888");
  27. dataSourceMap.put("ds0", dataSource1);
  28. // 配置第二个数据源
  29. HikariDataSource dataSource2 = new HikariDataSource();
  30. dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
  31. dataSource2.setJdbcUrl("jdbc:mysql://localhost:3306/ds1");
  32. dataSource2.setUsername("root");
  33. dataSource2.setPassword("888888");
  34. dataSourceMap.put("ds1", dataSource2);
  35. // 配置Order表规则
  36. TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("t_order","ds${0..1}.t_order${0..1}");
  37. // 配置分库 + 分表策略
  38. orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
  39. orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order${order_id % 2}"));
  40. // 配置分片规则
  41. ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
  42. shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
  43. // 省略配置order_item表规则...
  44. // ...
  45. // 获取数据源对象
  46. DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties());
  47. Connection connection = dataSource.getConnection();
  48. PreparedStatement preparedStatement = connection.prepareStatement("insert into t_order values(1,1),(1,2),(2,1),(2,2)");
  49. int result = preparedStatement.executeUpdate();
  50. System.out.println(result);
  51. }
  52. }

4.执行结果
上面的配置是:按照userId进行分库,orderId分表。
userId%2 ==0则数据进入ds0,否则进入ds1
之后,orderId%2==0则进入t_order0,否则进入t_order1

3.