編排治理
使用治理功能需要指定一個注冊中心哩牍。配置將全部存入注冊中心,可以在每次啟動時使用本地配置覆蓋注冊中心配置疗垛,也可以只通過注冊中心讀取配置罢洲。
不使用Spring
引入Maven依賴
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-orchestration</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<!--若使用zookeeper, 請加入下面Maven坐標(biāo)-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
</dependency>
<!--若使用etcd, 請下面Maven坐標(biāo)-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-etcd</artifactId>
</dependency>
基于Java編碼的規(guī)則配置
// 省略配置dataSourceMap以及shardingRuleConfig
// ...
// 配置注冊中心
RegistryCenterConfiguration regConfig = new RegistryCenterConfiguration();
regConfig.setServerLists("localhost:2181");
regConfig.setNamespace("sharding-sphere-orchestration");
// 配置治理
OrchestrationConfiguration orchConfig = new OrchestrationConfiguration("orchestration-sharding-data-source", regConfig, false);
// 獲取數(shù)據(jù)源對象
DataSource dataSource = OrchestrationShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new ConcurrentHashMap(), new Properties(), orchConfig);
基于Yaml的規(guī)則配置
或通過Yaml方式配置,與以上配置等價:
orchestration:
name: orchestration-sharding-data-source
overwrite: false
registry:
serverLists: localhost:2181
namespace: sharding-sphere-orchestration
DataSource dataSource = YamlOrchestrationShardingDataSourceFactory.createDataSource(yamlFile);
使用Spring
引入Maven依賴
<!-- for spring boot -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-orchestration-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<!--若使用zookeeper, 請加入下面Maven坐標(biāo)-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<!--若使用etcd, 請加入下面Maven坐標(biāo)-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-etcd</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<!-- for spring namespace -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-orchestration-spring-namespace</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<!--若使用zookeeper, 請加入下面Maven坐標(biāo)-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<!--若使用etcd, 請加入下面Maven坐標(biāo)-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-etcd</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
基于Spring boot的規(guī)則配置
spring.shardingsphere.orchestration.name=orchestration-sharding-data-source
spring.shardingsphere.orchestration.overwrite=false
spring.shardingsphere.orchestration.registry.server-lists=localhost:2181
spring.shardingsphere.orchestration.registry.namespace=sharding-jdbc-orchestration
基于Spring命名空間的規(guī)則配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:orchestraion="http://shardingsphere.apache.org/schema/shardingsphere/orchestration"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://shardingsphere.apache.org/schema/shardingsphere/orchestration
http://shardingsphere.apache.org/schema/shardingsphere/orchestration/orchestration.xsd">
<import resource="namespace/shardingDataSourceNamespace.xml" />
<orchestraion:registry-center id="regCenter" server-lists="localhost:3181" namespace="orchestration-spring-namespace-test" operation-timeout-milliseconds="1000" max-retries="3" />
<orchestraion:sharding-data-source id="simpleShardingOrchestration" data-source-ref="simpleShardingDataSource" registry-center-ref="regCenter" />
</beans>
更多的詳細配置請參考配置手冊塑崖。