You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2019/06/25 08:39:15 UTC

[dubbo] branch 2.7.3-release updated: Use empty protocol for nacos registry when address list is empty. (#4349)

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

liujun pushed a commit to branch 2.7.3-release
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/2.7.3-release by this push:
     new a1e4757  Use empty protocol for nacos registry when address list is empty. (#4349)
a1e4757 is described below

commit a1e4757f36bc4e88b4ff4b998bb718e62e9b19f4
Author: cvictory <sh...@gmail.com>
AuthorDate: Tue Jun 25 16:39:08 2019 +0800

    Use empty protocol for nacos registry when address list is empty. (#4349)
    
    fixes #4294.
---
 .../apache/dubbo/registry/nacos/NacosRegistry.java | 32 ++++++++++++++++------
 1 file changed, 23 insertions(+), 9 deletions(-)

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 5483052..24b55c0 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
@@ -17,6 +17,7 @@
 package org.apache.dubbo.registry.nacos;
 
 import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.URLBuilder;
 import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.utils.UrlUtils;
@@ -35,7 +36,6 @@ import org.apache.commons.lang3.StringUtils;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -52,13 +52,14 @@ import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
 import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
-import static org.apache.dubbo.registry.Constants.ADMIN_PROTOCOL;
 import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY;
 import static org.apache.dubbo.common.constants.RegistryConstants.CONFIGURATORS_CATEGORY;
 import static org.apache.dubbo.common.constants.RegistryConstants.CONSUMERS_CATEGORY;
 import static org.apache.dubbo.common.constants.RegistryConstants.DEFAULT_CATEGORY;
+import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
 import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDERS_CATEGORY;
 import static org.apache.dubbo.common.constants.RegistryConstants.ROUTERS_CATEGORY;
+import static org.apache.dubbo.registry.Constants.ADMIN_PROTOCOL;
 
 /**
  * Nacos {@link Registry}
@@ -136,7 +137,7 @@ public class NacosRegistry extends FailbackRegistry {
             List<String> serviceNames = getServiceNames(url, null);
             for (String serviceName : serviceNames) {
                 List<Instance> instances = namingService.getAllInstances(serviceName);
-                urls.addAll(buildURLs(url, instances));
+                urls.addAll(buildURLs(url, serviceName, instances));
             }
         });
         return urls;
@@ -168,7 +169,7 @@ public class NacosRegistry extends FailbackRegistry {
         execute(namingService -> {
             for (String serviceName : serviceNames) {
                 List<Instance> instances = namingService.getAllInstances(serviceName);
-                notifySubscriber(url, listener, instances);
+                notifySubscriber(url, serviceName, listener, instances);
                 subscribeEventListener(serviceName, url, listener);
             }
         });
@@ -332,9 +333,9 @@ public class NacosRegistry extends FailbackRegistry {
         return serviceNames;
     }
 
-    private List<URL> buildURLs(URL consumerURL, Collection<Instance> instances) {
+    private List<URL> buildURLs(URL consumerURL, String serviceName, Collection<Instance> instances) {
         if (instances.isEmpty()) {
-            return Collections.emptyList();
+            return emptyURL(consumerURL, serviceName);
         }
         List<URL> urls = new LinkedList<>();
         for (Instance instance : instances) {
@@ -352,7 +353,7 @@ public class NacosRegistry extends FailbackRegistry {
             EventListener eventListener = event -> {
                 if (event instanceof NamingEvent) {
                     NamingEvent e = (NamingEvent) event;
-                    notifySubscriber(url, listener, e.getInstances());
+                    notifySubscriber(url, serviceName, listener, e.getInstances());
                 }
             };
             namingService.subscribe(serviceName, eventListener);
@@ -364,14 +365,15 @@ public class NacosRegistry extends FailbackRegistry {
      * Notify the Healthy {@link Instance instances} to subscriber.
      *
      * @param url       {@link URL}
+     * @param serviceName
      * @param listener  {@link NotifyListener}
      * @param instances all {@link Instance instances}
      */
-    private void notifySubscriber(URL url, NotifyListener listener, Collection<Instance> instances) {
+    private void notifySubscriber(URL url, String serviceName, NotifyListener listener, Collection<Instance> instances) {
         List<Instance> healthyInstances = new LinkedList<>(instances);
         // Healthy Instances
         filterHealthyInstances(healthyInstances);
-        List<URL> urls = buildURLs(url, healthyInstances);
+        List<URL> urls = buildURLs(url, serviceName, healthyInstances);
         NacosRegistry.this.notify(url, listener, urls);
     }
 
@@ -397,6 +399,18 @@ public class NacosRegistry extends FailbackRegistry {
                 instance.getMetadata());
     }
 
+    private List<URL> emptyURL(URL consumerURL, String serviceName) {
+        int i = serviceName.indexOf(SERVICE_NAME_SEPARATOR);
+        String category = i < 0 ? serviceName : serviceName.substring(0, i);
+        URL empty = URLBuilder.from(consumerURL)
+                .setProtocol(EMPTY_PROTOCOL)
+                .addParameter(CATEGORY_KEY, category)
+                .build();
+        List<URL> result = new ArrayList<URL>();
+        result.add(empty);
+        return result;
+    }
+
     private Instance createInstance(URL url) {
         // Append default category if absent
         String category = url.getParameter(CATEGORY_KEY, DEFAULT_CATEGORY);