HA

Background

Build high availability rule configuration through Java API.

Parameters

Root Configuration

Class name: org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration Attributes:

NameData TypeDescription
dataSources (+)Collection<DatabaseDiscoveryDataSourceRuleConfiguration>Data source configuration
discoveryHeartbeats (+)Map<String, DatabaseDiscoveryHeartBeatConfiguration>Detect heartbeat configuration
discoveryTypes (+)Map<String, AlgorithmConfiguration>Database discovery type configuration

Data Source Configuration

Class name: org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration

Attributes:

NameData TypeDescription
groupName (+)StringDatabase discovery group name
dataSourceNames (+)Collection<String>Data source names, multiple data source names separated with comma. Such as: ds_0, ds_1
discoveryHeartbeatName (+)StringDetect heartbeat name
discoveryTypeName (+)StringDatabase discovery type name

Detect Heartbeat Configuration

Class name: org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration

Attributes:

NameData TypeDescription
props (+)PropertiesDetect heartbeat attribute configuration, keep-alive-cron configuration, cron expression. Such as: 0/5 ?

Database Discovery Type Configuration

Class name: org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration

Attributes:

NameData TypeDescription
type (+)StringDatabase discovery type, such as: MySQL.MGR
props (?)PropertiesRequired parameters for high-availability types, such as MGR’s group-name

Procedure

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

Notice: Please change ${latest.release.version} to the actual version.

Sample

  1. // Build data source ds_0, ds_1, ds_2
  2. Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1);
  3. dataSourceMap.put("ds_0", createDataSource1("primary_demo_ds"));
  4. dataSourceMap.put("ds_1", createDataSource2("primary_demo_ds"));
  5. dataSourceMap.put("ds_2", createDataSource3("primary_demo_ds"));
  6. DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource("database_discovery_db", dataSourceMap, Arrays.asList(createDatabaseDiscoveryConfiguration(), createReadwriteSplittingConfiguration()), null);
  7. private static DatabaseDiscoveryRuleConfiguration createDatabaseDiscoveryConfiguration() {
  8. DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfiguration = new DatabaseDiscoveryDataSourceRuleConfiguration("readwrite_ds", Arrays.asList("ds_0, ds_1, ds_2"), "mgr-heartbeat", "mgr");
  9. return new DatabaseDiscoveryRuleConfiguration(Collections.singleton(dataSourceRuleConfiguration), createDiscoveryHeartbeats(), createDiscoveryTypes());
  10. }
  11. private static ReadwriteSplittingRuleConfiguration createReadwriteSplittingConfiguration() {
  12. ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfiguration1 = new ReadwriteSplittingDataSourceRuleConfiguration("replica_ds", new DynamicReadwriteSplittingStrategyConfiguration("readwrite_ds", true), "");
  13. return new ReadwriteSplittingRuleConfiguration(Arrays.asList(dataSourceConfiguration1), Collections.emptyMap());
  14. }
  15. private static Map<String, AlgorithmConfiguration> createDiscoveryTypes() {
  16. Map<String, AlgorithmConfiguration> discoveryTypes = new HashMap<>(1 1);
  17. Properties props = new Properties();
  18. props.put("group-name", "558edd3c-02ec-11ea-9bb3-080027e39bd2");
  19. discoveryTypes.put("mgr", new AlgorithmConfiguration("MGR", props));
  20. return discoveryTypes;
  21. }
  22. private static Map<String, DatabaseDiscoveryHeartBeatConfiguration> createDiscoveryHeartbeats() {
  23. Map<String, DatabaseDiscoveryHeartBeatConfiguration> discoveryHeartBeatConfiguration = new HashMap<>(1 1);
  24. Properties props = new Properties();
  25. props.put("keep-alive-cron", "0/5 * * * * ?");
  26. discoveryHeartBeatConfiguration.put("mgr-heartbeat", new DatabaseDiscoveryHeartBeatConfiguration(props));
  27. return discoveryHeartBeatConfiguration;
  28. }