使用 Java API

引入 Maven 依赖

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>shardingsphere-jdbc-core</artifactId>
  4. <version>${shardingsphere.version}</version>
  5. </dependency>

规则配置

ShardingSphere-JDBC 的 Java API 通过数据源集合、规则集合以及属性配置组成。 以下示例是根据 user_id 取模分库, 且根据 order_id 取模分表的 2 库 2 表的配置。

注:示例的数据库连接池为HikariCP,可根据业务场景更换为其他主流数据库连接池。

  1. // 配置真实数据源
  2. Map<String, DataSource> dataSourceMap = new HashMap<>();
  3. // 配置第 1 个数据源
  4. HikariDataSource dataSource1 = new HikariDataSource();
  5. dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
  6. dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/ds0");
  7. dataSource1.setUsername("root");
  8. dataSource1.setPassword("");
  9. dataSourceMap.put("ds0", dataSource1);
  10. // 配置第 2 个数据源
  11. HikariDataSource dataSource2 = new HikariDataSource();
  12. dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
  13. dataSource2.setJdbcUrl("jdbc:mysql://localhost:3306/ds1");
  14. dataSource2.setUsername("root");
  15. dataSource2.setPassword("");
  16. dataSourceMap.put("ds1", dataSource2);
  17. // 配置 t_order 表规则
  18. ShardingTableRuleConfiguration orderTableRuleConfig = new ShardingTableRuleConfiguration("t_order", "ds${0..1}.t_order${0..1}");
  19. // 配置分库策略
  20. orderTableRuleConfig.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "dbShardingAlgorithm"));
  21. // 配置分表策略
  22. orderTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "tableShardingAlgorithm"));
  23. // 省略配置 t_order_item 表规则...
  24. // ...
  25. // 配置分片规则
  26. ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
  27. shardingRuleConfig.getTables().add(orderTableRuleConfig);
  28. // 配置分库算法
  29. Properties dbShardingAlgorithmrProps = new Properties();
  30. dbShardingAlgorithmrProps.setProperty("algorithm-expression", "ds${user_id % 2}");
  31. shardingRuleConfig.getShardingAlgorithms().put("dbShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE", dbShardingAlgorithmrProps));
  32. // 配置分表算法
  33. Properties tableShardingAlgorithmrProps = new Properties();
  34. tableShardingAlgorithmrProps.setProperty("algorithm-expression", "t_order${order_id % 2}");
  35. shardingRuleConfig.getShardingAlgorithms().put("tableShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE", tableShardingAlgorithmrProps));
  36. // 创建 ShardingSphereDataSource
  37. DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(shardingRuleConfig), new Properties());

使用 ShardingSphereDataSource

通过 ShardingSphereDataSourceFactory 工厂创建的 ShardingSphereDataSource 实现自 JDBC 的标准接口 DataSource。 可通过 DataSource 选择使用原生 JDBC,或JPA, MyBatis 等 ORM 框架。

以原生 JDBC 使用方式为例:

  1. DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(shardingRuleConfig), new Properties());
  2. String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
  3. try (
  4. Connection conn = dataSource.getConnection();
  5. PreparedStatement ps = conn.prepareStatement(sql)) {
  6. ps.setInt(1, 10);
  7. ps.setInt(2, 1000);
  8. try (ResultSet rs = ps.executeQuery()) {
  9. while(rs.next()) {
  10. // ...
  11. }
  12. }
  13. }