HA

Background

The Spring Boot Starter configuration method is applicable to business scenarios using SpringBoot. It can make full use of the SpringBoot configuration initialization and bean management capabilities, to automatically complete the creation of ShardingSphereDataSource objects.

Parameters

  1. spring.shardingsphere.datasource.names= # Omit data source configuration, please refer to the user manual
  2. spring.shardingsphere.rules.readwrite-splitting.data-sources.<readwrite-splitting-data-source-name>.dynamic-strategy.auto-aware-data-source-name= # Logical data source name discovered by the database
  3. spring.shardingsphere.rules.database-discovery.data-sources.<database-discovery-data-source-name>.data-source-names= # Data source name. Multiple data sources are separated by commas, for example: ds_0, ds_1
  4. spring.shardingsphere.rules.database-discovery.data-sources.<database-discovery-data-source-name>.discovery-heartbeat-name= # Detect heartbeat name
  5. spring.shardingsphere.rules.database-discovery.data-sources.<database-discovery-data-source-name>.discovery-type-name= # Database discovery type name
  6. spring.shardingsphere.rules.database-discovery.discovery-heartbeats.<discovery-heartbeat-name>.props.keep-alive-cron= # Cron expression, such as: '0/5 * * * * ?'
  7. spring.shardingsphere.rules.database-discovery.discovery-types.<discovery-type-name>.type= # Database discovery type, such as: MySQL.MGR
  8. spring.shardingsphere.rules.database-discovery.discovery-types.<discovery-type-name>.props.group-name= # Necessary parameters of database discovery type, such as group-name of MGR

Procedure

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

Note: please change `${latest.release.version}’ to the actual version number.

Sample

  1. spring.shardingsphere.datasource.names=ds-0,ds-1,ds-2
  2. spring.shardingsphere.datasource.ds-0.jdbc-url = jdbc:mysql://127.0.0.1:13306/primary_demo_ds?serverTimezone=UTC&useSSL=false
  3. spring.shardingsphere.datasource.ds-0.username=root
  4. spring.shardingsphere.datasource.ds-0.password=
  5. spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
  6. spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.cj.jdbc.Driver
  7. spring.shardingsphere.datasource.ds-1.jdbc-url = jdbc:mysql://127.0.0.1:13307/primary_demo_ds?serverTimezone=UTC&useSSL=false
  8. spring.shardingsphere.datasource.ds-1.username=root
  9. spring.shardingsphere.datasource.ds-1.password=
  10. spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
  11. spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.cj.jdbc.Driver
  12. spring.shardingsphere.datasource.ds-2.jdbc-url = jdbc:mysql://127.0.0.1:13308/primary_demo_ds?serverTimezone=UTC&useSSL=false
  13. spring.shardingsphere.datasource.ds-2.username=root
  14. spring.shardingsphere.datasource.ds-2.password=
  15. spring.shardingsphere.datasource.ds-2.type=com.zaxxer.hikari.HikariDataSource
  16. spring.shardingsphere.datasource.ds-2.driver-class-name=com.mysql.cj.jdbc.Driver
  17. spring.shardingsphere.rules.readwrite-splitting.data-sources.replica_ds.dynamic-strategy.auto-aware-data-source-name=readwrite_ds
  18. spring.shardingsphere.rules.database-discovery.data-sources.readwrite_ds.data-source-names=ds-0, ds-1, ds-2
  19. spring.shardingsphere.rules.database-discovery.data-sources.readwrite_ds.discovery-heartbeat-name=mgr-heartbeat
  20. spring.shardingsphere.rules.database-discovery.data-sources.readwrite_ds.discovery-type-name=mgr
  21. spring.shardingsphere.rules.database-discovery.discovery-heartbeats.mgr-heartbeat.props.keep-alive-cron=0/5 * * * * ?
  22. spring.shardingsphere.rules.database-discovery.discovery-types.mgr.type=MGR
  23. spring.shardingsphere.rules.database-discovery.discovery-types.mgr.props.groupName=b13df29e-90b6-11e8-8d1b-525400fc3996