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:17:56 UTC

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

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 aaa1562  add integration test for dubbo-samples-nacos-override
aaa1562 is described below

commit aaa1562230aa5457ffec098db1bbdf395074911d
Author: Ian Luo <ia...@gmail.com>
AuthorDate: Fri Aug 9 23:17:21 2019 +0800

    add integration test for dubbo-samples-nacos-override
---
 .../dubbo-samples-nacos-override/pom.xml           | 28 +++++-----
 .../dubbo/samples/governance/util/NacosUtils.java  | 65 ++++++++++++++++++++++
 .../src/main/resources/docker/docker-compose.yml   | 10 ++++
 .../main/resources/spring/dubbo-demo-consumer.xml  |  2 +
 .../main/resources/spring/dubbo-demo-provider.xml  |  4 +-
 .../dubbo/samples/governance/DemoServiceIT.java    | 55 ++++++++++++++++++
 6 files changed, 149 insertions(+), 15 deletions(-)

diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-override/pom.xml b/dubbo-samples-nacos/dubbo-samples-nacos-override/pom.xml
index 37da9b2..1ae3f21 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-override/pom.xml
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-override/pom.xml
@@ -41,7 +41,7 @@
         <image.name>${artifactId}:${dubbo.version}</image.name>
         <java-image.name>openjdk:8</java-image.name>
         <dubbo.port>20880</dubbo.port>
-        <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>
@@ -196,7 +196,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-override/src/main/java/org/apache/dubbo/samples/governance/util/NacosUtils.java b/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/java/org/apache/dubbo/samples/governance/util/NacosUtils.java
new file mode 100644
index 0000000..d18a513
--- /dev/null
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/java/org/apache/dubbo/samples/governance/util/NacosUtils.java
@@ -0,0 +1,65 @@
+/*
+ * 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 serverAddr = System.getProperty("nacos.address", "localhost");
+    private static String DATAID = "org.apache.dubbo.samples.governance.api.DemoService::.configurators";
+    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("dubbo-override.yml")) {
+            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-override/src/main/resources/docker/docker-compose.yml b/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/resources/docker/docker-compose.yml
new file mode 100644
index 0000000..dafcdc4
--- /dev/null
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-override/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-override/src/main/resources/spring/dubbo-demo-consumer.xml b/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/resources/spring/dubbo-demo-consumer.xml
index e871db4..4f7d8d0 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/resources/spring/dubbo-demo-consumer.xml
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/resources/spring/dubbo-demo-consumer.xml
@@ -27,6 +27,8 @@
 
     <dubbo:application name="governance-serviceoverride-consumer"/>
 
+    <dubbo:registry address="nacos://${nacos.address:127.0.0.1}:8848"/>
+
     <dubbo:config-center address="nacos://${nacos.address:127.0.0.1}:8848"/>
 
     <dubbo:reference timeout="1000" id="demoService" check="false"
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/resources/spring/dubbo-demo-provider.xml b/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/resources/spring/dubbo-demo-provider.xml
index fa3166b..e407688 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/resources/spring/dubbo-demo-provider.xml
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-override/src/main/resources/spring/dubbo-demo-provider.xml
@@ -27,9 +27,11 @@
 
     <dubbo:application name="governance-serviceoverride-provider"/>
 
+    <dubbo:registry address="nacos://${nacos.address:127.0.0.1}:8848"/>
+
     <dubbo:config-center address="nacos://${nacos.address:127.0.0.1}:8848"/>
 
-    <dubbo:protocol name="dubbo" port="-1"/>
+    <dubbo:protocol name="dubbo" port="20880"/>
 
     <bean id="demoService" class="org.apache.dubbo.samples.governance.impl.DemoServiceImpl"/>
 
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-override/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java b/dubbo-samples-nacos/dubbo-samples-nacos-override/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java
new file mode 100644
index 0000000..9ac0a91
--- /dev/null
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-override/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java
@@ -0,0 +1,55 @@
+/*
+ * 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.rpc.RpcException;
+import org.apache.dubbo.samples.governance.api.DemoService;
+import org.apache.dubbo.samples.governance.util.NacosUtils;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = {"classpath:/spring/dubbo-demo-consumer.xml"})
+public class DemoServiceIT {
+    @Autowired
+    private DemoService demoService;
+
+    @Test(expected = RpcException.class)
+    public void testWithoutRule() throws Exception {
+        Thread.sleep(2000);
+        demoService.sayHello("world", 3000);
+    }
+
+    @Test
+    public void testWithAppRuleWithSufficientTimeout() throws Throwable {
+        try {
+            NacosUtils.writeAppRule();
+            Thread.sleep(2000);
+            Assert.assertTrue(demoService.sayHello("world", 1000).startsWith("Hello world"));
+        } finally {
+            NacosUtils.clearAppRule();
+        }
+    }
+}


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