Use Spring Boot Starter

Import Maven Dependency

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

Configure Rule

Note: The example database connection pool is HikariCP, which can be replaced with other mainstream database connection pools according to business scenarios.

  1. # Configure actual data sources
  2. spring.shardingsphere.datasource.names=ds0,ds1
  3. # Configure the first data source
  4. spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
  5. spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
  6. spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/ds0
  7. spring.shardingsphere.datasource.ds0.username=root
  8. spring.shardingsphere.datasource.ds0.password=
  9. # Configure the second data source
  10. spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
  11. spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
  12. spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/ds1
  13. spring.shardingsphere.datasource.ds1.username=root
  14. spring.shardingsphere.datasource.ds1.password=
  15. # Configure t_order table rule
  16. spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order$->{0..1}
  17. # Configure database sharding strategy
  18. spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column=user_id
  19. spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name=database_inline
  20. # Configure table sharding strategy
  21. spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id
  22. spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=table_inline
  23. # Omit t_order_item table rule configuration ...
  24. # ...
  25. # Configure sharding algorithm
  26. spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.type=INLINE
  27. spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props.algorithm-expression=ds_${user_id % 2}
  28. spring.shardingsphere.rules.sharding.sharding-algorithms.table_inline.type=INLINE
  29. spring.shardingsphere.rules.sharding.sharding-algorithms.table_inline.props.algorithm-expression=t_order_${order_id % 2}

Use JNDI Data Source

If developer plan to use ShardingSphere-JDBC in Web Server (such as Tomcat) with JNDI data source, spring.shardingsphere.datasource.${datasourceName}.jndiName can be used as an alternative to series of configuration of datasource. For example:

  1. # Configure actual data sources
  2. spring.shardingsphere.datasource.names=ds0,ds1
  3. # Configure the first data source
  4. spring.shardingsphere.datasource.ds0.jndi-name=java:comp/env/jdbc/ds0
  5. # Configure the second data source
  6. spring.shardingsphere.datasource.ds1.jndi-name=java:comp/env/jdbc/ds1
  7. # Omit rule configurations ...
  8. # ...

Use ShardingSphereDataSource in Spring

ShardingSphereDataSource can be used directly by injection; or configure ShardingSphereDataSource in ORM frameworks such as JPA or MyBatis.

  1. @Resource
  2. private DataSource dataSource;