高可用
类名称:org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration 可配置属性:
可配置属性:
可配置属性:
- 引入 Maven 依赖
// 构建数据源 ds_0,ds_1,ds_2
Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1);
dataSourceMap.put("ds_0", createDataSource1("primary_demo_ds"));
dataSourceMap.put("ds_2", createDataSource3("primary_demo_ds"));
DataSource dataSource = ShardingSphereDataSourceFactory.createDataSource("database_discovery_db", dataSourceMap, Arrays.asList(createDatabaseDiscoveryConfiguration(), createReadwriteSplittingConfiguration()), null);
private static DatabaseDiscoveryRuleConfiguration createDatabaseDiscoveryConfiguration() {
return new DatabaseDiscoveryRuleConfiguration(Collections.singleton(dataSourceRuleConfiguration), createDiscoveryHeartbeats(), createDiscoveryTypes());
}
private static ReadwriteSplittingRuleConfiguration createReadwriteSplittingConfiguration() {
ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfiguration1 = new ReadwriteSplittingDataSourceRuleConfiguration("replica_ds", new DynamicReadwriteSplittingStrategyConfiguration("readwrite_ds", true), "");
return new ReadwriteSplittingRuleConfiguration(Arrays.asList(dataSourceConfiguration1), Collections.emptyMap());
}
Map<String, AlgorithmConfiguration> discoveryTypes = new HashMap<>(1, 1);
Properties props = new Properties();
discoveryTypes.put("mgr", new AlgorithmConfiguration("MGR", props));
return discoveryTypes;
}
private static Map<String, DatabaseDiscoveryHeartBeatConfiguration> createDiscoveryHeartbeats() {
Map<String, DatabaseDiscoveryHeartBeatConfiguration> discoveryHeartBeatConfiguration = new HashMap<>(1, 1);
Properties props = new Properties();
props.put("keep-alive-cron", "0/5 * * * * ?");
discoveryHeartBeatConfiguration.put("mgr-heartbeat", new DatabaseDiscoveryHeartBeatConfiguration(props));
return discoveryHeartBeatConfiguration;
}