数据治理

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

不使用Spring

引入Maven依赖

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

基于Java编码的规则配置

  1. // 省略配置dataSourceMap以及shardingRuleConfig
  2. // ...
  3. // 配置注册中心
  4. RegistryCenterConfiguration regConfig = new RegistryCenterConfiguration();
  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 ConcurrentHashMap(), new Properties(), orchConfig);

基于Yaml的规则配置

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

  1. orchestration:
  2. name: orchestration-sharding-data-source
  3. overwrite: false
  4. registry:
  5. serverLists: localhost:2181
  6. namespace: sharding-sphere-orchestration
  1. DataSource dataSource = YamlOrchestrationShardingDataSourceFactory.createDataSource(yamlFile);

使用Spring

引入Maven依赖

  1. <!-- for spring boot -->
  2. <dependency>
  3. <groupId>io.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>io.shardingsphere</groupId>
  10. <artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
  11. <version>${sharding-sphere.version}</version>
  12. </dependency>
  13. <!--若使用etcd, 请加入下面Maven坐标-->
  14. <dependency>
  15. <groupId>io.shardingsphere</groupId>
  16. <artifactId>sharding-orchestration-reg-etcd</artifactId>
  17. <version>${sharding-sphere.version}</version>
  18. </dependency>
  1. <!-- for spring namespace -->
  2. <dependency>
  3. <groupId>io.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>io.shardingsphere</groupId>
  10. <artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
  11. <version>${sharding-sphere.version}</version>
  12. </dependency>
  13. <!--若使用etcd, 请加入下面Maven坐标-->
  14. <dependency>
  15. <groupId>io.shardingsphere</groupId>
  16. <artifactId>sharding-orchestration-reg-etcd</artifactId>
  17. <version>${sharding-sphere.version}</version>
  18. </dependency>

基于Spring boot的规则配置

  1. sharding.jdbc.config.orchestration.name=orchestration-sharding-data-source
  2. sharding.jdbc.config.orchestration.overwrite=false
  3. sharding.jdbc.config.orchestration.registry.server-lists=localhost:2181
  4. sharding.jdbc.config.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.io/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.io/schema/shardingsphere/orchestration
  8. http://shardingsphere.io/schema/shardingsphere/orchestration/orchestration.xsd">
  9. <import resource="namespace/shardingDataSourceNamespace.xml" />
  10. <orchestraion:registry-center id="regCenter" 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>

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