数据分片

不使用Spring

引入Maven依赖

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

基于Java编码的规则配置

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

  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/ds0");
  7. dataSource1.setUsername("root");
  8. dataSource1.setPassword("");
  9. dataSourceMap.put("ds0", dataSource1);
  10. // 配置第二个数据源
  11. BasicDataSource dataSource2 = new BasicDataSource();
  12. dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
  13. dataSource2.setUrl("jdbc:mysql://localhost:3306/ds1");
  14. dataSource2.setUsername("root");
  15. dataSource2.setPassword("");
  16. dataSourceMap.put("ds1", dataSource2);
  17. // 配置Order表规则
  18. TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration("t_order","ds${0..1}.t_order${0..1}");
  19. // 配置分库 + 分表策略
  20. orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
  21. orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order${order_id % 2}"));
  22. // 配置分片规则
  23. ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
  24. shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
  25. // 省略配置order_item表规则...
  26. // ...
  27. // 获取数据源对象
  28. DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties());

基于Yaml的规则配置

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

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

使用原生JDBC

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

  1. DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(yamlFile);
  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. <!-- for spring boot -->
  2. <dependency>
  3. <groupId>org.apache.shardingsphere</groupId>
  4. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  5. <version>${sharding-sphere.version}</version>
  6. </dependency>
  7. <!-- for spring namespace -->
  8. <dependency>
  9. <groupId>org.apache.shardingsphere</groupId>
  10. <artifactId>sharding-jdbc-spring-namespace</artifactId>
  11. <version>${sharding-sphere.version}</version>
  12. </dependency>

基于Spring boot的规则配置

  1. spring.shardingsphere.datasource.names=ds0,ds1
  2. spring.shardingsphere.datasource.ds0.type=org.apache.commons.dbcp2.BasicDataSource
  3. spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
  4. spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0
  5. spring.shardingsphere.datasource.ds0.username=root
  6. spring.shardingsphere.datasource.ds0.password=
  7. spring.shardingsphere.datasource.ds1.type=org.apache.commons.dbcp2.BasicDataSource
  8. spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
  9. spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1
  10. spring.shardingsphere.datasource.ds1.username=root
  11. spring.shardingsphere.datasource.ds1.password=
  12. spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
  13. spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}
  14. spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
  15. spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
  16. spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}
  17. spring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item$->{0..1}
  18. spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
  19. spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item$->{order_id % 2}

基于Spring boot + JNDI的规则配置

如果您计划使用Spring boot + JNDI的方式,在应用容器(如Tomcat)中使用Sharding-JDBC时,可使用spring.shardingsphere.datasource.${datasourceName}.jndiName来代替数据源的一系列配置。如:

  1. spring.shardingsphere.datasource.names=ds0,ds1
  2. spring.shardingsphere.datasource.ds0.jndi-name=java:comp/env/jdbc/ds0
  3. spring.shardingsphere.datasource.ds1.jndi-name=jdbc/ds1
  4. spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=user_id
  5. spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}
  6. spring.shardingsphere.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
  7. spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
  8. spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order$->{order_id % 2}
  9. spring.shardingsphere.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item$->{0..1}
  10. spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
  11. spring.shardingsphere.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item$->{order_id % 2}

基于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:sharding="http://shardingsphere.apache.org/schema/shardingsphere/sharding"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://shardingsphere.apache.org/schema/shardingsphere/sharding
  8. http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd
  9. ">
  10. <bean id="ds0" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  11. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  12. <property name="url" value="jdbc:mysql://localhost:3306/ds0" />
  13. <property name="username" value="root" />
  14. <property name="password" value="" />
  15. </bean>
  16. <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  17. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  18. <property name="url" value="jdbc:mysql://localhost:3306/ds1" />
  19. <property name="username" value="root" />
  20. <property name="password" value="" />
  21. </bean>
  22. <sharding:inline-strategy id="databaseStrategy" sharding-column="user_id" algorithm-expression="ds$->{user_id % 2}" />
  23. <sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order$->{order_id % 2}" />
  24. <sharding:inline-strategy id="orderItemTableStrategy" sharding-column="order_id" algorithm-expression="t_order_item$->{order_id % 2}" />
  25. <sharding:data-source id="shardingDataSource">
  26. <sharding:sharding-rule data-source-names="ds0,ds1">
  27. <sharding:table-rules>
  28. <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" />
  29. <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" />
  30. </sharding:table-rules>
  31. </sharding:sharding-rule>
  32. </sharding:data-source>
  33. </beans>

在Spring中使用DataSource

直接通过注入的方式即可使用DataSource,或者将DataSource配置在JPA、Hibernate或MyBatis中使用。

  1. @Resource
  2. private DataSource dataSource;

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