编排治理

使用治理功能需要指定配置中心和注册中心。配置将全部存入配置中心,可以在每次启动时使用本地配置覆盖配置中心配置,也可以只通过配置中心读取配置。

不使用Spring

引入Maven依赖

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>sharding-jdbc-orchestration</artifactId>
  4. <version>${sharding-sphere.version}</version>
  5. </dependency>
  6. <!--若使用zookeeper, 请加入下面Maven坐标-->
  7. <dependency>
  8. <groupId>org.apache.shardingsphere</groupId>
  9. <artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
  10. <version>${sharding-sphere.version}</version>
  11. </dependency>

基于Java编码的规则配置

  1. // 省略配置dataSourceMap以及shardingRuleConfig
  2. // ...
  3. // 配置注册中心
  4. Properties properties = new Properties();
  5. properties.setProperty("overwrite", overwrite);
  6. CenterConfiguration centerConfiguration = new CenterConfiguration("zookeeper", properties);
  7. centerConfiguration.setServerLists("localhost:2181");
  8. centerConfiguration.setNamespace("sharding-sphere-orchestration");
  9. centerConfiguration.setOrchestrationType("registry_center,config_center");
  10. // 配置治理
  11. Map<String, CenterConfiguration> instanceConfigurationMap = new HashMap<String, CenterConfiguration>();
  12. instanceConfigurationMap.put("orchestration-sharding-data-source", centerConfiguration);
  13. // 获取数据源对象
  14. OrchestrationShardingDataSourceFactory.createDataSource(
  15. createDataSourceMap(), createShardingRuleConfig(), new HashMap<String, Object>(), new Properties(), new OrchestrationConfiguration(instanceConfigurationMap));

基于Yaml的规则配置

或通过Yaml方式配置,与以上配置等价:

  1. orchestration:
  2. orchestration_ds:
  3. orchestrationType: registry_center,config_center
  4. instanceType: zookeeper
  5. serverLists: localhost:2181
  6. namespace: orchestration
  7. props:
  8. overwrite: true
  1. DataSource dataSource = YamlOrchestrationShardingDataSourceFactory.createDataSource(yamlFile);

使用Spring

引入Maven依赖

  1. <!-- for spring boot -->
  2. <dependency>
  3. <groupId>org.apache.shardingsphere</groupId>
  4. <artifactId>sharding-jdbc-orchestration-spring-boot-starter</artifactId>
  5. <version>${sharding-sphere.version}</version>
  6. </dependency>
  7. <!--若使用zookeeper, 请加入下面Maven坐标-->
  8. <dependency>
  9. <groupId>org.apache.shardingsphere</groupId>
  10. <artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
  11. <version>${sharding-sphere.version}</version>
  12. </dependency>
  1. <!-- for spring namespace -->
  2. <dependency>
  3. <groupId>org.apache.shardingsphere</groupId>
  4. <artifactId>sharding-jdbc-orchestration-spring-namespace</artifactId>
  5. <version>${sharding-sphere.version}</version>
  6. </dependency>
  7. <!--若使用zookeeper, 请加入下面Maven坐标-->
  8. <dependency>
  9. <groupId>org.apache.shardingsphere</groupId>
  10. <artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
  11. <version>${sharding-sphere.version}</version>
  12. </dependency>

基于Spring boot的规则配置

  1. spring.shardingsphere.orchestration.spring_boot_ds_sharding.orchestration-type=registry_center,config_center
  2. spring.shardingsphere.orchestration.spring_boot_ds_sharding.instance-type=zookeeper
  3. spring.shardingsphere.orchestration.spring_boot_ds_sharding.server-lists=localhost:2181
  4. spring.shardingsphere.orchestration.spring_boot_ds_sharding.namespace=orchestration-spring-boot-sharding-test
  5. spring.shardingsphere.orchestration.spring_boot_ds_sharding.props.overwrite=true

基于Spring命名空间的规则配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:orchestraion="http://shardingsphere.apache.org/schema/shardingsphere/orchestration"
  4. xmlns="http://www.springframework.org/schema/beans"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://shardingsphere.apache.org/schema/shardingsphere/orchestration
  8. http://shardingsphere.apache.org/schema/shardingsphere/orchestration/orchestration.xsd">
  9. <import resource="namespace/shardingDataSourceNamespace.xml" />
  10. <util:properties id="instance-props">
  11. <prop key="max-retries">3</prop>
  12. <prop key="operation-timeout-milliseconds">3000</prop>
  13. </util:properties>
  14. <orchestraion:instance id="regCenter" orchestration-type="registry_center,config_center" instance-type="zookeeper" server-lists="localhost:2181" namespace="orchestration-spring-namespace-demo"
  15. props-ref="instance-props" />
  16. <orchestraion:sharding-data-source id="shardingDatabasesTablesDataSource" data-source-ref="realShardingDatabasesTablesDataSource" instance-ref="regCenter" overwrite="true" />
  17. <orchestraion:master-slave-data-source id="masterSlaveDataSource" data-source-ref="realMasterSlaveDataSource" instance-ref="regCenter" overwrite="true" />
  18. <orchestraion:encrypt-data-source id="encryptDataSource" data-source-ref="realEncryptDataSource" instance-ref="regCenter" overwrite="true" />
  19. </beans>

更多的详细配置请参考配置手册