高可用

    类名称:org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration 可配置属性:

    可配置属性:

    可配置属性:

    1. 引入 Maven 依赖
    1. // 构建数据源 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. private static DatabaseDiscoveryRuleConfiguration createDatabaseDiscoveryConfiguration() {
    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. Properties props = new Properties();
    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;
    24. }