You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by al...@apache.org on 2023/03/06 08:37:20 UTC

[dubbo-samples] branch master updated: Fix notify samples (#770)

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

albumenj 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 fcfbfd3f Fix notify samples (#770)
fcfbfd3f is described below

commit fcfbfd3f152ee88aaf3828cd0427f6781f72bf69
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Mon Mar 6 16:37:10 2023 +0800

    Fix notify samples (#770)
---
 .../dubbo-samples-notify/case-configuration.yml    |  35 ++-
 .../src/main/resources/application.yml             |  20 +-
 .../src/main/resources/dubbo.properties            |  17 +-
 .../dubbo/samples/notify/consumer/NotifyIT.java    |   5 +-
 .../samples/notify/provider/EmbeddedZooKeeper.java | 255 ---------------------
 .../notify/provider/ProviderApplication.java       |   2 +-
 .../src/main/resources/application.yml             |   5 +-
 7 files changed, 51 insertions(+), 288 deletions(-)

diff --git a/2-advanced/dubbo-samples-notify/case-configuration.yml b/2-advanced/dubbo-samples-notify/case-configuration.yml
index 324783fa..84aea320 100644
--- a/2-advanced/dubbo-samples-notify/case-configuration.yml
+++ b/2-advanced/dubbo-samples-notify/case-configuration.yml
@@ -14,11 +14,34 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from: app-builtin-zookeeper.yml
+services:
+  zookeeper:
+    image: zookeeper:latest
 
-props:
-  project_name: dubbo-samples-notify
-  main_class: org.apache.dubbo.samples.notify.provider.ProviderApplication
-  zookeeper_port: 2181
-  dubbo_port: 20880
+  dubbo-samples-notify-provider:
+    type: app
+    basedir: dubbo-samples-notify-provider
+    mainClass: org.apache.dubbo.samples.notify.provider.ProviderApplication
+    systemProps:
+      - zookeeper.address=zookeeper
+    waitPortsBeforeRun:
+      - zookeeper:2181
+    checkPorts:
+      - 20880
+    checkLog: "Current Spring Boot Application is await"
+    depends_on:
+      - zookeeper
 
+  dubbo-samples-notify-test:
+    type: test
+    basedir: dubbo-samples-notify-consumer
+    tests:
+      - "**/*IT.class"
+    systemProps:
+      - zookeeper.address=zookeeper
+    waitPortsBeforeRun:
+      - zookeeper:2181
+      - dubbo-samples-notify-provider:20880
+    depends_on:
+      - zookeeper
+      - dubbo-samples-notify-provider
diff --git a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/main/resources/application.yml b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/main/resources/application.yml
index f36a439c..9175fb45 100644
--- a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/main/resources/application.yml
+++ b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/main/resources/application.yml
@@ -14,14 +14,12 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-#dubbo:
-#  application:
-#    name: dubbo-samples-notify-consumer
-#  protocol:
-#    name: dubbo
-#    port: -1
-#  registry:
-#    address: zookeeper://${zookeeper.address:127.0.0.1}:2181
-#dubbo:
-#  reference:
-#    org.apache.dubbo.samples.notify.DemoService.sayHello.onreturn: notify.onReturn
\ No newline at end of file
+dubbo:
+  application:
+    name: dubbo-samples-notify-consumer
+    logger: slf4j
+  protocol:
+    name: dubbo
+    port: -1
+  registry:
+    address: zookeeper://${zookeeper.address:127.0.0.1}:2181
diff --git a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/main/resources/dubbo.properties b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/main/resources/dubbo.properties
index 41953394..741f3d9d 100644
--- a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/main/resources/dubbo.properties
+++ b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/main/resources/dubbo.properties
@@ -14,15 +14,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-#dubbo.protocol.port=20880
-dubbo.application.name=dubbo-samples-notify-consumer
-dubbo.protocol.name=dubbo
-dubbo.protocol.port=20082
-dubbo.registry.address=zookeeper://127.0.0.1:2181
 
-dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.name=sayHello
-dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.timeout=7000
-dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.oninvoke=notify.onInvoke
-dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.onreturn=notify.onReturn
-dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.onthrow=notify.onThrow
-dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.0.callback=true
+#dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.name=sayHello
+#dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.timeout=7000
+#dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.oninvoke=notify.onInvoke
+#dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.onreturn=notify.onReturn
+#dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.onthrow=notify.onThrow
+#dubbo.reference.org.apache.dubbo.samples.notify.DemoService.sayHello.0.callback=true
diff --git a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/test/java/org/apache/dubbo/samples/notify/consumer/NotifyIT.java b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/test/java/org/apache/dubbo/samples/notify/consumer/NotifyIT.java
index a688c4f5..04f71948 100644
--- a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/test/java/org/apache/dubbo/samples/notify/consumer/NotifyIT.java
+++ b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-consumer/src/test/java/org/apache/dubbo/samples/notify/consumer/NotifyIT.java
@@ -20,6 +20,7 @@ package org.apache.dubbo.samples.notify.consumer;
 import org.apache.dubbo.config.annotation.DubboReference;
 import org.apache.dubbo.config.annotation.Method;
 import org.apache.dubbo.samples.notify.DemoService;
+
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -47,7 +48,7 @@ public class NotifyIT {
     public void testOnReturn() throws Exception {
         int id = 2;
         demoService.sayHello(id);
-        for (int i = 0; i < 10; i++) {
+        for (int i = 0; i < 100; i++) {
             if (!notify.ret.containsKey(id)) {
                 Thread.sleep(200);
             } else {
@@ -67,7 +68,7 @@ public class NotifyIT {
             // ignore
         }
 
-        for (int i = 0; i < 10; i++) {
+        for (int i = 0; i < 100; i++) {
             if (!notify.ret.containsKey(id)) {
                 Thread.sleep(200);
             } else {
diff --git a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/java/org/apache/dubbo/samples/notify/provider/EmbeddedZooKeeper.java b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/java/org/apache/dubbo/samples/notify/provider/EmbeddedZooKeeper.java
deleted file mode 100644
index 357f6fca..00000000
--- a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/java/org/apache/dubbo/samples/notify/provider/EmbeddedZooKeeper.java
+++ /dev/null
@@ -1,255 +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.notify.provider;
-
-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
- *
- * Helper class to start an embedded instance of standalone (non clustered) ZooKeeper.
- *
- * 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);
-                }
-            }
-        }
-    }
-
-}
diff --git a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/java/org/apache/dubbo/samples/notify/provider/ProviderApplication.java b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/java/org/apache/dubbo/samples/notify/provider/ProviderApplication.java
index 5ccf8fa2..a537f636 100644
--- a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/java/org/apache/dubbo/samples/notify/provider/ProviderApplication.java
+++ b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/java/org/apache/dubbo/samples/notify/provider/ProviderApplication.java
@@ -18,6 +18,7 @@
 package org.apache.dubbo.samples.notify.provider;
 
 import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
+
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@@ -25,7 +26,6 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
 @EnableDubbo
 public class ProviderApplication {
     public static void main(String[] args) {
-        new EmbeddedZooKeeper(2181, false).start();
         SpringApplication.run(ProviderApplication.class, args);
     }
 
diff --git a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/resources/application.yml b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/resources/application.yml
index c209cd79..07d8a504 100644
--- a/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/resources/application.yml
+++ b/2-advanced/dubbo-samples-notify/dubbo-samples-notify-provider/src/main/resources/application.yml
@@ -17,8 +17,9 @@
 dubbo:
   application:
     name: dubbo-samples-notify-provider
+    logger: slf4j
   protocol:
     name: dubbo
-    port: 20080
+    port: 20880
   registry:
-    address: zookeeper://${zookeeper.address:127.0.0.1}:2181
\ No newline at end of file
+    address: zookeeper://${zookeeper.address:127.0.0.1}:2181


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