高可用

背景信息

通过 Java API 方式构建高可用规则配置。

参数解释

配置入口

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

名称数据类型说明
dataSources (+)Collection<DatabaseDiscoveryDataSourceRuleConfiguration>数据源配置
discoveryHeartbeats (+)Map<String, DatabaseDiscoveryHeartBeatConfiguration>监听心跳配置
discoveryTypes (+)Map<String, AlgorithmConfiguration>数据库发现类型配置

数据源配置

类名称:org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration

可配置属性:

名称数据类型说明
groupName (+)String数据库发现组名称
dataSourceNames (+)Collection<String>数据源名称,多个数据源用逗号分隔 如:ds_0, ds_1
discoveryHeartbeatName (+)String监听心跳名称
discoveryTypeName (+)String数据库发现类型名称

监听心跳配置

类名称:org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration

可配置属性:

名称数据类型说明默认值
props (+)Properties监听心跳属性配置,keep-alive-cron 属性配置 cron 表达式,如:‘0/5 ?’-

数据库发现类型配置

类名称:org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration

名称数据类型说明
type (+)String数据库发现类型,如:MySQL.MGR
props (?)Properties数据库发现类型配置,如 MGR 的 group-name 属性配置

操作步骤

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

注意:请将 ${latest.release.version} 更改为实际的版本号。

配置示例

  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_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. }

相关参考