Skip to content

Commit

Permalink
Use empty protocol for nacos registry when address list is empty. (#4349
Browse files Browse the repository at this point in the history
)

fixes #4294.
  • Loading branch information
cvictory authored and chickenlj committed Jun 25, 2019
1 parent a2f0d9a commit a1e4757
Showing 1 changed file with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +36,6 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -52,13 +52,14 @@
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}
Expand Down Expand Up @@ -136,7 +137,7 @@ public List<URL> lookup(final URL url) {
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;
Expand Down Expand Up @@ -168,7 +169,7 @@ private void doSubscribe(final URL url, final NotifyListener listener, final Lis
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);
}
});
Expand Down Expand Up @@ -332,9 +333,9 @@ private List<String> doGetServiceNames(URL url) {
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) {
Expand All @@ -352,7 +353,7 @@ private void subscribeEventListener(String serviceName, final URL url, final Not
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);
Expand All @@ -364,14 +365,15 @@ private void subscribeEventListener(String serviceName, final URL url, final Not
* 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);
}

Expand All @@ -397,6 +399,18 @@ private URL buildURL(Instance instance) {
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);
Expand Down

0 comments on commit a1e4757

Please sign in to comment.