快速入门

使用API配置

Maven安装

  1. <!-- 引入sharding-jdbc核心模块 -->
  2. <dependency>
  3. <groupId>io.shardingjdbc</groupId>
  4. <artifactId>sharding-jdbc-core</artifactId>
  5. <version>${sharding-jdbc.version}</version>
  6. </dependency>

规则配置

Sharding-JDBC的分库分表通过规则配置描述,以下例子是根据user_id取模分库, 且根据order_id取模分表的两库两表的配置。

可以通过Java编码的方式配置:

  1. // 配置真实数据源
  2. Map<String, DataSource> dataSourceMap = new HashMap<>();
  3. // 配置第一个数据源
  4. BasicDataSource dataSource1 = new BasicDataSource();
  5. dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
  6. dataSource1.setUrl("jdbc:mysql://localhost:3306/ds_0");
  7. dataSource1.setUsername("root");
  8. dataSource1.setPassword("");
  9. dataSourceMap.put("ds_0", dataSource1);
  10. // 配置第二个数据源
  11. BasicDataSource dataSource2 = new BasicDataSource();
  12. dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
  13. dataSource2.setUrl("jdbc:mysql://localhost:3306/ds_1");
  14. dataSource2.setUsername("root");
  15. dataSource2.setPassword("");
  16. dataSourceMap.put("ds_1", dataSource2);
  17. // 配置Order表规则
  18. TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
  19. orderTableRuleConfig.setLogicTable("t_order");
  20. orderTableRuleConfig.setActualDataNodes("ds_${0..1}.t_order_${0..1}");
  21. // 配置分库策略
  22. orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds_${user_id % 2}"));
  23. // 配置分表策略
  24. orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_${order_id % 2}"));
  25. // 配置分片规则
  26. ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
  27. shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
  28. // 省略配置order_item表规则...
  29. // 获取数据源对象
  30. DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new ConcurrentHashMap(), new Properties());

或通过YAML方式配置,与以上配置等价:

  1. dataSources:
  2. ds_0: !!org.apache.commons.dbcp.BasicDataSource
  3. driverClassName: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ds_0
  5. username: root
  6. password:
  7. ds_1: !!org.apache.commons.dbcp.BasicDataSource
  8. driverClassName: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://localhost:3306/ds_1
  10. username: root
  11. password:
  12. tables:
  13. t_order:
  14. actualDataNodes: ds_${0..1}.t_order_${0..1}
  15. databaseStrategy:
  16. inline:
  17. shardingColumn: user_id
  18. algorithmInlineExpression: ds_$_${user_id % 2}
  19. tableStrategy:
  20. inline:
  21. shardingColumn: order_id
  22. algorithmInlineExpression: t_order_${order_id % 2}
  23. t_order_item:
  24. actualDataNodes: ds_${0..1}.t_order_item_${0..1}
  25. databaseStrategy:
  26. inline:
  27. shardingColumn: user_id
  28. algorithmInlineExpression: ds_$_${user_id % 2}
  29. tableStrategy:
  30. inline:
  31. shardingColumn: order_id
  32. algorithmInlineExpression: t_order_item_${order_id % 2}
  1. DataSource dataSource = ShardingDataSourceFactory.createDataSource(yamlFile);

规则配置包括数据源配置、表规则配置、分库策略和分表策略组成。这只是最简单的配置方式,实际使用可更加灵活,如:多分片键,分片策略直接和表规则配置绑定等。

详细的规则配置请参考分库分表

使用基于ShardingDataSource的JDBC接口

通过ShardingDataSourceFactory工厂和规则配置对象获取ShardingDataSource,ShardingDataSource实现自JDBC的标准接口DataSource。然后可通过DataSource选择使用原生JDBC开发,或者使用JPA, MyBatis等ORM工具。以JDBC原生实现为例:

  1. DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
  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 preparedStatement = conn.prepareStatement(sql)) {
  6. preparedStatement.setInt(1, 10);
  7. preparedStatement.setInt(2, 1001);
  8. try (ResultSet rs = preparedStatement.executeQuery()) {
  9. while(rs.next()) {
  10. System.out.println(rs.getInt(1));
  11. System.out.println(rs.getInt(2));
  12. }
  13. }
  14. }

使用Spring

Maven安装

  1. <!-- 引入sharding-jdbc核心模块 -->
  2. <dependency>
  3. <groupId>io.shardingjdbc</groupId>
  4. <artifactId>sharding-jdbc-core-spring-namespace</artifactId>
  5. <version>${sharding-jdbc.version}</version>
  6. </dependency>

Spring命名空间

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:sharding="http://shardingsphere.io/schema/shardingjdbc/sharding"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://shardingsphere.io/schema/shardingjdbc/sharding
  11. http://shardingsphere.io/schema/shardingjdbc/sharding/sharding.xsd
  12. ">
  13. <context:property-placeholder location="classpath:conf/conf.properties" ignore-unresolvable="true" />
  14. <bean id="ds_0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  15. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  16. <property name="url" value="jdbc:mysql://localhost:3306/ds_0" />
  17. <property name="username" value="root" />
  18. <property name="password" value="" />
  19. </bean>
  20. <bean id="ds_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  21. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  22. <property name="url" value="jdbc:mysql://localhost:3306/ds_1" />
  23. <property name="username" value="root" />
  24. <property name="password" value="" />
  25. </bean>
  26. <sharding:inline-strategy id="databaseStrategy" sharding-column="user_id" algorithm-expression="ds_${user_id % 2}" />
  27. <sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order_${order_id % 2}" />
  28. <sharding:inline-strategy id="orderItemTableStrategy" sharding-column="order_id" algorithm-expression="t_order_item_${order_id % 2}" />
  29. <sharding:data-source id="shardingDataSource">
  30. <sharding:sharding-rule data-source-names="ds_0,ds_1">
  31. <sharding:table-rules>
  32. <sharding:table-rule logic-table="t_order" actual-data-nodes="ds_${0..1}.t_order_${0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderTableStrategy" />
  33. <sharding:table-rule logic-table="t_order_item" actual-data-nodes="ds_${0..1}.t_order_item_${0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderItemTableStrategy" />
  34. </sharding:table-rules>
  35. </sharding:sharding-rule>
  36. </sharding:data-source>
  37. </beans>

详细的规则配置请参考配置手册