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/01/06 14:08:46 UTC

[dubbo-samples] branch master updated: Enhance app level override case (#699)

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 0ad36f2d Enhance app level override case (#699)
0ad36f2d is described below

commit 0ad36f2da65dce78c935d626e658fb07c67ebb5b
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Fri Jan 6 22:08:40 2023 +0800

    Enhance app level override case (#699)
    
    * Enhance app level override case
    
    * Fix pom
---
 .../dubbo-samples-applevel-override/pom.xml        |  11 +-
 .../dubbo/samples/governance/api/DemoService2.java |  26 ++++
 .../samples/governance/impl/DemoService2Impl.java  |  39 ++++++
 .../main/resources/spring/dubbo-demo-consumer.xml  |   1 +
 .../main/resources/spring/dubbo-demo-provider.xml  |   2 +
 .../dubbo/samples/governance/DemoServiceIT.java    | 138 ++++++++++++++++++---
 6 files changed, 199 insertions(+), 18 deletions(-)

diff --git a/4-governance/dubbo-samples-applevel-override/pom.xml b/4-governance/dubbo-samples-applevel-override/pom.xml
index 472d5191..3fd7b65e 100644
--- a/4-governance/dubbo-samples-applevel-override/pom.xml
+++ b/4-governance/dubbo-samples-applevel-override/pom.xml
@@ -33,9 +33,10 @@
     <properties>
         <source.level>1.8</source.level>
         <target.level>1.8</target.level>
-        <dubbo.version>3.0.7</dubbo.version>
-        <spring.version>4.3.29.RELEASE</spring.version>
+        <dubbo.version>3.1.4</dubbo.version>
+        <spring.version>4.3.16.RELEASE</spring.version>
         <junit.version>4.13.1</junit.version>
+        <awaitility.version>4.2.0</awaitility.version>
         <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
     </properties>
 
@@ -101,6 +102,12 @@
             <artifactId>spring-test</artifactId>
             <scope>test</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.awaitility</groupId>
+            <artifactId>awaitility</artifactId>
+            <version>${awaitility.version}</version>
+        </dependency>
     </dependencies>
 
     <profiles>
diff --git a/4-governance/dubbo-samples-applevel-override/src/main/java/org/apache/dubbo/samples/governance/api/DemoService2.java b/4-governance/dubbo-samples-applevel-override/src/main/java/org/apache/dubbo/samples/governance/api/DemoService2.java
new file mode 100644
index 00000000..828d87c9
--- /dev/null
+++ b/4-governance/dubbo-samples-applevel-override/src/main/java/org/apache/dubbo/samples/governance/api/DemoService2.java
@@ -0,0 +1,26 @@
+/*
+ *
+ *   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.api;
+
+public interface DemoService2 {
+
+    String sayHello(String name);
+
+}
diff --git a/4-governance/dubbo-samples-applevel-override/src/main/java/org/apache/dubbo/samples/governance/impl/DemoService2Impl.java b/4-governance/dubbo-samples-applevel-override/src/main/java/org/apache/dubbo/samples/governance/impl/DemoService2Impl.java
new file mode 100644
index 00000000..ccf690fa
--- /dev/null
+++ b/4-governance/dubbo-samples-applevel-override/src/main/java/org/apache/dubbo/samples/governance/impl/DemoService2Impl.java
@@ -0,0 +1,39 @@
+/*
+ *
+ *   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.impl;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.dubbo.rpc.RpcContext;
+import org.apache.dubbo.samples.governance.api.DemoService2;
+
+public class DemoService2Impl implements DemoService2 {
+    private static AtomicInteger count = new AtomicInteger(0);
+
+    @Override
+    public String sayHello(String name) {
+        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name +
+                ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
+        return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress() +
+                ", count: " + count.incrementAndGet();
+    }
+}
diff --git a/4-governance/dubbo-samples-applevel-override/src/main/resources/spring/dubbo-demo-consumer.xml b/4-governance/dubbo-samples-applevel-override/src/main/resources/spring/dubbo-demo-consumer.xml
index 10d99a7b..c1ccdf4f 100644
--- a/4-governance/dubbo-samples-applevel-override/src/main/resources/spring/dubbo-demo-consumer.xml
+++ b/4-governance/dubbo-samples-applevel-override/src/main/resources/spring/dubbo-demo-consumer.xml
@@ -30,5 +30,6 @@
     <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181?delay-notification=0"/>
 
     <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.samples.governance.api.DemoService"/>
+    <dubbo:reference id="demoService2" check="false" interface="org.apache.dubbo.samples.governance.api.DemoService2"/>
 
 </beans>
diff --git a/4-governance/dubbo-samples-applevel-override/src/main/resources/spring/dubbo-demo-provider.xml b/4-governance/dubbo-samples-applevel-override/src/main/resources/spring/dubbo-demo-provider.xml
index cea1c873..aaa18660 100644
--- a/4-governance/dubbo-samples-applevel-override/src/main/resources/spring/dubbo-demo-provider.xml
+++ b/4-governance/dubbo-samples-applevel-override/src/main/resources/spring/dubbo-demo-provider.xml
@@ -32,5 +32,7 @@
     <dubbo:protocol name="dubbo" port="${dubbo.port:20880}"/>
 
     <bean id="demoService" class="org.apache.dubbo.samples.governance.impl.DemoServiceImpl"/>
+    <bean id="demoService2" class="org.apache.dubbo.samples.governance.impl.DemoService2Impl"/>
     <dubbo:service interface="org.apache.dubbo.samples.governance.api.DemoService" ref="demoService"/>
+    <dubbo:service interface="org.apache.dubbo.samples.governance.api.DemoService2" ref="demoService2"/>
 </beans>
diff --git a/4-governance/dubbo-samples-applevel-override/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java b/4-governance/dubbo-samples-applevel-override/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java
index f2fbcc82..652b208f 100644
--- a/4-governance/dubbo-samples-applevel-override/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java
+++ b/4-governance/dubbo-samples-applevel-override/src/test/java/org/apache/dubbo/samples/governance/DemoServiceIT.java
@@ -17,8 +17,21 @@
 
 package org.apache.dubbo.samples.governance;
 
-import org.apache.dubbo.samples.governance.api.DemoService;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.registry.Registry;
+import org.apache.dubbo.registry.client.migration.MigrationInvoker;
+import org.apache.dubbo.registry.client.migration.model.MigrationStep;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcContext;
+import org.apache.dubbo.rpc.cluster.ClusterInvoker;
+import org.apache.dubbo.rpc.model.ConsumerModel;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+import org.apache.dubbo.samples.governance.api.DemoService;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -27,11 +40,15 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
+import static org.awaitility.Awaitility.await;
+
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = {"classpath:/spring/dubbo-demo-consumer.xml"})
 public class DemoServiceIT {
     @Autowired
     private DemoService demoService;
+    @Autowired
+    private DemoService demoService2;
 
     @BeforeClass
     public static void setUp() throws Exception {
@@ -41,9 +58,9 @@ public class DemoServiceIT {
     @Test
     public void test20880_interface() throws Exception {
         UpgradeUtil.writeForceInterfaceRule();
-        checkIfNotified();
+        await().until(() -> checkIfNotified(MigrationStep.FORCE_INTERFACE));
         ZKTools.generateAppLevelOverride(100, 0);
-        Thread.sleep(5000);
+        await().until(() -> checkWeight(100, 0));
         for (int i = 0; i < 10; i++) {
             String result = demoService.sayHello("world");
             System.out.println(result);
@@ -54,9 +71,9 @@ public class DemoServiceIT {
     @Test
     public void test20880_application() throws Exception {
         UpgradeUtil.writeForceApplicationRule();
-        checkIfNotified();
+        await().until(() -> checkIfNotified(MigrationStep.FORCE_APPLICATION));
         ZKTools.generateAppLevelOverride(100, 0);
-        Thread.sleep(5000);
+        await().until(() -> checkWeight(100, 0));
         for (int i = 0; i < 10; i++) {
             String result = demoService.sayHello("world");
             System.out.println(result);
@@ -67,9 +84,9 @@ public class DemoServiceIT {
     @Test
     public void test20881_interface() throws Exception {
         UpgradeUtil.writeForceInterfaceRule();
-        checkIfNotified();
+        await().until(() -> checkIfNotified(MigrationStep.FORCE_INTERFACE));
         ZKTools.generateAppLevelOverride(0, 100);
-        Thread.sleep(5000);
+        await().until(() -> checkWeight(0, 100));
         for (int i = 0; i < 10; i++) {
             String result = demoService.sayHello("world");
             System.out.println(result);
@@ -80,9 +97,9 @@ public class DemoServiceIT {
     @Test
     public void test20881_application() throws Exception {
         UpgradeUtil.writeForceApplicationRule();
-        checkIfNotified();
+        await().until(() -> checkIfNotified(MigrationStep.FORCE_APPLICATION));
         ZKTools.generateAppLevelOverride(0, 100);
-        Thread.sleep(5000);
+        await().until(() -> checkWeight(0, 100));
         for (int i = 0; i < 10; i++) {
             String result = demoService.sayHello("world");
             System.out.println(result);
@@ -90,13 +107,102 @@ public class DemoServiceIT {
         }
     }
 
-    private void checkIfNotified() throws InterruptedException {
-        for (int i = 0; i < 50; i++) {
-            if (FrameworkStatusReporterImpl.getReport().size() == 1) {
-                FrameworkStatusReporterImpl.clearReport();
-                return;
-            }
-            Thread.sleep(100);
+    @Test
+    public void test20880_interface2() throws Exception {
+        UpgradeUtil.writeForceInterfaceRule();
+        await().until(() -> checkIfNotified(MigrationStep.FORCE_INTERFACE));
+        ZKTools.generateAppLevelOverride(100, 0);
+        await().until(() -> checkWeight(100, 0));
+        for (int i = 0; i < 10; i++) {
+            String result = demoService2.sayHello("world");
+            System.out.println(result);
+            Assert.assertTrue(result.contains("20880"));
         }
     }
+
+    @Test
+    public void test20880_application2() throws Exception {
+        UpgradeUtil.writeForceApplicationRule();
+        await().until(() -> checkIfNotified(MigrationStep.FORCE_APPLICATION));
+        ZKTools.generateAppLevelOverride(100, 0);
+        await().until(() -> checkWeight(100, 0));
+        for (int i = 0; i < 10; i++) {
+            String result = demoService2.sayHello("world");
+            System.out.println(result);
+            Assert.assertTrue(result.contains("20880"));
+        }
+    }
+
+    @Test
+    public void test20881_interface2() throws Exception {
+        UpgradeUtil.writeForceInterfaceRule();
+        await().until(() -> checkIfNotified(MigrationStep.FORCE_INTERFACE));
+        ZKTools.generateAppLevelOverride(0, 100);
+        await().until(() -> checkWeight(0, 100));
+        for (int i = 0; i < 10; i++) {
+            String result = demoService2.sayHello("world");
+            System.out.println(result);
+            Assert.assertTrue(result.contains("20881"));
+        }
+    }
+
+    @Test
+    public void test20881_application2() throws Exception {
+        UpgradeUtil.writeForceApplicationRule();
+        await().until(() -> checkIfNotified(MigrationStep.FORCE_APPLICATION));
+        ZKTools.generateAppLevelOverride(0, 100);
+        await().until(() -> checkWeight(0, 100));
+        for (int i = 0; i < 10; i++) {
+            String result = demoService2.sayHello("world");
+            System.out.println(result);
+            Assert.assertTrue(result.contains("20881"));
+        }
+    }
+
+    private boolean checkIfNotified(MigrationStep expectedStep) throws InterruptedException {
+        return FrameworkModel.defaultModel().getServiceRepository()
+                .allConsumerModels()
+                .stream()
+                .map(ConsumerModel::getServiceMetadata)
+                .map(s -> s.getAttribute(CommonConstants.CURRENT_CLUSTER_INVOKER_KEY))
+                .filter(Objects::nonNull)
+                .map(m -> (Map<Registry, MigrationInvoker<?>>) m)
+                .map(Map::entrySet)
+                .flatMap(Set::stream)
+                .map(Map.Entry::getValue)
+                .map(MigrationInvoker::getMigrationStep)
+                .allMatch(expectedStep::equals);
+    }
+
+    private boolean checkWeight(int weight1, int weight2) throws InterruptedException {
+        AtomicBoolean match = new AtomicBoolean(true);
+        FrameworkModel.defaultModel().getServiceRepository()
+                .allConsumerModels()
+                .stream()
+                .map(ConsumerModel::getServiceMetadata)
+                .map(s -> s.getAttribute(CommonConstants.CURRENT_CLUSTER_INVOKER_KEY))
+                .filter(Objects::nonNull)
+                .map(m -> (Map<Registry, MigrationInvoker<?>>) m)
+                .map(Map::entrySet)
+                .flatMap(Set::stream)
+                .map(Map.Entry::getValue)
+                .map(MigrationInvoker::getCurrentAvailableInvoker)
+                .map(ClusterInvoker::getDirectory)
+                .forEach(directory -> {
+                    RpcContext.getServiceContext().setConsumerUrl(directory.getConsumerUrl());
+                    if (!directory.getAllInvokers()
+                            .stream()
+                            .map(Invoker::getUrl)
+                            .allMatch(url -> {
+                                if (url.getPort() == 20880) {
+                                    return weight1 == url.getServiceParameter(DemoService.class.getName(), "weight", -1);
+                                } else {
+                                    return weight2 == url.getParameter("weight", -1);
+                                }
+                            })) {
+                        match.set(false);
+                    }
+                });
+        return match.get();
+    }
 }


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