使用 Spring 命名空间

引入Maven依赖

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

规则配置

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

  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. <!-- 配置真实数据源 -->
  11. <!-- 配置第 1 个数据源 -->
  12. <bean id="ds0" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  13. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  14. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ds0" />
  15. <property name="username" value="root" />
  16. <property name="password" value="" />
  17. </bean>
  18. <!-- 配置第 2 个数据源 -->
  19. <bean id="ds1" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  20. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  21. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ds1" />
  22. <property name="username" value="root" />
  23. <property name="password" value="" />
  24. </bean>
  25. <!-- 配置分库策略 -->
  26. <sharding:sharding-algorithm id="dbShardingAlgorithm" type="INLINE">
  27. <props>
  28. <prop key="algorithm-expression">ds$->{user_id % 2}</prop>
  29. </props>
  30. </sharding:sharding-algorithm>
  31. <sharding:standard-strategy id="dbStrategy" sharding-column="user_id" algorithm-ref="dbShardingAlgorithm" />
  32. <!-- 配置分表策略 -->
  33. <sharding:sharding-algorithm id="tableShardingAlgorithm" type="INLINE">
  34. <props>
  35. <prop key="algorithm-expression">t_order$->{order_id % 2}</prop>
  36. </props>
  37. </sharding:sharding-algorithm>
  38. <sharding:standard-strategy id="tableStrategy" sharding-column="user_id" algorithm-ref="tableShardingAlgorithm" />
  39. <!-- 配置分布式id生成策略 -->
  40. <sharding:key-generate-algorithm id="snowflakeAlgorithm" type="SNOWFLAKE">
  41. <props>
  42. <prop key="worker-id">123</prop>
  43. </props>
  44. </sharding:key-generate-algorithm>
  45. <sharding:key-generate-strategy id="orderKeyGenerator" column="order_id" algorithm-ref="snowflakeAlgorithm" />
  46. <!-- 配置sharding策略 -->
  47. <sharding:rule id="shardingRule">
  48. <sharding:table-rules>
  49. <sharding:table-rule logic-table="t_order" actual-data-nodes="ds${0..1}.t_order_${0..1}" database-strategy-ref="dbStrategy" table-strategy-ref="tableStrategy" key-generate-strategy-ref="orderKeyGenerator" />
  50. </sharding:table-rules>
  51. <sharding:binding-table-rules>
  52. <sharding:binding-table-rule logic-tables="t_order,t_order_item"/>
  53. </sharding:binding-table-rules>
  54. <sharding:broadcast-table-rules>
  55. <sharding:broadcast-table-rule table="t_address"/>
  56. </sharding:broadcast-table-rules>
  57. </sharding:rule>
  58. <!-- 配置ShardingSphereDataSource -->
  59. <shardingsphere:data-source id="shardingDataSource" data-source-names="ds0, ds1" rule-refs="shardingRule">
  60. <props>
  61. <prop key="sql-show">false</prop>
  62. </props>
  63. </shardingsphere:data-source>
  64. </beans>

在 Spring 中使用 ShardingSphereDataSource

直接通过注入的方式即可使用 ShardingSphereDataSource;或者将 ShardingSphereDataSource 配置在JPA, MyBatis 等 ORM 框架中配合使用。

  1. @Resource
  2. private DataSource dataSource;