编排治理

    基于Java编码的规则配置

    1. // ...
    2. // 配置注册中心
    3. RegistryCenterConfiguration regConfig = new RegistryCenterConfiguration("zookeeper");
    4. regConfig.setServerLists("localhost:2181");
    5. regConfig.setNamespace("sharding-sphere-orchestration");
    6. // 配置治理
    7. OrchestrationConfiguration orchConfig = new OrchestrationConfiguration("orchestration-sharding-data-source", regConfig, false);
    8. // 获取数据源对象
    9. DataSource dataSource = OrchestrationShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig, new Properties(), orchConfig);

    使用Spring

    引入Maven依赖

    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. <artifactId>sharding-orchestration-reg-zookeeper-curator</artifactId>
    10. <version>${sharding-sphere.version}</version>

    基于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>