You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2023/07/29 12:40:11 UTC

[dubbo] branch 3.2 updated: support reExport (#12809)

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

albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.2 by this push:
     new 1e29cdc1fa support reExport (#12809)
1e29cdc1fa is described below

commit 1e29cdc1fa2ac659ecd1f388f7f057fd675f3213
Author: suncairong163 <10...@users.noreply.github.com>
AuthorDate: Sat Jul 29 20:40:04 2023 +0800

    support reExport (#12809)
    
    Co-authored-by: suncr <su...@moresec.cn>
---
 .../rpc/protocol/rest/PathAndInvokerMapper.java    | 19 +++++++-----
 .../pair/InvokerAndRestMethodMetadataPair.java     | 36 +++++++++++++++++++++-
 .../rpc/protocol/rest/JaxrsRestProtocolTest.java   | 23 +++++++++++++-
 3 files changed, 69 insertions(+), 9 deletions(-)

diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
index a9fc5c2d2d..d04b652958 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
@@ -57,7 +57,8 @@ public class PathAndInvokerMapper {
 
 
     /**
-     *  get rest method metadata by path matcher
+     * get rest method metadata by path matcher
+     *
      * @param pathMatcher
      * @return
      */
@@ -100,13 +101,17 @@ public class PathAndInvokerMapper {
 
         if (pathMatcherPairMap.containsKey(pathMatcher)) {
 
-            InvokerAndRestMethodMetadataPair beforeMetadata = pathMatcherPairMap.get(pathMatcher);
+            // cover the old service metadata when  current interface is old interface & current method desc equals old`s method desc,else ,throw double check exception
 
-            throw new DoublePathCheckException(
-                "dubbo rest double path check error, current path is: " + pathMatcher
-                    + " ,and service method is: " + invokerRestMethodMetadataPair.getRestMethodMetadata().getReflectMethod()
-                    + "before service  method is: " + beforeMetadata.getRestMethodMetadata().getReflectMethod()
-            );
+            InvokerAndRestMethodMetadataPair beforeMetadata = pathMatcherPairMap.get(pathMatcher);
+            // true when reExport
+            if (!invokerRestMethodMetadataPair.compareServiceMethod(beforeMetadata)){
+                throw new DoublePathCheckException(
+                    "dubbo rest double path check error, current path is: " + pathMatcher
+                        + " ,and service method is: " + invokerRestMethodMetadataPair.getRestMethodMetadata().getReflectMethod()
+                        + "before service  method is: " + beforeMetadata.getRestMethodMetadata().getReflectMethod()
+                );
+            }
         }
 
         pathMatcherPairMap.put(pathMatcher, invokerRestMethodMetadataPair);
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/pair/InvokerAndRestMethodMetadataPair.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/pair/InvokerAndRestMethodMetadataPair.java
index 0075275e49..70380c986b 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/pair/InvokerAndRestMethodMetadataPair.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/pair/InvokerAndRestMethodMetadataPair.java
@@ -19,8 +19,11 @@ package org.apache.dubbo.rpc.protocol.rest.pair;
 import org.apache.dubbo.metadata.rest.RestMethodMetadata;
 import org.apache.dubbo.rpc.Invoker;
 
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
 /**
- *  for invoker & restMethodMetadata pair
+ * for invoker & restMethodMetadata pair
  */
 public class InvokerAndRestMethodMetadataPair {
 
@@ -45,5 +48,36 @@ public class InvokerAndRestMethodMetadataPair {
         return new InvokerAndRestMethodMetadataPair(invoker, restMethodMetadata);
     }
 
+    /**
+     * same interface  & same  method desc
+     *
+     * @param beforeMetadata
+     * @return
+     */
+    public boolean compareServiceMethod(InvokerAndRestMethodMetadataPair beforeMetadata) {
+
+        Class currentServiceInterface = this.invoker.getInterface();
+        Class<?> beforeServiceInterface = beforeMetadata.getInvoker().getInterface();
+
+        if (!currentServiceInterface.equals(beforeServiceInterface)) {
+            return false;
+        }
+
+
+        Method beforeServiceMethod = beforeMetadata.getRestMethodMetadata().getReflectMethod();
+
+        Method currentReflectMethod = this.restMethodMetadata.getReflectMethod();
+
+        if (beforeServiceMethod.getName().equals(currentReflectMethod.getName()) // method name
+            // method param types
+            && Arrays.toString(beforeServiceMethod.getParameterTypes()).equals(Arrays.toString(currentReflectMethod.getParameterTypes()))) {
+            return true;
+        }
+
+        return false;
+
+
+    }
+
 
 }
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
index eed6639cd1..21bb14b911 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
@@ -460,14 +460,19 @@ class JaxrsRestProtocolTest {
 
             Map<PathMatcher, RestMethodMetadata> pathContainPathVariableToServiceMap = serviceRestMetadata.getPathUnContainPathVariableToServiceMap();
 
+            Invoker<TestInterface> invokerNew = proxy.getInvoker(new TestInterface() {
+            }, TestInterface.class, exportUrl);
 
             pathAndInvokerMapper.addPathAndInvoker(pathContainPathVariableToServiceMap, invoker);
-            pathAndInvokerMapper.addPathAndInvoker(pathContainPathVariableToServiceMap, invoker);
+            pathAndInvokerMapper.addPathAndInvoker(pathContainPathVariableToServiceMap, invokerNew);
         });
 
 
     }
 
+    public static interface TestInterface{}
+
+
     @Test
     void testMapParam() {
         DemoService server = new DemoServiceImpl();
@@ -722,6 +727,22 @@ class JaxrsRestProtocolTest {
         exporter.unexport();
     }
 
+    @Test
+    void testReExport() {
+        DemoService server = new DemoServiceImpl();
+
+        URL url = this.registerProvider(exportUrl, server, DemoService.class);
+
+        URL nettyUrl = url.addParameter(SERVER_KEY, "netty");
+
+        Exporter<DemoService> exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));
+        nettyUrl = url.addParameter("SERVER_KEY", "netty");
+        exporter = protocol.export(proxy.getInvoker(server, DemoService.class, nettyUrl));
+
+
+        exporter.unexport();
+    }
+
     private URL registerProvider(URL url, Object impl, Class<?> interfaceClass) {
         ServiceDescriptor serviceDescriptor = repository.registerService(interfaceClass);
         ProviderModel providerModel = new ProviderModel(