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 2022/11/16 08:24:28 UTC
[dubbo-samples] branch master updated: Add router chain switch test (#574)
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 1c3ad31d Add router chain switch test (#574)
1c3ad31d is described below
commit 1c3ad31d99cef77745b5a3e8fd20c03d2215472f
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Wed Nov 16 16:24:22 2022 +0800
Add router chain switch test (#574)
---
.../case-configuration.yml | 23 ++
.../case-versions.conf | 23 ++
.../dubbo-samples-router-chain-switch/pom.xml | 91 ++++++++
.../apache/dubbo/samples/api/GreetingService.java | 23 ++
.../apache/dubbo/samples/provider/Application.java | 79 +++++++
.../samples/provider/GreetingServiceImpl.java | 33 +++
.../dubbo/samples/zookeeper/EmbeddedZooKeeper.java | 255 +++++++++++++++++++++
.../org/apache/dubbo/samples/DemoClientIT.java | 84 +++++++
.../org/apache/dubbo/samples/LongWaitRouter.java | 79 +++++++
.../dubbo/samples/LongWaitRouterFactory.java | 30 +++
...bbo.rpc.cluster.router.state.StateRouterFactory | 1 +
dubbo-samples-boundary-test/pom.xml | 1 +
12 files changed, 722 insertions(+)
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/case-configuration.yml b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/case-configuration.yml
new file mode 100644
index 00000000..048c292e
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/case-configuration.yml
@@ -0,0 +1,23 @@
+# 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.
+
+from: app-external-zookeeper.yml
+
+props:
+ project_name: dubbo-samples-router-chain-switch
+ main_class: org.apache.dubbo.samples.provider.Application
+ dubbo_port: 20880
+
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/case-versions.conf b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/case-versions.conf
new file mode 100644
index 00000000..f1355f39
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/case-versions.conf
@@ -0,0 +1,23 @@
+#
+#
+# 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.
+#
+
+
+# Supported component versions of the test case
+
+# Spring app
+dubbo.version=[ >=3.2.0 ]
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/pom.xml b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/pom.xml
new file mode 100644
index 00000000..630df959
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/pom.xml
@@ -0,0 +1,91 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+-->
+<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">
+ <parent>
+ <artifactId>dubbo-samples-boundary-test</artifactId>
+ <groupId>org.apache.dubbo</groupId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>dubbo-samples-router-chain-switch</artifactId>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ <dubbo.version>3.2.0-beta.2-SNAPSHOT</dubbo.version>
+ <junit.version>4.12</junit.version>
+ <source.level>1.8</source.level>
+ <target.level>1.8</target.level>
+ <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
+ </properties>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-bom</artifactId>
+ <version>${dubbo.version}</version>
+ <type>pom</type>
+ <scope>import</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-dependencies-zookeeper</artifactId>
+ <version>${dubbo.version}</version>
+ <type>pom</type>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-dependencies-zookeeper</artifactId>
+ <type>pom</type>
+ <version>${dubbo.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo</artifactId>
+ <version>${dubbo.version}</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>${maven-compiler-plugin.version}</version>
+ <configuration>
+ <source>${source.level}</source>
+ <target>${target.level}</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
\ No newline at end of file
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/api/GreetingService.java b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/api/GreetingService.java
new file mode 100644
index 00000000..d9ea138a
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/api/GreetingService.java
@@ -0,0 +1,23 @@
+/*
+ * 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.api;
+
+public interface GreetingService {
+
+ String sayHi();
+}
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/provider/Application.java b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/provider/Application.java
new file mode 100644
index 00000000..c8ddfb45
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/provider/Application.java
@@ -0,0 +1,79 @@
+/*
+ * 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.provider;
+
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ProtocolConfig;
+import org.apache.dubbo.config.RegistryConfig;
+import org.apache.dubbo.config.ServiceConfig;
+import org.apache.dubbo.config.bootstrap.DubboBootstrap;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+import org.apache.dubbo.samples.api.GreetingService;
+import org.apache.dubbo.samples.zookeeper.EmbeddedZooKeeper;
+
+import java.util.concurrent.CountDownLatch;
+
+public class Application {
+
+
+ public static void main(String[] args) throws Exception {
+ new EmbeddedZooKeeper(2181, false).start();
+
+ {
+ FrameworkModel frameworkModel = new FrameworkModel();
+
+ ApplicationConfig applicationConfig = new ApplicationConfig("first-dubbo-provider");
+ applicationConfig.setMetadataType("remote");
+
+ ServiceConfig<GreetingService> service = new ServiceConfig<>();
+ service.setInterface(GreetingService.class);
+ service.setRef(new GreetingServiceImpl("Main1"));
+ service.setProtocol(new ProtocolConfig(CommonConstants.DUBBO_PROTOCOL, 20880));
+
+ String zookeeperAddress = System.getProperty("zookeeper.address", "127.0.0.1");
+ DubboBootstrap.getInstance(frameworkModel.newApplication())
+ .application(applicationConfig)
+ .registry(new RegistryConfig("zookeeper://" + zookeeperAddress + ":" + "2181"))
+ .service(service)
+ .start();
+ }
+
+ {
+ FrameworkModel frameworkModel = new FrameworkModel();
+
+ ApplicationConfig applicationConfig = new ApplicationConfig("second-dubbo-provider");
+ applicationConfig.setMetadataType("remote");
+
+ ServiceConfig<GreetingService> service = new ServiceConfig<>();
+ service.setInterface(GreetingService.class);
+ service.setRef(new GreetingServiceImpl("Main1"));
+ service.setProtocol(new ProtocolConfig(CommonConstants.DUBBO_PROTOCOL, 20881));
+
+ String zookeeperAddress = System.getProperty("zookeeper.address", "127.0.0.1");
+ DubboBootstrap.getInstance(frameworkModel.newApplication())
+ .application(applicationConfig)
+ .registry(new RegistryConfig("zookeeper://" + zookeeperAddress + ":" + "2181"))
+ .service(service)
+ .start();
+ }
+
+ System.out.println("dubbo service started");
+ new CountDownLatch(1).await();
+ }
+}
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/provider/GreetingServiceImpl.java b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/provider/GreetingServiceImpl.java
new file mode 100644
index 00000000..29ff1ede
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/provider/GreetingServiceImpl.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.dubbo.samples.provider;
+
+import org.apache.dubbo.samples.api.GreetingService;
+
+public class GreetingServiceImpl implements GreetingService {
+ private final String name;
+
+ public GreetingServiceImpl(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public String sayHi() {
+ return name;
+ }
+}
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/zookeeper/EmbeddedZooKeeper.java b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/zookeeper/EmbeddedZooKeeper.java
new file mode 100644
index 00000000..17e14bfd
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/main/java/org/apache/dubbo/samples/zookeeper/EmbeddedZooKeeper.java
@@ -0,0 +1,255 @@
+/*
+ * 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.zookeeper;
+
+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/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/java/org/apache/dubbo/samples/DemoClientIT.java b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/java/org/apache/dubbo/samples/DemoClientIT.java
new file mode 100644
index 00000000..c87f9ae0
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/java/org/apache/dubbo/samples/DemoClientIT.java
@@ -0,0 +1,84 @@
+/*
+ * 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;
+
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ProtocolConfig;
+import org.apache.dubbo.config.ReferenceConfig;
+import org.apache.dubbo.config.RegistryConfig;
+import org.apache.dubbo.config.ServiceConfig;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.ModuleModel;
+import org.apache.dubbo.samples.api.GreetingService;
+import org.apache.dubbo.samples.provider.GreetingServiceImpl;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+public class DemoClientIT {
+
+ @Test
+ public void testInterfaceDiscovery() throws InterruptedException {
+ ApplicationModel applicationModel = ApplicationModel.defaultModel();
+ applicationModel.getApplicationConfigManager().setApplication(new ApplicationConfig("first-dubbo-consumer"));
+ ModuleModel moduleModel = applicationModel.newModule();
+ String zookeeperAddress = System.getProperty("zookeeper.address", "127.0.0.1");
+
+ ReferenceConfig<GreetingService> reference = new ReferenceConfig<>(moduleModel);
+ reference.setInterface(GreetingService.class);
+ reference.setRegistry(new RegistryConfig(
+ "zookeeper://" + zookeeperAddress + ":" + "2181?file.cache=false"));
+ GreetingService service = reference.get();
+
+ AtomicBoolean stop = new AtomicBoolean(false);
+
+ new Thread(() -> {
+ while (!stop.get()) {
+ try {
+ service.sayHi();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ }).start();
+
+ ServiceConfig<GreetingService> serviceConfig = new ServiceConfig<>(moduleModel);
+ serviceConfig.setInterface(GreetingService.class);
+ serviceConfig.setRef(new GreetingServiceImpl("Test"));
+ serviceConfig.setRegistry(new RegistryConfig(
+ "zookeeper://" + zookeeperAddress + ":" + "2181"));
+ serviceConfig.setProtocol(new ProtocolConfig(CommonConstants.DUBBO_PROTOCOL, NetUtils.getAvailablePort()));
+ serviceConfig.export();
+
+ while (!service.sayHi().equals("Test")) {
+ Thread.sleep(10);
+ }
+
+ try {
+ Assert.assertFalse(LongWaitRouter.isFoundFailed());
+ } finally {
+ stop.set(true);
+ LongWaitRouter.setEnd();
+ }
+ }
+
+
+}
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/java/org/apache/dubbo/samples/LongWaitRouter.java b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/java/org/apache/dubbo/samples/LongWaitRouter.java
new file mode 100644
index 00000000..2338eb18
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/java/org/apache/dubbo/samples/LongWaitRouter.java
@@ -0,0 +1,79 @@
+/*
+ * 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;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.utils.Holder;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.cluster.router.RouterSnapshotNode;
+import org.apache.dubbo.rpc.cluster.router.state.AbstractStateRouter;
+import org.apache.dubbo.rpc.cluster.router.state.BitList;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+
+public class LongWaitRouter<T> extends AbstractStateRouter<T> {
+ public LongWaitRouter(URL url) {
+ super(url);
+ }
+
+ private final AtomicReference<BitList<Invoker<T>>> expectedInvokers = new AtomicReference<>();
+
+ private static final AtomicBoolean foundFailed = new AtomicBoolean(false);
+ private static final AtomicBoolean end = new AtomicBoolean(false);
+
+ private static final AtomicInteger invokeCount = new AtomicInteger(0);
+
+
+
+ @Override
+ protected BitList<Invoker<T>> doRoute(BitList<Invoker<T>> invokers, URL url, Invocation invocation, boolean needToPrintMessage, Holder<RouterSnapshotNode<T>> routerSnapshotNodeHolder, Holder<String> messageHolder) throws RpcException {
+ invokeCount.incrementAndGet();
+ if (expectedInvokers.get() != null) {
+ if (expectedInvokers.get().getOriginList() != invokers.getOriginList()) {
+ foundFailed.set(true);
+ }
+ }
+ return invokers;
+ }
+
+ @Override
+ public void notify(BitList<Invoker<T>> invokers) {
+ if (expectedInvokers.get() != null && !end.get()) {
+ int i = invokeCount.get();
+ try {
+ while (invokeCount.get() - i < 1000 && !end.get()) {
+ Thread.sleep(1);
+ }
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ expectedInvokers.set(invokers);
+ }
+
+ public static boolean isFoundFailed() {
+ return foundFailed.get();
+ }
+
+ public static void setEnd() {
+ end.set(true);
+ }
+}
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/java/org/apache/dubbo/samples/LongWaitRouterFactory.java b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/java/org/apache/dubbo/samples/LongWaitRouterFactory.java
new file mode 100644
index 00000000..fe4c60db
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/java/org/apache/dubbo/samples/LongWaitRouterFactory.java
@@ -0,0 +1,30 @@
+/*
+ * 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;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.rpc.cluster.router.state.StateRouter;
+import org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory;
+
+@Activate(order = Integer.MIN_VALUE)
+public class LongWaitRouterFactory implements StateRouterFactory {
+ @Override
+ public <T> StateRouter<T> getRouter(Class<T> interfaceClass, URL url) {
+ return new LongWaitRouter<>(url);
+ }
+}
diff --git a/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory
new file mode 100644
index 00000000..04111dc9
--- /dev/null
+++ b/dubbo-samples-boundary-test/dubbo-samples-router-chain-switch/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory
@@ -0,0 +1 @@
+longwait=org.apache.dubbo.samples.LongWaitRouterFactory
\ No newline at end of file
diff --git a/dubbo-samples-boundary-test/pom.xml b/dubbo-samples-boundary-test/pom.xml
index a12eef18..858d5d7f 100644
--- a/dubbo-samples-boundary-test/pom.xml
+++ b/dubbo-samples-boundary-test/pom.xml
@@ -32,6 +32,7 @@
<module>dubbo-samples-sd-merge</module>
<module>dubbo-samples-nacos-merge</module>
<module>dubbo-samples-port-unification-netty3</module>
+ <module>dubbo-samples-router-chain-switch</module>
<module>dubbo-samples-empty-protection</module>
<module>dubbo-samples-empty-protection-nacos</module>
</modules>
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org