You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by wa...@apache.org on 2024/04/22 11:42:53 UTC

(dubbo) branch 3.2 updated: compatible service discovery, export noting suffix servicename, eg: ${interface}:${version} (#14096)

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

wangchengming 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 0a1ad55bff compatible service discovery, export noting suffix servicename, eg: ${interface}:${version} (#14096)
0a1ad55bff is described below

commit 0a1ad55bff34c8cffc066c4ab26ac8466691a4f4
Author: Wang Chengming <wa...@apache.org>
AuthorDate: Mon Apr 22 19:42:47 2024 +0800

    compatible service discovery, export noting suffix servicename, eg: ${interface}:${version} (#14096)
    
    compatible service discovery, export noting suffix servicename, eg: {interface}:${version}
    
    code format
    
    compatible service discovery, export noting suffix servicename, eg: {interface}:${version}
    
    Co-authored-by: 呈铭 <be...@antgroup.com>
---
 .../src/main/java/org/apache/dubbo/common/URL.java | 21 ++++++
 .../dubbo/common/constants/RegistryConstants.java  |  7 ++
 .../apache/dubbo/registry/nacos/NacosRegistry.java | 74 +++++++++++++++++-----
 3 files changed, 85 insertions(+), 17 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
index 0c7bb1e3e8..eaf8ffd401 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
@@ -1295,6 +1295,19 @@ public /*final**/ class URL implements Serializable {
         return serviceNameBuilder.toString();
     }
 
+    /**
+     * The format is "{interface}:[version]"
+     *
+     * @return
+     */
+    public String getCompatibleColonSeparatedKey() {
+        StringBuilder serviceNameBuilder = new StringBuilder();
+        serviceNameBuilder.append(this.getServiceInterface());
+        compatibleAppend(serviceNameBuilder, VERSION_KEY);
+        compatibleAppend(serviceNameBuilder, GROUP_KEY);
+        return serviceNameBuilder.toString();
+    }
+
     private void append(StringBuilder target, String parameterName, boolean first) {
         String parameterValue = this.getParameter(parameterName);
         if (!isBlank(parameterValue)) {
@@ -1307,6 +1320,14 @@ public /*final**/ class URL implements Serializable {
         }
     }
 
+    private void compatibleAppend(StringBuilder target, String parameterName) {
+        String parameterValue = this.getParameter(parameterName);
+        if (!isBlank(parameterValue)) {
+            target.append(':');
+            target.append(parameterValue);
+        }
+    }
+
     /**
      * The format of return value is '{group}/{interfaceName}:{version}'
      *
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java
index 74a8f0b4b2..18cb945aee 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/RegistryConstants.java
@@ -141,4 +141,11 @@ public interface RegistryConstants {
     String ENABLE_EMPTY_PROTECTION_KEY = "enable-empty-protection";
     boolean DEFAULT_ENABLE_EMPTY_PROTECTION = false;
     String REGISTER_CONSUMER_URL_KEY = "register-consumer-url";
+
+    /**
+     * export noting suffix servicename
+     * by default, dubbo export servicename is "${interface}:${version}:", this servicename with ':' suffix
+     * for compatible, we should export noting suffix servicename, eg: ${interface}:${version}
+     */
+    String NACOE_REGISTER_COMPATIBLE = "nacos.register-compatible";
 }
diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java
index 9fbbd9ebb4..27f6608b81 100644
--- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java
+++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosRegistry.java
@@ -35,6 +35,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.List;
@@ -71,6 +72,7 @@ import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGO
 import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_ENABLE_EMPTY_PROTECTION;
 import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
 import static org.apache.dubbo.common.constants.RegistryConstants.ENABLE_EMPTY_PROTECTION_KEY;
+import static org.apache.dubbo.common.constants.RegistryConstants.NACOE_REGISTER_COMPATIBLE;
 import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY;
 import static org.apache.dubbo.common.constants.RegistryConstants.REGISTER_CONSUMER_URL_KEY;
 import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY;
@@ -107,9 +109,8 @@ public class NacosRegistry extends FailbackRegistry {
     private static final String UP = "UP";
 
     /**
-     * The separator for service name
-     * Change a constant to be configurable, it's designed for Windows file name that is compatible with old
-     * Nacos binary release(< 0.6.1)
+     * The separator for service name Change a constant to be configurable, it's designed for Windows file name that is
+     * compatible with old Nacos binary release(< 0.6.1)
      */
     private static final String SERVICE_NAME_SEPARATOR = System.getProperty("nacos.service.name.separator", ":");
 
@@ -174,18 +175,33 @@ public class NacosRegistry extends FailbackRegistry {
     public void doRegister(URL url) {
         try {
             if (PROVIDER_SIDE.equals(url.getSide()) || getUrl().getParameter(REGISTER_CONSUMER_URL_KEY, false)) {
-                String serviceName = getServiceName(url);
                 Instance instance = createInstance(url);
+
+                Set<String> serviceNames = new HashSet<>();
+                // by default servicename is "org.apache.dubbo.xxService:1.0.0:"
+                String serviceName = getServiceName(url, false);
+                serviceNames.add(serviceName);
+
+                // in https://github.com/apache/dubbo/issues/14075
+                if (getUrl().getParameter(NACOE_REGISTER_COMPATIBLE, false)) {
+                    // servicename is "org.apache.dubbo.xxService:1.0.0"
+                    String compatibleServiceName = getServiceName(url, true);
+                    serviceNames.add(compatibleServiceName);
+                }
+
                 /**
-                 *  namingService.registerInstance with {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
+                 *  namingService.registerInstance with
+                 *  {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
                  *  default {@link DEFAULT_GROUP}
                  *
                  * in https://github.com/apache/dubbo/issues/5978
                  */
-                namingService.registerInstance(serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP), instance);
+                for (String service : serviceNames) {
+                    namingService.registerInstance(service, getUrl().getGroup(Constants.DEFAULT_GROUP), instance);
+                }
             } else {
-                logger.info(
-                        "Please set 'dubbo.registry.parameters.register-consumer-url=true' to turn on consumer url registration.");
+                logger.info("Please set 'dubbo.registry.parameters.register-consumer-url=true' to turn on consumer "
+                        + "url registration.");
             }
         } catch (SkipFailbackWrapperException exception) {
             throw exception;
@@ -198,10 +214,24 @@ public class NacosRegistry extends FailbackRegistry {
     @Override
     public void doUnregister(final URL url) {
         try {
-            String serviceName = getServiceName(url);
             Instance instance = createInstance(url);
-            namingService.deregisterInstance(
-                    serviceName, getUrl().getGroup(Constants.DEFAULT_GROUP), instance.getIp(), instance.getPort());
+
+            Set<String> serviceNames = new HashSet<>();
+            // by default servicename is "org.apache.dubbo.xxService:1.0.0:"
+            String serviceName = getServiceName(url, false);
+            serviceNames.add(serviceName);
+
+            // in https://github.com/apache/dubbo/issues/14075
+            if (getUrl().getParameter(NACOE_REGISTER_COMPATIBLE, false)) {
+                // servicename is "org.apache.dubbo.xxService:1.0.0"
+                String serviceName1 = getServiceName(url, true);
+                serviceNames.add(serviceName1);
+            }
+
+            for (String service : serviceNames) {
+                namingService.deregisterInstance(
+                        service, getUrl().getGroup(Constants.DEFAULT_GROUP), instance.getIp(), instance.getPort());
+            }
         } catch (SkipFailbackWrapperException exception) {
             throw exception;
         } catch (Exception cause) {
@@ -230,7 +260,8 @@ public class NacosRegistry extends FailbackRegistry {
                  * Get all instances with serviceNames to avoid instance overwrite and but with empty instance mentioned
                  * in https://github.com/apache/dubbo/issues/5885 and https://github.com/apache/dubbo/issues/5899
                  *
-                 * namingService.getAllInstances with {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
+                 * namingService.getAllInstances with
+                 * {@link org.apache.dubbo.registry.support.AbstractRegistry#registryUrl}
                  * default {@link DEFAULT_GROUP}
                  *
                  * in https://github.com/apache/dubbo/issues/5978
@@ -268,8 +299,8 @@ public class NacosRegistry extends FailbackRegistry {
     }
 
     /**
-     * Since 2.7.6 the legacy service name will be added to serviceNames
-     * to fix bug with https://github.com/apache/dubbo/issues/5442
+     * Since 2.7.6 the legacy service name will be added to serviceNames to fix bug with
+     * https://github.com/apache/dubbo/issues/5442
      *
      * @param url
      * @return
@@ -290,7 +321,8 @@ public class NacosRegistry extends FailbackRegistry {
                         "",
                         "",
                         String.format(
-                                "No aggregate listener found for url %s, this service might have already been unsubscribed.",
+                                "No aggregate listener found for url %s, "
+                                        + "this service might have already been unsubscribed.",
                                 url));
                 return;
             }
@@ -581,7 +613,8 @@ public class NacosRegistry extends FailbackRegistry {
                     REGISTRY_NACOS_EXCEPTION,
                     "",
                     "",
-                    "Received empty url address list and empty protection is disabled, will clear current available addresses");
+                    "Received empty url address list and empty protection is "
+                            + "disabled, will clear current available addresses");
             URL empty = URLBuilder.from(consumerURL)
                     .setProtocol(EMPTY_PROTOCOL)
                     .addParameter(CATEGORY_KEY, DEFAULT_CATEGORY)
@@ -697,7 +730,10 @@ public class NacosRegistry extends FailbackRegistry {
         return valueOf(url);
     }
 
-    private String getServiceName(URL url) {
+    private String getServiceName(URL url, boolean needCompatible) {
+        if (needCompatible) {
+            return getCompatibleServiceName(url, url.getCategory(DEFAULT_CATEGORY));
+        }
         return getServiceName(url, url.getCategory(DEFAULT_CATEGORY));
     }
 
@@ -705,6 +741,10 @@ public class NacosRegistry extends FailbackRegistry {
         return category + SERVICE_NAME_SEPARATOR + url.getColonSeparatedKey();
     }
 
+    private String getCompatibleServiceName(URL url, String category) {
+        return category + SERVICE_NAME_SEPARATOR + url.getCompatibleColonSeparatedKey();
+    }
+
     private void filterEnabledInstances(Collection<Instance> instances) {
         filterData(instances, Instance::isEnabled);
     }