This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 404e4a8  Implement traffic rule configuration loading and 
initialization (#14318)
404e4a8 is described below

commit 404e4a8d6ded028fde1c6c2297b91aea68e01a44
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Sat Dec 25 19:34:52 2021 +0800

    Implement traffic rule configuration loading and initialization (#14318)
    
    * implement traffic rule configuration loading and initialization
    
    * modify java doc for TrafficLoadBalanceAlgorithm
    
    * remove useless param when init TrafficRule
    
    * modify param name
    
    * add unit test
---
 .../shardingsphere-jdbc-core/pom.xml               |   5 +
 .../namespace/AbstractSpringNamespaceTest.java     |   2 +-
 shardingsphere-kernel/pom.xml                      |   1 +
 .../{ => shardingsphere-traffic}/pom.xml           |  14 +--
 .../shardingsphere-traffic-api}/pom.xml            |  25 ++--
 .../traffic/config/TrafficRuleConfiguration.java   |  40 +++++++
 .../config/TrafficStrategyConfiguration.java       |  39 +++++++
 .../traffic/spi/TrafficAlgorithm.java              |  27 +++++
 .../traffic/spi/TrafficLoadBalanceAlgorithm.java   |  27 +++++
 .../shardingsphere-traffic-core}/pom.xml           |  30 ++---
 .../traffic/constant/TrafficOrder.java             |  33 ++++++
 .../shardingsphere/traffic/rule/TrafficRule.java   |  53 +++++++++
 .../DefaultTrafficRuleConfigurationBuilder.java    |  43 +++++++
 .../traffic/rule/builder/TrafficRuleBuilder.java   |  48 ++++++++
 .../yaml/config/YamlTrafficRuleConfiguration.java  |  46 ++++++++
 .../config/YamlTrafficStrategyConfiguration.java   |  41 +++++++
 .../TrafficRuleConfigurationYamlSwapper.java       |  90 +++++++++++++++
 .../TrafficStrategyConfigurationYamlSwapper.java   |  43 +++++++
 ...er.global.DefaultGlobalRuleConfigurationBuilder |  18 +++
 ...ere.infra.rule.builder.global.GlobalRuleBuilder |  18 +++
 ...aml.config.swapper.YamlRuleConfigurationSwapper |  18 +++
 .../traffic/rule/TrafficRuleTest.java              |  34 ++++++
 ...DefaultTrafficRuleConfigurationBuilderTest.java |  50 ++++++++
 .../rule/builder/TrafficRuleBuilderTest.java       |  40 +++++++
 .../TrafficRuleConfigurationYamlSwapperTest.java   | 128 +++++++++++++++++++++
 .../shardingsphere-proxy-backend/pom.xml           |   5 +
 26 files changed, 880 insertions(+), 38 deletions(-)

diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml 
b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
index 1cc3b06..442c494 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/pom.xml
@@ -69,6 +69,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-traffic-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-infra-context</artifactId>
             <version>${project.version}</version>
         </dependency>
diff --git 
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/AbstractSpringNamespaceTest.java
 
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/AbstractSpringNamespaceTest.java
index ee187d4..591a7ae 100644
--- 
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/AbstractSpringNamespaceTest.java
+++ 
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-namespace/src/test/java/org/apache/shardingsphere/spring/namespace/AbstractSpringNamespaceTest.java
@@ -61,7 +61,7 @@ public abstract class AbstractSpringNamespaceTest extends 
AbstractJUnit4SpringCo
                 assertEncryptRule((EncryptRule) each);
             }
         }
-        assertThat(globalRules.size(), is(3));
+        assertThat(globalRules.size(), is(4));
         for (ShardingSphereRule each : globalRules) {
             if (each instanceof SQLParserRule) {
                 assertSQLParserRule((SQLParserRule) each);
diff --git a/shardingsphere-kernel/pom.xml b/shardingsphere-kernel/pom.xml
index 3625183..00222ae 100644
--- a/shardingsphere-kernel/pom.xml
+++ b/shardingsphere-kernel/pom.xml
@@ -36,5 +36,6 @@
         <module>shardingsphere-schedule</module>
         <module>shardingsphere-data-pipeline</module>
         <module>shardingsphere-parser</module>
+        <module>shardingsphere-traffic</module>
     </modules>
 </project>
diff --git a/shardingsphere-kernel/pom.xml 
b/shardingsphere-kernel/shardingsphere-traffic/pom.xml
similarity index 76%
copy from shardingsphere-kernel/pom.xml
copy to shardingsphere-kernel/shardingsphere-traffic/pom.xml
index 3625183..0f1fb25 100644
--- a/shardingsphere-kernel/pom.xml
+++ b/shardingsphere-kernel/shardingsphere-traffic/pom.xml
@@ -22,19 +22,15 @@
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.apache.shardingsphere</groupId>
-        <artifactId>shardingsphere</artifactId>
+        <artifactId>shardingsphere-kernel</artifactId>
         <version>5.0.1-SNAPSHOT</version>
     </parent>
-    <artifactId>shardingsphere-kernel</artifactId>
+    <artifactId>shardingsphere-traffic</artifactId>
     <packaging>pom</packaging>
     <name>${project.artifactId}</name>
-    
+
     <modules>
-        <module>shardingsphere-single-table</module>
-        <module>shardingsphere-authority</module>
-        <module>shardingsphere-transaction</module>
-        <module>shardingsphere-schedule</module>
-        <module>shardingsphere-data-pipeline</module>
-        <module>shardingsphere-parser</module>
+        <module>shardingsphere-traffic-api</module>
+        <module>shardingsphere-traffic-core</module>
     </modules>
 </project>
diff --git a/shardingsphere-kernel/pom.xml 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/pom.xml
similarity index 66%
copy from shardingsphere-kernel/pom.xml
copy to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/pom.xml
index 3625183..d945a55 100644
--- a/shardingsphere-kernel/pom.xml
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/pom.xml
@@ -16,25 +16,22 @@
   ~ limitations under the License.
   -->
 
-<project xmlns="http://maven.apache.org/POM/4.0.0";
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.apache.shardingsphere</groupId>
-        <artifactId>shardingsphere</artifactId>
+        <artifactId>shardingsphere-traffic</artifactId>
         <version>5.0.1-SNAPSHOT</version>
     </parent>
-    <artifactId>shardingsphere-kernel</artifactId>
-    <packaging>pom</packaging>
+    <artifactId>shardingsphere-traffic-api</artifactId>
     <name>${project.artifactId}</name>
-    
-    <modules>
-        <module>shardingsphere-single-table</module>
-        <module>shardingsphere-authority</module>
-        <module>shardingsphere-transaction</module>
-        <module>shardingsphere-schedule</module>
-        <module>shardingsphere-data-pipeline</module>
-        <module>shardingsphere-parser</module>
-    </modules>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-infra-common</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
 </project>
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficRuleConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficRuleConfiguration.java
new file mode 100644
index 0000000..5c96233
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficRuleConfiguration.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.config;
+
+import lombok.Getter;
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.config.scope.GlobalRuleConfiguration;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+
+/**
+ * Traffic rule configuration.
+ */
+@Getter
+public final class TrafficRuleConfiguration implements GlobalRuleConfiguration 
{
+    
+    private final Collection<TrafficStrategyConfiguration> trafficStrategies = 
new LinkedList<>();
+    
+    private final Map<String, ShardingSphereAlgorithmConfiguration> 
trafficAlgorithms = new LinkedHashMap<>();
+    
+    private final Map<String, ShardingSphereAlgorithmConfiguration> 
loadBalancers = new LinkedHashMap<>();
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
new file mode 100644
index 0000000..0b1699d
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/config/TrafficStrategyConfiguration.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.config;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.util.Collection;
+
+/**
+ * Traffic strategy configuration.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class TrafficStrategyConfiguration {
+    
+    private final String name;
+    
+    private final Collection<String> labels;
+    
+    private final String algorithmName;
+    
+    private final String loadBalancerName;
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficAlgorithm.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficAlgorithm.java
new file mode 100644
index 0000000..f6b634f
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficAlgorithm.java
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.spi;
+
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmPostProcessor;
+
+/**
+ * Traffic algorithm.
+ */
+public interface TrafficAlgorithm extends ShardingSphereAlgorithm, 
ShardingSphereAlgorithmPostProcessor {
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java
new file mode 100644
index 0000000..f7e8393
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-api/src/main/java/org/apache/shardingsphere/traffic/spi/TrafficLoadBalanceAlgorithm.java
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.spi;
+
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
+import org.apache.shardingsphere.spi.required.RequiredSPI;
+
+/**
+ * Traffic load balance algorithm.
+ */
+public interface TrafficLoadBalanceAlgorithm extends ShardingSphereAlgorithm, 
RequiredSPI {
+}
diff --git a/shardingsphere-kernel/pom.xml 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/pom.xml
similarity index 63%
copy from shardingsphere-kernel/pom.xml
copy to 
shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/pom.xml
index 3625183..83eda10 100644
--- a/shardingsphere-kernel/pom.xml
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/pom.xml
@@ -16,25 +16,27 @@
   ~ limitations under the License.
   -->
 
-<project xmlns="http://maven.apache.org/POM/4.0.0";
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
     <modelVersion>4.0.0</modelVersion>
     <parent>
         <groupId>org.apache.shardingsphere</groupId>
-        <artifactId>shardingsphere</artifactId>
+        <artifactId>shardingsphere-traffic</artifactId>
         <version>5.0.1-SNAPSHOT</version>
     </parent>
-    <artifactId>shardingsphere-kernel</artifactId>
-    <packaging>pom</packaging>
+    <artifactId>shardingsphere-traffic-core</artifactId>
     <name>${project.artifactId}</name>
-    
-    <modules>
-        <module>shardingsphere-single-table</module>
-        <module>shardingsphere-authority</module>
-        <module>shardingsphere-transaction</module>
-        <module>shardingsphere-schedule</module>
-        <module>shardingsphere-data-pipeline</module>
-        <module>shardingsphere-parser</module>
-    </modules>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-infra-common</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-traffic-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
 </project>
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/constant/TrafficOrder.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/constant/TrafficOrder.java
new file mode 100644
index 0000000..2aaa178
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/constant/TrafficOrder.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.constant;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+/**
+ * Traffic order.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class TrafficOrder {
+    
+    /**
+     * Traffic order.
+     */
+    public static final int ORDER = 800;
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
new file mode 100644
index 0000000..a77cc2f
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/TrafficRule.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.rule;
+
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
+import org.apache.shardingsphere.infra.rule.identifier.scope.GlobalRule;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.spi.TrafficAlgorithm;
+import org.apache.shardingsphere.traffic.spi.TrafficLoadBalanceAlgorithm;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * Traffic rule.
+ */
+public final class TrafficRule implements GlobalRule {
+    
+    static {
+        ShardingSphereServiceLoader.register(TrafficAlgorithm.class);
+        
ShardingSphereServiceLoader.register(TrafficLoadBalanceAlgorithm.class);
+    }
+    
+    private final Map<String, TrafficAlgorithm> trafficAlgorithms = new 
LinkedHashMap<>();
+    
+    private final Map<String, TrafficLoadBalanceAlgorithm> loadBalancers = new 
LinkedHashMap<>();
+    
+    public TrafficRule(final TrafficRuleConfiguration config) {
+        config.getTrafficAlgorithms().forEach((key, value) -> 
trafficAlgorithms.put(key, 
ShardingSphereAlgorithmFactory.createAlgorithm(value, TrafficAlgorithm.class)));
+        config.getLoadBalancers().forEach((key, value) -> 
loadBalancers.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, 
TrafficLoadBalanceAlgorithm.class)));
+    }
+    
+    @Override
+    public String getType() {
+        return TrafficRule.class.getSimpleName();
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilder.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilder.java
new file mode 100644
index 0000000..88e4390
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilder.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.rule.builder;
+
+import 
org.apache.shardingsphere.infra.rule.builder.global.DefaultGlobalRuleConfigurationBuilder;
+import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.constant.TrafficOrder;
+
+/**
+ * Default traffic rule configuration builder.
+ */
+public final class DefaultTrafficRuleConfigurationBuilder implements 
DefaultGlobalRuleConfigurationBuilder<TrafficRuleConfiguration, 
TrafficRuleBuilder> {
+    
+    @Override
+    public TrafficRuleConfiguration build() {
+        return new TrafficRuleConfiguration();
+    }
+    
+    @Override
+    public int getOrder() {
+        return TrafficOrder.ORDER;
+    }
+    
+    @Override
+    public Class<TrafficRuleBuilder> getTypeClass() {
+        return TrafficRuleBuilder.class;
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilder.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilder.java
new file mode 100644
index 0000000..8959908
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilder.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.rule.builder;
+
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder;
+import org.apache.shardingsphere.infra.rule.identifier.scope.GlobalRule;
+import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.constant.TrafficOrder;
+import org.apache.shardingsphere.traffic.rule.TrafficRule;
+
+import java.util.Map;
+
+/**
+ * Traffic rule builder.
+ */
+public final class TrafficRuleBuilder implements 
GlobalRuleBuilder<TrafficRuleConfiguration> {
+    
+    @Override
+    public GlobalRule build(final TrafficRuleConfiguration ruleConfig, final 
Map<String, ShardingSphereMetaData> mataDataMap) {
+        return new TrafficRule(ruleConfig);
+    }
+    
+    @Override
+    public int getOrder() {
+        return TrafficOrder.ORDER;
+    }
+    
+    @Override
+    public Class<TrafficRuleConfiguration> getTypeClass() {
+        return TrafficRuleConfiguration.class;
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficRuleConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficRuleConfiguration.java
new file mode 100644
index 0000000..45d50e5
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficRuleConfiguration.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.yaml.config;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRuleConfiguration;
+import 
org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * Traffic rule configuration for YAML.
+ */
+@Getter
+@Setter
+public final class YamlTrafficRuleConfiguration implements 
YamlRuleConfiguration {
+    
+    private Map<String, YamlTrafficStrategyConfiguration> trafficStrategies = 
new LinkedHashMap<>();
+    
+    private Map<String, YamlShardingSphereAlgorithmConfiguration> 
trafficAlgorithms = new LinkedHashMap<>();
+    
+    private Map<String, YamlShardingSphereAlgorithmConfiguration> 
loadBalancers = new LinkedHashMap<>();
+    
+    @Override
+    public Class<TrafficRuleConfiguration> getRuleConfigurationType() {
+        return TrafficRuleConfiguration.class;
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficStrategyConfiguration.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficStrategyConfiguration.java
new file mode 100644
index 0000000..6b6f705
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/config/YamlTrafficStrategyConfiguration.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.yaml.config;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.yaml.config.pojo.YamlConfiguration;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * Yaml traffic strategy configuration.
+ */
+@Getter
+@Setter
+public final class YamlTrafficStrategyConfiguration implements 
YamlConfiguration {
+    
+    private String name;
+    
+    private Collection<String> labels = new LinkedList<>();
+    
+    private String algorithmName;
+    
+    private String loadBalancerName;
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapper.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapper.java
new file mode 100644
index 0000000..979a474
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapper.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.yaml.swapper;
+
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapper;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.ShardingSphereAlgorithmConfigurationYamlSwapper;
+import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.constant.TrafficOrder;
+import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficRuleConfiguration;
+import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficStrategyConfiguration;
+
+import java.util.Map.Entry;
+
+/**
+ * TrafficRuleConfiguration rule configuration YAML swapper.
+ */
+public final class TrafficRuleConfigurationYamlSwapper implements 
YamlRuleConfigurationSwapper<YamlTrafficRuleConfiguration, 
TrafficRuleConfiguration> {
+    
+    private final TrafficStrategyConfigurationYamlSwapper strategySwapper = 
new TrafficStrategyConfigurationYamlSwapper();
+    
+    private final ShardingSphereAlgorithmConfigurationYamlSwapper 
algorithmSwapper = new ShardingSphereAlgorithmConfigurationYamlSwapper();
+    
+    @Override
+    public YamlTrafficRuleConfiguration swapToYamlConfiguration(final 
TrafficRuleConfiguration data) {
+        YamlTrafficRuleConfiguration result = new 
YamlTrafficRuleConfiguration();
+        data.getTrafficStrategies().forEach(each -> 
result.getTrafficStrategies().put(each.getName(), 
strategySwapper.swapToYamlConfiguration(each)));
+        setYamlAlgorithms(data, result);
+        return result;
+    }
+    
+    private void setYamlAlgorithms(final TrafficRuleConfiguration data, final 
YamlTrafficRuleConfiguration yamlConfig) {
+        if (null != data.getTrafficAlgorithms()) {
+            data.getTrafficAlgorithms().forEach((key, value) -> 
yamlConfig.getTrafficAlgorithms().put(key, 
algorithmSwapper.swapToYamlConfiguration(value)));
+        }
+        if (null != data.getLoadBalancers()) {
+            data.getLoadBalancers().forEach((key, value) -> 
yamlConfig.getLoadBalancers().put(key, 
algorithmSwapper.swapToYamlConfiguration(value)));
+        }
+    }
+    
+    @Override
+    public TrafficRuleConfiguration swapToObject(final 
YamlTrafficRuleConfiguration yamlConfig) {
+        TrafficRuleConfiguration result = new TrafficRuleConfiguration();
+        for (Entry<String, YamlTrafficStrategyConfiguration> entry : 
yamlConfig.getTrafficStrategies().entrySet()) {
+            YamlTrafficStrategyConfiguration strategyConfig = entry.getValue();
+            strategyConfig.setName(entry.getKey());
+            
result.getTrafficStrategies().add(strategySwapper.swapToObject(strategyConfig));
+        }
+        setAlgorithms(yamlConfig, result);
+        return result;
+    }
+    
+    private void setAlgorithms(final YamlTrafficRuleConfiguration yamlConfig, 
final TrafficRuleConfiguration ruleConfig) {
+        if (null != yamlConfig.getTrafficAlgorithms()) {
+            yamlConfig.getTrafficAlgorithms().forEach((key, value) -> 
ruleConfig.getTrafficAlgorithms().put(key, 
algorithmSwapper.swapToObject(value)));
+        }
+        if (null != yamlConfig.getLoadBalancers()) {
+            yamlConfig.getLoadBalancers().forEach((key, value) -> 
ruleConfig.getLoadBalancers().put(key, algorithmSwapper.swapToObject(value)));
+        }
+    }
+    
+    @Override
+    public Class<TrafficRuleConfiguration> getTypeClass() {
+        return TrafficRuleConfiguration.class;
+    }
+    
+    @Override
+    public String getRuleTagName() {
+        return "TRAFFIC";
+    }
+    
+    @Override
+    public int getOrder() {
+        return TrafficOrder.ORDER;
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficStrategyConfigurationYamlSwapper.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficStrategyConfigurationYamlSwapper.java
new file mode 100644
index 0000000..669cbf5
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficStrategyConfigurationYamlSwapper.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.yaml.swapper;
+
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.YamlConfigurationSwapper;
+import org.apache.shardingsphere.traffic.config.TrafficStrategyConfiguration;
+import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficStrategyConfiguration;
+
+/**
+ * TrafficRuleConfiguration rule configuration YAML swapper.
+ */
+public final class TrafficStrategyConfigurationYamlSwapper implements 
YamlConfigurationSwapper<YamlTrafficStrategyConfiguration, 
TrafficStrategyConfiguration> {
+    
+    @Override
+    public YamlTrafficStrategyConfiguration swapToYamlConfiguration(final 
TrafficStrategyConfiguration data) {
+        YamlTrafficStrategyConfiguration result = new 
YamlTrafficStrategyConfiguration();
+        result.setName(data.getName());
+        result.setLabels(data.getLabels());
+        result.setAlgorithmName(data.getAlgorithmName());
+        result.setLoadBalancerName(data.getLoadBalancerName());
+        return result;
+    }
+    
+    @Override
+    public TrafficStrategyConfiguration swapToObject(final 
YamlTrafficStrategyConfiguration yamlConfig) {
+        return new TrafficStrategyConfiguration(yamlConfig.getName(), 
yamlConfig.getLabels(), yamlConfig.getAlgorithmName(), 
yamlConfig.getLoadBalancerName());
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.global.DefaultGlobalRuleConfigurationBuilder
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.global.DefaultGlobalRuleConfigurationBuilder
new file mode 100644
index 0000000..2d5f746
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.global.DefaultGlobalRuleConfigurationBuilder
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.traffic.rule.builder.DefaultTrafficRuleConfigurationBuilder
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder
new file mode 100644
index 0000000..e35fc02
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.builder.global.GlobalRuleBuilder
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.traffic.rule.builder.TrafficRuleBuilder
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapper
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapper
new file mode 100644
index 0000000..40ded6c
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapper
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.traffic.yaml.swapper.TrafficRuleConfigurationYamlSwapper
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
new file mode 100644
index 0000000..3e71569
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/TrafficRuleTest.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.rule;
+
+import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class TrafficRuleTest {
+    
+    @Test
+    public void assertGetRuleType() {
+        TrafficRuleConfiguration ruleConfig = new TrafficRuleConfiguration();
+        TrafficRule authorityRule = new TrafficRule(ruleConfig);
+        assertThat(authorityRule.getType(), 
is(TrafficRule.class.getSimpleName()));
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilderTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilderTest.java
new file mode 100644
index 0000000..ee5117a
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/DefaultTrafficRuleConfigurationBuilderTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.rule.builder;
+
+import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+public final class DefaultTrafficRuleConfigurationBuilderTest {
+    
+    private final DefaultTrafficRuleConfigurationBuilder builder = new 
DefaultTrafficRuleConfigurationBuilder();
+    
+    @Test
+    public void assertBuild() {
+        TrafficRuleConfiguration configuration = builder.build();
+        assertNotNull(configuration);
+        assertThat(configuration.getTrafficStrategies().size(), is(0));
+        assertThat(configuration.getTrafficAlgorithms().size(), is(0));
+        assertThat(configuration.getLoadBalancers().size(), is(0));
+    }
+    
+    @Test
+    public void assertGetOrder() {
+        assertThat(builder.getOrder(), is(800));
+    }
+    
+    @Test
+    public void assertGetTypeClass() {
+        assertThat(builder.getTypeClass(), equalTo(TrafficRuleBuilder.class));
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilderTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilderTest.java
new file mode 100644
index 0000000..fab8768
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/rule/builder/TrafficRuleBuilderTest.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.rule.builder;
+
+import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class TrafficRuleBuilderTest {
+    
+    private final TrafficRuleBuilder builder = new TrafficRuleBuilder();
+    
+    @Test
+    public void getOrderTest() {
+        assertThat(builder.getOrder(), is(800));
+    }
+    
+    @Test
+    public void getTypeClassTest() {
+        assertThat(TrafficRuleConfiguration.class, 
equalTo(builder.getTypeClass()));
+    }
+}
diff --git 
a/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapperTest.java
 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapperTest.java
new file mode 100644
index 0000000..8e8b234
--- /dev/null
+++ 
b/shardingsphere-kernel/shardingsphere-traffic/shardingsphere-traffic-core/src/test/java/org/apache/shardingsphere/traffic/yaml/swapper/TrafficRuleConfigurationYamlSwapperTest.java
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.traffic.yaml.swapper;
+
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import 
org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.traffic.config.TrafficRuleConfiguration;
+import org.apache.shardingsphere.traffic.config.TrafficStrategyConfiguration;
+import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficRuleConfiguration;
+import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficStrategyConfiguration;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class TrafficRuleConfigurationYamlSwapperTest {
+    
+    private final TrafficRuleConfigurationYamlSwapper swapper = new 
TrafficRuleConfigurationYamlSwapper();
+    
+    @Test
+    public void assertSwapToYamlConfiguration() {
+        YamlTrafficRuleConfiguration actual = 
swapper.swapToYamlConfiguration(createTrafficRuleConfiguration());
+        assertThat(actual.getTrafficStrategies().size(), is(1));
+        
assertTrue(actual.getTrafficStrategies().containsKey("group_by_traffic"));
+        assertThat(actual.getTrafficAlgorithms().size(), is(1));
+        
assertTrue(actual.getTrafficAlgorithms().containsKey("group_by_algorithm"));
+        assertThat(actual.getLoadBalancers().size(), is(1));
+        assertTrue(actual.getLoadBalancers().containsKey("random"));
+    }
+    
+    private TrafficRuleConfiguration createTrafficRuleConfiguration() {
+        TrafficRuleConfiguration result = new TrafficRuleConfiguration();
+        result.getTrafficStrategies().add(new 
TrafficStrategyConfiguration("group_by_traffic", Arrays.asList("OLTP", "OLAP"), 
"group_by_algorithm", "random"));
+        result.getTrafficAlgorithms().put("group_by_algorithm", 
createTrafficAlgorithm());
+        result.getLoadBalancers().put("random", createLoadBalancer());
+        return result;
+    }
+    
+    private ShardingSphereAlgorithmConfiguration createTrafficAlgorithm() {
+        ShardingSphereAlgorithmConfiguration result = 
mock(ShardingSphereAlgorithmConfiguration.class);
+        when(result.getType()).thenReturn("SIMPLE");
+        return result;
+    }
+    
+    private ShardingSphereAlgorithmConfiguration createLoadBalancer() {
+        ShardingSphereAlgorithmConfiguration result = 
mock(ShardingSphereAlgorithmConfiguration.class);
+        when(result.getType()).thenReturn("RANDOM");
+        return result;
+    }
+    
+    @Test
+    public void assertSwapToObject() {
+        TrafficRuleConfiguration actual = 
swapper.swapToObject(createYamlTrafficRuleConfiguration());
+        assertThat(actual.getTrafficStrategies().size(), is(1));
+        TrafficStrategyConfiguration strategyConfig = 
actual.getTrafficStrategies().iterator().next();
+        assertThat(strategyConfig.getName(), is("group_by_traffic"));
+        assertThat(strategyConfig.getLabels(), is(Arrays.asList("OLTP", 
"OLAP")));
+        assertThat(strategyConfig.getAlgorithmName(), 
is("group_by_algorithm"));
+        assertThat(strategyConfig.getLoadBalancerName(), is("random"));
+        assertThat(actual.getTrafficAlgorithms().size(), is(1));
+        
assertTrue(actual.getTrafficAlgorithms().containsKey("group_by_algorithm"));
+        assertThat(actual.getLoadBalancers().size(), is(1));
+        assertTrue(actual.getLoadBalancers().containsKey("random"));
+    }
+    
+    private YamlTrafficRuleConfiguration createYamlTrafficRuleConfiguration() {
+        YamlTrafficStrategyConfiguration trafficStrategyConfig = new 
YamlTrafficStrategyConfiguration();
+        trafficStrategyConfig.setLabels(Arrays.asList("OLTP", "OLAP"));
+        trafficStrategyConfig.setAlgorithmName("group_by_algorithm");
+        trafficStrategyConfig.setLoadBalancerName("random");
+        YamlTrafficRuleConfiguration result = new 
YamlTrafficRuleConfiguration();
+        result.getTrafficStrategies().put("group_by_traffic", 
trafficStrategyConfig);
+        result.getTrafficAlgorithms().put("group_by_algorithm", 
createYamlTrafficAlgorithm());
+        result.getLoadBalancers().put("random", createYamlLoadBalancer());
+        return result;
+    }
+    
+    private YamlShardingSphereAlgorithmConfiguration 
createYamlTrafficAlgorithm() {
+        YamlShardingSphereAlgorithmConfiguration result = 
mock(YamlShardingSphereAlgorithmConfiguration.class);
+        when(result.getType()).thenReturn("SIMPLE");
+        return result;
+    }
+    
+    private YamlShardingSphereAlgorithmConfiguration createYamlLoadBalancer() {
+        YamlShardingSphereAlgorithmConfiguration result = 
mock(YamlShardingSphereAlgorithmConfiguration.class);
+        when(result.getType()).thenReturn("RANDOM");
+        return result;
+    }
+    
+    @Test
+    public void assertGetTypeClass() {
+        assertThat(swapper.getTypeClass(), 
equalTo(TrafficRuleConfiguration.class));
+    }
+    
+    @Test
+    public void assertGetRuleTagName() {
+        assertThat(swapper.getRuleTagName(), is("TRAFFIC"));
+    }
+    
+    @Test
+    public void assertGetOrder() {
+        assertThat(swapper.getOrder(), is(800));
+    }
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml 
b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
index 62ee812..064f3e6 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/pom.xml
@@ -124,6 +124,11 @@
         </dependency>
         <dependency>
             <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-traffic-core</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
             <artifactId>shardingsphere-transaction-xa-core</artifactId>
             <version>${project.parent.version}</version>
         </dependency>

Reply via email to