编排治理

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

不使用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. RegistryCenterConfiguration regConfig = new RegistryCenterConfiguration("zookeeper");
  5. regConfig.setServerLists("localhost:2181");
  6. regConfig.setNamespace("sharding-sphere-orchestration");
  7. // 配置治理
  8. OrchestrationConfiguration orchConfig = new OrchestrationConfiguration("orchestration-sharding-data-source", regConfig, false);
  9. // 获取数据源对象
  10. DataSource dataSource = OrchestrationShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties(), orchConfig);

基于Yaml的规则配置

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

  1. orchestration:
  2. name: orchestration-sharding-data-source
  3. overwrite: false
  4. registry:
  5. type: zookeeper
  6. serverLists: localhost:2181
  7. namespace: sharding-sphere-orchestration
  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.name=orchestration-sharding-data-source
  2. spring.shardingsphere.orchestration.overwrite=false
  3. spring.shardingsphere.orchestration.registry.type=zookeeper
  4. spring.shardingsphere.orchestration.registry.server-lists=localhost:2181
  5. spring.shardingsphere.orchestration.registry.namespace=sharding-jdbc-orchestration

基于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. <orchestraion:registry-center id="regCenter" type="zookeeper" server-lists="localhost:3181" namespace="orchestration-spring-namespace-test" operation-timeout-milliseconds="1000" max-retries="3" />
  11. <orchestraion:sharding-data-source id="simpleShardingOrchestration" data-source-ref="simpleShardingDataSource" registry-center-ref="regCenter" />
  12. </beans>

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