HA

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

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

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

    Attributes:

    Attributes:

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