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>
  6. <!-- import if using ZooKeeper -->
  7. <dependency>
  8. <groupId>org.apache.shardingsphere</groupId>
  9. <artifactId>shardingsphere-cluster-mode-repository-zookeeper-curator</artifactId>
  10. <version>${shardingsphere.version}</version>
  11. </dependency>
  12. <!-- import if using Etcd -->
  13. <dependency>
  14. <groupId>org.apache.shardingsphere</groupId>
  15. <artifactId>shardingsphere-cluster-mode-repository-etcd</artifactId>
  16. <version>${shardingsphere.version}</version>
  17. </dependency>

Configure Rule

  1. spring.shardingsphere.mode.type=Cluster
  2. spring.shardingsphere.mode.repository.type=ZooKeeper
  3. spring.shardingsphere.mode.repository.props.namespace=governance-spring-boot-shardingsphere-test
  4. spring.shardingsphere.mode.repository.props.server-lists=localhost:2181
  5. spring.shardingsphere.mode.overwrite=true

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;