You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by il...@apache.org on 2019/08/09 15:32:21 UTC

[dubbo-samples] branch master updated: integration test for dubbo-samples-nacos-tagrouter

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

iluo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-samples.git


The following commit(s) were added to refs/heads/master by this push:
     new 4f4eb40  integration test for dubbo-samples-nacos-tagrouter
4f4eb40 is described below

commit 4f4eb406aba026ec7c063f8483279a1c199e90d5
Author: Ian Luo <ia...@gmail.com>
AuthorDate: Fri Aug 9 23:31:52 2019 +0800

    integration test for dubbo-samples-nacos-tagrouter
---
 .../dubbo-samples-nacos-tagrouter/pom.xml          | 28 ++++----
 .../dubbo/samples/governance/BasicConsumer.java    | 28 +++-----
 .../apache/dubbo/samples/governance/RuleUtil.java  | 82 ----------------------
 .../dubbo/samples/governance/util/NacosUtils.java  | 66 +++++++++++++++++
 .../src/main/resources/docker/docker-compose.yml   | 10 +++
 .../main/resources/spring/dubbo-demo-consumer.xml  |  2 +
 .../main/resources/spring/dubbo-demo-provider.xml  |  2 +
 .../dubbo/samples/governance/DemoServiceIT.java    | 13 ++--
 8 files changed, 111 insertions(+), 120 deletions(-)

diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/pom.xml b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/pom.xml
index 70ae66c..07c84f6 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/pom.xml
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/pom.xml
@@ -41,7 +41,7 @@
         <java-image.name>openjdk:8</java-image.name>
         <dubbo.port.1>20880</dubbo.port.1>
         <dubbo.port.2>20881</dubbo.port.2>
-        <zookeeper.port>2181</zookeeper.port>
+        <nacos.port>8848</nacos.port>
         <main-class>org.apache.dubbo.samples.governance.BasicProvider</main-class>
     </properties>
 
@@ -63,14 +63,6 @@
             <version>${dubbo.version}</version>
         </dependency>
 
-
-        <dependency>
-            <groupId>org.apache.dubbo</groupId>
-            <artifactId>dubbo-dependencies-zookeeper</artifactId>
-            <version>${dubbo.version}</version>
-            <type>pom</type>
-        </dependency>
-
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
@@ -88,7 +80,7 @@
 
     <profiles>
         <profile>
-            <id>dubbo-integration-test-disabled</id>
+            <id>dubbo-integration-test</id>
             <build>
                 <plugins>
                     <plugin>
@@ -125,7 +117,7 @@
                                     <DUBBO_IP_TO_REGISTRY>${dubbo-local-address}</DUBBO_IP_TO_REGISTRY>
                                 </environment>
                                 <jvmFlags>
-                                    <jvmFlag>-Dzookeeper.address=${dubbo-local-address}</jvmFlag>
+                                    <jvmFlag>-Dnacos.address=${dubbo-local-address}</jvmFlag>
                                 </jvmFlags>
                             </container>
                         </configuration>
@@ -146,11 +138,19 @@
                         <configuration>
                             <images>
                                 <image>
-                                    <name>zookeeper:latest</name>
+                                    <name>nacos/nacos-server:latest</name>
                                     <run>
                                         <ports>
-                                            <port>${zookeeper.port}:${zookeeper.port}</port>
+                                            <port>${nacos.port}:${nacos.port}</port>
                                         </ports>
+                                        <wait>
+                                            <time>30000</time>
+                                            <log>Nacos started successfully in stand alone mode</log>
+                                        </wait>
+                                        <env>
+                                            <PREFER_HOST_MODE>hostname</PREFER_HOST_MODE>
+                                            <MODE>standalone</MODE>
+                                        </env>
                                     </run>
                                 </image>
                                 <image>
@@ -213,7 +213,7 @@
                                 </goals>
                                 <configuration>
                                     <systemPropertyVariables>
-                                        <zookeeper.address>${dubbo-local-address}</zookeeper.address>
+                                        <nacos.address>${dubbo-local-address}</nacos.address>
                                     </systemPropertyVariables>
                                     <includes>
                                         <include>**/*IT.java</include>
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java
index 09f433d..c8ea4c1 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/BasicConsumer.java
@@ -36,25 +36,13 @@ public class BasicConsumer {
         DemoService demoService = context.getBean("demoService", DemoService.class);
         DemoService2 demoService2 = context.getBean("demoService2", DemoService2.class);
 
-        while (true) {
-            try {
-                RpcContext.getContext().setAttachment(Constants.TAG_KEY, "tag1");
-                String hello = demoService.sayHello("world");
-                System.out.println(hello);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-
-            try {
-                RpcContext.getContext().setAttachment(FORCE_USE_TAG, "true");
-                RpcContext.getContext().setAttachment(Constants.TAG_KEY, "tag2");
-                String hello2 = demoService2.sayHello("world again");
-                System.out.println(hello2);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-
-            Thread.sleep(1000);
-        }
+        RpcContext.getContext().setAttachment(Constants.TAG_KEY, "tag1");
+        String hello = demoService.sayHello("world");
+        System.out.println(hello);
+
+        RpcContext.getContext().setAttachment(FORCE_USE_TAG, "true");
+        RpcContext.getContext().setAttachment(Constants.TAG_KEY, "tag2");
+        String hello2 = demoService2.sayHello("world again");
+        System.out.println(hello2);
     }
 }
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java
deleted file mode 100644
index 53d4a2d..0000000
--- a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/RuleUtil.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * 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.dubbo.samples.governance;
-
-import org.apache.dubbo.common.utils.StringUtils;
-
-import org.apache.curator.framework.CuratorFramework;
-import org.apache.curator.framework.CuratorFrameworkFactory;
-import org.apache.curator.retry.ExponentialBackoffRetry;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-public class RuleUtil {
-    private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");
-    private static CuratorFramework client;
-
-    public static void main(String[] args) throws Exception {
-        initClient();
-        generateRule();
-        System.in.read();
-        deleteRule();
-    }
-
-    public static void initClient() {
-        client = CuratorFrameworkFactory.newClient(zookeeperHost + ":2181", 60 * 1000, 60 * 1000,
-                new ExponentialBackoffRetry(1000, 3));
-        client.start();
-    }
-
-    public static void generateRule() {
-        try (InputStream yamlStream = RuleUtil.class.getResourceAsStream("/dubbo-routers-tag.yml")) {
-            String path = "/dubbo/config/dubbo/governance-tagrouter-provider.tag-router";
-            if (client.checkExists().forPath(path) == null) {
-                client.create().creatingParentsIfNeeded().forPath(path);
-            }
-            setData(path, streamToString(yamlStream));
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-    public static void deleteRule() throws Exception {
-        String path = "/dubbo/config/dubbo/governance-tagrouter-provider.tag-router";
-        if (client.checkExists().forPath(path) == null) {
-            client.create().creatingParentsIfNeeded().forPath(path);
-        }
-        setData(path, "");
-    }
-
-    private static String streamToString(InputStream stream) throws IOException {
-        byte[] bytes = new byte[stream.available()];
-        stream.read(bytes);
-        return new String(bytes);
-    }
-
-    private static void setData(String path, String data) throws Exception {
-        client.setData().forPath(path, data.getBytes());
-    }
-
-    private static String pathToKey(String path) {
-        if (StringUtils.isEmpty(path)) {
-            return path;
-        }
-        return path.replace("/dubbo/config/", "").replaceAll("/", ".");
-    }
-
-}
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/util/NacosUtils.java b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/util/NacosUtils.java
new file mode 100644
index 0000000..f80570e
--- /dev/null
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/java/org/apache/dubbo/samples/governance/util/NacosUtils.java
@@ -0,0 +1,66 @@
+/*
+ * 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.dubbo.samples.governance.util;
+
+import com.alibaba.nacos.api.NacosFactory;
+import com.alibaba.nacos.api.PropertyKeyConst;
+import com.alibaba.nacos.api.config.ConfigService;
+import com.alibaba.nacos.api.exception.NacosException;
+import org.apache.commons.io.IOUtils;
+
+import java.io.InputStream;
+import java.util.Properties;
+
+public class NacosUtils {
+    private static String YAML = "dubbo-routers-tag.yml";
+    private static String serverAddr = System.getProperty("nacos.address", "localhost");
+    private static String DATAID = "governance-tagrouter-provider.tag-router";
+    private static String GROUP = "dubbo";
+
+    private static ConfigService configService;
+
+    static {
+        Properties properties = new Properties();
+        properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
+        try {
+            configService = NacosFactory.createConfigService(properties);
+        } catch (NacosException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void main(String[] args) throws Throwable {
+        writeAppRule();
+    }
+
+
+    public static void writeAppRule() throws Throwable {
+        try (InputStream is = NacosUtils.class.getClassLoader().getResourceAsStream(YAML)) {
+            String content = IOUtils.toString(is);
+            if (configService.publishConfig(DATAID, GROUP, content)) {
+                System.out.println("write " + DATAID + ":" + GROUP + " successfully.");
+            }
+        }
+    }
+
+    public static void clearAppRule() throws Throwable {
+        if (configService.removeConfig(DATAID, GROUP)) {
+            System.out.println("remove " + DATAID + ":" + GROUP + " successfully.");
+        }
+    }
+}
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/docker/docker-compose.yml b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/docker/docker-compose.yml
new file mode 100644
index 0000000..dafcdc4
--- /dev/null
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/docker/docker-compose.yml
@@ -0,0 +1,10 @@
+version: "2"
+services:
+  nacos:
+    image: nacos/nacos-server:latest
+    container_name: nacos-standalone
+    environment:
+      - PREFER_HOST_MODE=hostname
+      - MODE=standalone
+    ports:
+      - "8848:8848"
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/spring/dubbo-demo-consumer.xml b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/spring/dubbo-demo-consumer.xml
index c18c885..e5f3293 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/spring/dubbo-demo-consumer.xml
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/spring/dubbo-demo-consumer.xml
@@ -27,6 +27,8 @@
 
     <dubbo:application name="governance-tagrouter-consumer"/>
 
+    <dubbo:registry address="nacos://${nacos.address:127.0.0.1}:8848"/>
+
     <dubbo:config-center highest-priority="true" address="nacos://${nacos.address:127.0.0.1}:8848"/>
 
     <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.governance.api.DemoService"/>
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/spring/dubbo-demo-provider.xml b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/spring/dubbo-demo-provider.xml
index df375b1..8767871 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/spring/dubbo-demo-provider.xml
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/main/resources/spring/dubbo-demo-provider.xml
@@ -29,6 +29,8 @@
 
     <dubbo:protocol name="dubbo" port="${dubbo.port:-1}" heartbeat="10000"/>
 
+    <dubbo:registry address="nacos://${nacos.address:127.0.0.1}:8848"/>
+
     <dubbo:config-center highest-priority="true" address="nacos://${nacos.address:127.0.0.1}:8848"/>
 
     <bean id="demoService" class="org.apache.dubbo.samples.governance.impl.DemoServiceImpl"/>
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java
index ff66e5e..8bf4c30 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-tagrouter/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java
@@ -21,7 +21,8 @@ import org.apache.dubbo.rpc.RpcContext;
 import org.apache.dubbo.rpc.RpcException;
 import org.apache.dubbo.rpc.cluster.Constants;
 import org.apache.dubbo.samples.governance.api.DemoService;
-
+import org.apache.dubbo.samples.governance.util.NacosUtils;
+import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -41,12 +42,16 @@ public class DemoServiceIT {
     private DemoService demoService;
 
     @BeforeClass
-    public static void setUp() throws Exception {
-        RuleUtil.initClient();
-        RuleUtil.generateRule();
+    public static void setUp() throws Throwable {
+        NacosUtils.writeAppRule();
         Thread.sleep(1000);
     }
 
+    @AfterClass
+    public static void tearDown() throws Throwable {
+        NacosUtils.clearAppRule();
+    }
+
     @Test
     public void testDemoService1() throws Exception {
         for (int i = 0; i < 10; i++) {


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org