应用治理性能须要指定配置核心和注册核心。配置将全副存入配置核心,能够在每次启动时应用本地配置笼罩配置核心配置,也能够只通过配置核心读取配置。
不应用 Spring
引入 Maven 依赖
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-orchestration</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<!-- 若应用 zookeeper, 请退出上面 Maven 坐标 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
基于 Java 编码的规定配置
// 省略配置 dataSourceMap 以及 shardingRuleConfig
// ...
// 配置注册核心
Properties properties = new Properties();
properties.setProperty("overwrite", overwrite);
CenterConfiguration centerConfiguration = new CenterConfiguration("zookeeper", properties);
centerConfiguration.setServerLists("localhost:2181");
centerConfiguration.setNamespace("sharding-sphere-orchestration");
centerConfiguration.setOrchestrationType("registry_center,config_center");
// 配置治理
Map<String, CenterConfiguration> instanceConfigurationMap = new HashMap<String, CenterConfiguration>();
instanceConfigurationMap.put("orchestration-sharding-data-source", centerConfiguration);
// 获取数据源对象
OrchestrationShardingDataSourceFactory.createDataSource(createDataSourceMap(), createShardingRuleConfig(), new HashMap<String, Object>(), new Properties(), new OrchestrationConfiguration(instanceConfigurationMap));
基于 Yaml 的规定配置
或通过 Yaml 形式配置,与以上配置等价:
orchestration:
orchestration_ds:
orchestrationType: registry_center,config_center
instanceType: zookeeper
serverLists: localhost:2181
namespace: orchestration
props:
overwrite: true
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 坐标 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-zookeeper-curator</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 坐标 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
基于 Spring boot 的规定配置
spring.shardingsphere.orchestration.spring_boot_ds_sharding.orchestration-type=registry_center,config_center
spring.shardingsphere.orchestration.spring_boot_ds_sharding.instance-type=zookeeper
spring.shardingsphere.orchestration.spring_boot_ds_sharding.server-lists=localhost:2181
spring.shardingsphere.orchestration.spring_boot_ds_sharding.namespace=orchestration-spring-boot-sharding-test
spring.shardingsphere.orchestration.spring_boot_ds_sharding.props.overwrite=true
基于 Spring 命名空间的规定配置
<?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" />
<util:properties id="instance-props">
<prop key="max-retries">3</prop>
<prop key="operation-timeout-milliseconds">3000</prop>
</util:properties>
<orchestraion:instance id="regCenter" orchestration-type="registry_center,config_center" instance-type="zookeeper" server-lists="localhost:2181" namespace="orchestration-spring-namespace-demo"
props-ref="instance-props" />
<orchestraion:sharding-data-source id="shardingDatabasesTablesDataSource" data-source-ref="realShardingDatabasesTablesDataSource" instance-ref="regCenter" overwrite="true" />
<orchestraion:master-slave-data-source id="masterSlaveDataSource" data-source-ref="realMasterSlaveDataSource" instance-ref="regCenter" overwrite="true" />
<orchestraion:encrypt-data-source id="encryptDataSource" data-source-ref="realEncryptDataSource" instance-ref="regCenter" overwrite="true" />
</beans>
更多的具体配置请参考配置手册。