From b8e05d298de0b5cfacbdfea4aebca359ccd0b368 Mon Sep 17 00:00:00 2001 From: cvictory Date: Thu, 20 Jun 2019 11:34:26 +0800 Subject: [PATCH] fix empty protocol in consule registry. #4294 --- .../dubbo/registry/consul/ConsulRegistry.java | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistry.java b/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistry.java index e7cf986cc45..e4832ac6ccc 100644 --- a/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistry.java +++ b/dubbo-registry/dubbo-registry-consul/src/main/java/org/apache/dubbo/registry/consul/ConsulRegistry.java @@ -18,8 +18,10 @@ package org.apache.dubbo.registry.consul; 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.CollectionUtils; import org.apache.dubbo.common.utils.NamedThreadFactory; import org.apache.dubbo.registry.NotifyListener; import org.apache.dubbo.registry.support.FailbackRegistry; @@ -49,6 +51,8 @@ import static java.util.concurrent.Executors.newCachedThreadPool; import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE; +import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY; +import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL; import static org.apache.dubbo.registry.Constants.CONSUMER_PROTOCOL; import static org.apache.dubbo.registry.Constants.PROVIDER_PROTOCOL; @@ -136,12 +140,12 @@ public void doSubscribe(URL url, NotifyListener listener) { Response>> response = getAllServices(-1, buildWatchTimeout(url)); index = response.getConsulIndex(); List services = getHealthServices(response.getValue()); - urls = convert(services); + urls = convert(services, url); } else { String service = url.getServiceKey(); Response> response = getHealthServices(service, -1, buildWatchTimeout(url)); index = response.getConsulIndex(); - urls = convert(response.getValue()); + urls = convert(response.getValue(), url); } notify(url, listener, urls); @@ -175,7 +179,7 @@ public List lookup(URL url) { if (result == null || result.getValue() == null || result.getValue().isEmpty()) { return new ArrayList<>(); } else { - return convert(result.getValue()); + return convert(result.getValue(), url); } } catch (Throwable e) { throw new RpcException("Failed to lookup " + url + " from consul " + getUrl() + ", cause: " + e.getMessage(), e); @@ -241,7 +245,10 @@ private boolean isProviderSide(URL url) { return url.getProtocol().equals(PROVIDER_PROTOCOL); } - private List convert(List services) { + private List convert(List services, URL consumerURL) { + if (CollectionUtils.isEmpty(services)) { + return emptyURL(consumerURL); + } return services.stream() .map(HealthService::getService) .filter(Objects::nonNull) @@ -252,6 +259,17 @@ private List convert(List services) { .collect(Collectors.toList()); } + private List emptyURL(URL consumerURL) { + // No Category Parameter + URL empty = URLBuilder.from(consumerURL) + .setProtocol(EMPTY_PROTOCOL) + .removeParameter(CATEGORY_KEY) + .build(); + List result = new ArrayList(); + result.add(empty); + return result; + } + private NewService buildService(URL url) { NewService service = new NewService(); service.setAddress(url.getHost()); @@ -318,7 +336,7 @@ private void processService() { if (currentIndex != null && currentIndex > consulIndex) { consulIndex = currentIndex; List services = response.getValue(); - List urls = convert(services); + List urls = convert(services, url); for (NotifyListener listener : getSubscribed().get(url)) { doNotify(url, listener, urls); } @@ -331,7 +349,7 @@ private void processServices() { if (currentIndex != null && currentIndex > consulIndex) { consulIndex = currentIndex; List services = getHealthServices(response.getValue()); - List urls = convert(services); + List urls = convert(services, url); for (NotifyListener listener : getSubscribed().get(url)) { doNotify(url, listener, urls); }