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 06:42:56 UTC
[dubbo-samples] branch master updated: integration test for
dubbo-samples-nacos-configcenter
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 f00a713 integration test for dubbo-samples-nacos-configcenter
f00a713 is described below
commit f00a713e4d19541fd4632bfaafafe2e375eb8154
Author: Ian Luo <ia...@gmail.com>
AuthorDate: Fri Aug 9 14:42:28 2019 +0800
integration test for dubbo-samples-nacos-configcenter
---
.../dubbo-samples-nacos-configcenter/README.md | 16 ++
.../dubbo-samples-nacos-configcenter/pom.xml | 36 +--
.../dubbo/samples/configcenter/BasicConsumer.java | 6 +-
.../dubbo/samples/configcenter/BasicProvider.java | 11 +-
.../samples/configcenter/EmbeddedZooKeeper.java | 250 ---------------------
.../samples/configcenter/util/NacosUtils.java | 59 +++++
.../src/main/resources/config-center.properties | 5 +-
.../src/main/resources/docker/docker-compose.yml | 10 +
.../resources/spring/configcenter-consumer.xml | 2 +-
.../resources/spring/configcenter-provider.xml | 4 +-
.../dubbo/samples/configcenter/DemoServiceIT.java | 2 +-
11 files changed, 124 insertions(+), 277 deletions(-)
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/README.md b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/README.md
new file mode 100644
index 0000000..903ac1a
--- /dev/null
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/README.md
@@ -0,0 +1,16 @@
+## Steps to run the samples
+
+1. Start Nacos locally
+
+ ```
+ cd src/main/resources/docker
+ docker-compose up
+ ```
+
+2. Run Dubbo provider demo `org.apache.dubbo.samples.configcenter.BasicProvider`
+
+3. Run Dubbo consumer demo `org.apache.dubbo.samples.configcenter.BasicConsumer`, verify that standard ouput has the following content on the consumer side:
+
+ ```
+ result: hello, nacos
+ ```
\ No newline at end of file
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/pom.xml b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/pom.xml
index b768b85..b3b2f98 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/pom.xml
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/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.configcenter.BasicProvider</main-class>
</properties>
@@ -64,13 +64,6 @@
</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>
<version>${junit.version}</version>
@@ -87,7 +80,7 @@
<profiles>
<profile>
- <id>dubbo-integration-test-disabled</id>
+ <id>dubbo-integration-test</id>
<build>
<plugins>
<plugin>
@@ -120,15 +113,11 @@
</to>
<container>
<mainClass>${main-class}</mainClass>
- <ports>
- <port>${dubbo.port}</port>
- <port>${zookeeper.port}</port>
- </ports>
<environment>
<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>
@@ -149,11 +138,26 @@
<configuration>
<images>
<image>
+ <name>nacos/nacos-server:latest</name>
+ <run>
+ <ports>
+ <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>
<name>${image.name}</name>
<run>
<ports>
<port>${dubbo.port}:${dubbo.port}</port>
- <port>${zookeeper.port}:${zookeeper.port}</port>
</ports>
<wait>
<log>dubbo service started</log>
@@ -192,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-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/BasicConsumer.java b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/BasicConsumer.java
index 567d6e9..fbaa836 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/BasicConsumer.java
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/BasicConsumer.java
@@ -28,8 +28,8 @@ public class BasicConsumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/configcenter-consumer.xml");
context.start();
- DemoService demoService = (DemoService) context.getBean("demoService");
- String hello = demoService.sayHello("world");
- System.out.println(hello);
+ DemoService demoService = context.getBean("demoService", DemoService.class);
+ String hello = demoService.sayHello("nacos");
+ System.out.println("result: " + hello);
}
}
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/BasicProvider.java b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/BasicProvider.java
index 3ab3cc6..8a4f879 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/BasicProvider.java
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/BasicProvider.java
@@ -19,15 +19,22 @@
package org.apache.dubbo.samples.configcenter;
+import org.apache.dubbo.samples.configcenter.util.NacosUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.concurrent.CountDownLatch;
public class BasicProvider {
+ static {
+ try {
+ NacosUtils.writeDubboProperties();
+ Thread.sleep(1000);
+ } catch (Throwable t) {
+ // ignore
+ }
+ }
public static void main(String[] args) throws Exception {
- new EmbeddedZooKeeper(2181, false).start();
-
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/configcenter-provider.xml");
context.registerShutdownHook();
context.start();
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/EmbeddedZooKeeper.java b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/EmbeddedZooKeeper.java
deleted file mode 100644
index e2013f3..0000000
--- a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/EmbeddedZooKeeper.java
+++ /dev/null
@@ -1,250 +0,0 @@
-/*
- * Copyright 2014 the original author or authors.
- *
- * Licensed 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.configcenter;
-
-import org.apache.zookeeper.server.ServerConfig;
-import org.apache.zookeeper.server.ZooKeeperServerMain;
-import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.context.SmartLifecycle;
-import org.springframework.util.ErrorHandler;
-import org.springframework.util.SocketUtils;
-
-import java.io.File;
-import java.lang.reflect.Method;
-import java.util.Properties;
-import java.util.UUID;
-
-/**
- * from: https://github.com/spring-projects/spring-xd/blob/v1.3.1.RELEASE/spring-xd-dirt/src/main/java/org/springframework/xd/dirt/zookeeper/ZooKeeperUtils.java
- * <p>
- * Helper class to start an embedded instance of standalone (non clustered) ZooKeeper.
- * <p>
- * NOTE: at least an external standalone server (if not an ensemble) are recommended, even for
- * {@link org.springframework.xd.dirt.server.singlenode.SingleNodeApplication}
- *
- * @author Patrick Peralta
- * @author Mark Fisher
- * @author David Turanski
- */
-public class EmbeddedZooKeeper implements SmartLifecycle {
-
- /**
- * Logger.
- */
- private static final Logger logger = LoggerFactory.getLogger(EmbeddedZooKeeper.class);
-
- /**
- * ZooKeeper client port. This will be determined dynamically upon startup.
- */
- private final int clientPort;
-
- /**
- * Whether to auto-start. Default is true.
- */
- private boolean autoStartup = true;
-
- /**
- * Lifecycle phase. Default is 0.
- */
- private int phase = 0;
-
- /**
- * Thread for running the ZooKeeper server.
- */
- private volatile Thread zkServerThread;
-
- /**
- * ZooKeeper server.
- */
- private volatile ZooKeeperServerMain zkServer;
-
- /**
- * {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread.
- */
- private ErrorHandler errorHandler;
-
- private boolean daemon = true;
-
- /**
- * Construct an EmbeddedZooKeeper with a random port.
- */
- public EmbeddedZooKeeper() {
- clientPort = SocketUtils.findAvailableTcpPort();
- }
-
- /**
- * Construct an EmbeddedZooKeeper with the provided port.
- *
- * @param clientPort port for ZooKeeper server to bind to
- */
- public EmbeddedZooKeeper(int clientPort, boolean daemon) {
- this.clientPort = clientPort;
- this.daemon = daemon;
- }
-
- /**
- * Returns the port that clients should use to connect to this embedded server.
- *
- * @return dynamically determined client port
- */
- public int getClientPort() {
- return this.clientPort;
- }
-
- /**
- * Specify whether to start automatically. Default is true.
- *
- * @param autoStartup whether to start automatically
- */
- public void setAutoStartup(boolean autoStartup) {
- this.autoStartup = autoStartup;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean isAutoStartup() {
- return this.autoStartup;
- }
-
- /**
- * Specify the lifecycle phase for the embedded server.
- *
- * @param phase the lifecycle phase
- */
- public void setPhase(int phase) {
- this.phase = phase;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int getPhase() {
- return this.phase;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean isRunning() {
- return (zkServerThread != null);
- }
-
- /**
- * Start the ZooKeeper server in a background thread.
- * <p>
- * Register an error handler via {@link #setErrorHandler} in order to handle
- * any exceptions thrown during startup or execution.
- */
- @Override
- public synchronized void start() {
- if (zkServerThread == null) {
- zkServerThread = new Thread(new ServerRunnable(), "ZooKeeper Server Starter");
- zkServerThread.setDaemon(daemon);
- zkServerThread.start();
- }
- }
-
- /**
- * Shutdown the ZooKeeper server.
- */
- @Override
- public synchronized void stop() {
- if (zkServerThread != null) {
- // The shutdown method is protected...thus this hack to invoke it.
- // This will log an exception on shutdown; see
- // https://issues.apache.org/jira/browse/ZOOKEEPER-1873 for details.
- try {
- Method shutdown = ZooKeeperServerMain.class.getDeclaredMethod("shutdown");
- shutdown.setAccessible(true);
- shutdown.invoke(zkServer);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
-
- // It is expected that the thread will exit after
- // the server is shutdown; this will block until
- // the shutdown is complete.
- try {
- zkServerThread.join(5000);
- zkServerThread = null;
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- logger.warn("Interrupted while waiting for embedded ZooKeeper to exit");
- // abandoning zk thread
- zkServerThread = null;
- }
- }
- }
-
- /**
- * Stop the server if running and invoke the callback when complete.
- */
- @Override
- public void stop(Runnable callback) {
- stop();
- callback.run();
- }
-
- /**
- * Provide an {@link ErrorHandler} to be invoked if an Exception is thrown from the ZooKeeper server thread. If none
- * is provided, only error-level logging will occur.
- *
- * @param errorHandler the {@link ErrorHandler} to be invoked
- */
- public void setErrorHandler(ErrorHandler errorHandler) {
- this.errorHandler = errorHandler;
- }
-
- /**
- * Runnable implementation that starts the ZooKeeper server.
- */
- private class ServerRunnable implements Runnable {
-
- @Override
- public void run() {
- try {
- Properties properties = new Properties();
- File file = new File(System.getProperty("java.io.tmpdir")
- + File.separator + UUID.randomUUID());
- file.deleteOnExit();
- properties.setProperty("dataDir", file.getAbsolutePath());
- properties.setProperty("clientPort", String.valueOf(clientPort));
-
- QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();
- quorumPeerConfig.parseProperties(properties);
-
- zkServer = new ZooKeeperServerMain();
- ServerConfig configuration = new ServerConfig();
- configuration.readFrom(quorumPeerConfig);
-
- zkServer.runFromConfig(configuration);
- } catch (Exception e) {
- if (errorHandler != null) {
- errorHandler.handleError(e);
- } else {
- logger.error("Exception running embedded ZooKeeper", e);
- }
- }
- }
- }
-
-}
\ No newline at end of file
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/util/NacosUtils.java b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/util/NacosUtils.java
new file mode 100644
index 0000000..4f858ca
--- /dev/null
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/java/org/apache/dubbo/samples/configcenter/util/NacosUtils.java
@@ -0,0 +1,59 @@
+/*
+ * 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.configcenter.util;
+
+import com.alibaba.nacos.api.NacosFactory;
+import com.alibaba.nacos.api.PropertyKeyConst;
+import com.alibaba.nacos.api.config.ConfigService;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.util.Properties;
+
+public class NacosUtils {
+ public static void main(String[] args) throws Throwable {
+ writeDubboProperties();
+ }
+
+ public static void writeDubboProperties() throws Throwable {
+ String serverAddr = System.getProperty("nacos.address", "localhost");
+ String dataId = "dubbo.properties";
+ String group = "dubbo";
+ Properties properties = new Properties();
+ properties.put(PropertyKeyConst.SERVER_ADDR, serverAddr);
+ ConfigService configService = NacosFactory.createConfigService(properties);
+
+ StringBuilder content = new StringBuilder();
+ File file = new File(NacosUtils.class.getClassLoader().getResource("config-center.properties").getFile());
+ try (FileReader reader = new FileReader(file)) {
+ BufferedReader br = new BufferedReader(reader);
+ String line;
+ while ((line = br.readLine()) != null) {
+ if (line.startsWith("dubbo.registry.address=")) {
+ line = "dubbo.registry.address=nacos://" + serverAddr + ":8848";
+ }
+ content.append(line).append("\n");
+ }
+ }
+
+ if (configService.publishConfig(dataId, group, content.toString())) {
+ System.out.println("write " + dataId + ":" + group + " successfully.");
+ }
+ }
+}
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/config-center.properties b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/config-center.properties
index 6230f4a..e71a108 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/config-center.properties
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/config-center.properties
@@ -14,8 +14,9 @@
# limitations under the License.
# Add the following global external properties to `/dubbo/config/dubbo/dubbo.properties`
dubbo.registry.address=nacos://127.0.0.1:8848
-dubbo.protocol.port=-1
-dubbo.registry.simplified=true
+dubbo.protocol.port=20880
+# FIXME: https://github.com/apache/dubbo/issues/4780
+dubbo.registry.simplified=false
# Optional. Add the following application(Consumer) specific external properties
# to `/dubbo/config/configcenter-consumer/dubbo.properties`
dubbo.consumer.timeout=6666
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/docker/docker-compose.yml b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/docker/docker-compose.yml
new file mode 100644
index 0000000..dafcdc4
--- /dev/null
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/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-configcenter/src/main/resources/spring/configcenter-consumer.xml b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/spring/configcenter-consumer.xml
index cb13e9d..7a3350f 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/spring/configcenter-consumer.xml
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/spring/configcenter-consumer.xml
@@ -26,7 +26,7 @@
<context:property-placeholder/>
- <dubbo:application name="configcenter-consumer"/>
+ <dubbo:application name="configcenter-nacos-consumer"/>
<dubbo:config-center highest-priority="false" address="nacos://${nacos.address:127.0.0.1}:8848"/>
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/spring/configcenter-provider.xml b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/spring/configcenter-provider.xml
index 691af59..887da17 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/spring/configcenter-provider.xml
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/main/resources/spring/configcenter-provider.xml
@@ -21,9 +21,9 @@
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
-
<context:property-placeholder/>
- <dubbo:application name="configcenter-provider"/>
+
+ <dubbo:application name="configcenter-nacos-provider"/>
<dubbo:config-center address="nacos://${nacos.address:127.0.0.1}:8848"/>
<bean id="demoService" class="org.apache.dubbo.samples.configcenter.impl.DemoServiceImpl"/>
diff --git a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/test/java/org/apache/dubbo/samples/configcenter/DemoServiceIT.java b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/test/java/org/apache/dubbo/samples/configcenter/DemoServiceIT.java
index ccaf83f..2a2f989 100644
--- a/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/test/java/org/apache/dubbo/samples/configcenter/DemoServiceIT.java
+++ b/dubbo-samples-nacos/dubbo-samples-nacos-configcenter/src/test/java/org/apache/dubbo/samples/configcenter/DemoServiceIT.java
@@ -28,7 +28,7 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations = "classpath*:spring/configcenter-consumer.xml")
+@ContextConfiguration(locations = "classpath:spring/configcenter-consumer.xml")
public class DemoServiceIT {
@Autowired
@Qualifier("demoService")
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org