1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Polish spring-cloud-incubator/spring-cloud-alibaba#623 : Add EMPTY_PROTOCOL URLs when no service instance is available

This commit is contained in:
mercyblitz 2019-08-01 10:27:29 +08:00
parent 8b1f78c237
commit a666ddc8cb
3 changed files with 47 additions and 23 deletions

View File

@ -442,12 +442,18 @@ public class DubboServiceDiscoveryAutoConfiguration {
} }
} }
/**
* Consul Customized Configuration
*/
@Configuration @Configuration
@ConditionalOnBean(name = CONSUL_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME) @ConditionalOnBean(name = CONSUL_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME)
class ConsulConfiguration { class ConsulConfiguration {
} }
/**
* Nacos Customized Configuration
*/
@Configuration @Configuration
@ConditionalOnBean(name = NACOS_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME) @ConditionalOnBean(name = NACOS_DISCOVERY_AUTO_CONFIGURATION_CLASS_NAME)
class NacosConfiguration { class NacosConfiguration {

View File

@ -32,6 +32,7 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.CollectionUtils;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
@ -41,12 +42,15 @@ import java.util.Set;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
import static org.apache.dubbo.common.URLBuilder.from;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE; import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
import static org.apache.dubbo.registry.Constants.ADMIN_PROTOCOL; import static org.apache.dubbo.registry.Constants.ADMIN_PROTOCOL;
import static org.springframework.util.StringUtils.hasText; import static org.springframework.util.StringUtils.hasText;
@ -182,12 +186,12 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
String serviceName = event.getServiceName(); String serviceName = event.getServiceName();
Collection<ServiceInstance> serviceInstances = event.getServiceInstances(); Collection<ServiceInstance> serviceInstances = event.getServiceInstances();
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("The event of the service instances[name : {} , size: {}] changed has been arrived", logger.info("The event of the service instances[name : {} , size: {}] change has been arrived",
serviceName, serviceInstances.size()); serviceName, serviceInstances.size());
} }
subscribeDubboServiceURLs(url, listener, serviceName, s -> serviceInstances); subscribeDubboServiceURLs(url, listener, serviceName, s -> serviceInstances);
// Mark event to be processed // Mark event to be processed
event.process(); event.processed();
} }
}); });
} }
@ -222,36 +226,50 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
return; return;
} }
List<URL> exportedURLs = getExportedURLs(dubboMetadataService, url); Collection<ServiceInstance> serviceInstances = serviceInstancesFunction.apply(serviceName);
List<URL> allSubscribedURLs = new LinkedList<>(); List<URL> allSubscribedURLs = new LinkedList<>();
for (URL exportedURL : exportedURLs) {
Collection<ServiceInstance> serviceInstances = serviceInstancesFunction.apply(serviceName); if (CollectionUtils.isEmpty(serviceInstances)) {
String protocol = exportedURL.getProtocol(); /**
List<URL> subscribedURLs = new LinkedList<>(); * URLs with {@link RegistryConstants#EMPTY_PROTOCOL}
serviceInstances.forEach(serviceInstance -> { */
Integer port = repository.getDubboProtocolPort(serviceInstance, protocol); allSubscribedURLs.addAll(emptyURLs(url));
String host = serviceInstance.getHost(); } else {
if (port == null) { List<URL> exportedURLs = getExportedURLs(dubboMetadataService, url);
if (logger.isWarnEnabled()) {
logger.warn("The protocol[{}] port of Dubbo service instance[host : {}] " + for (URL exportedURL : exportedURLs) {
"can't be resolved", protocol, host); String protocol = exportedURL.getProtocol();
List<URL> subscribedURLs = new LinkedList<>();
serviceInstances.forEach(serviceInstance -> {
Integer port = repository.getDubboProtocolPort(serviceInstance, protocol);
String host = serviceInstance.getHost();
if (port == null) {
if (logger.isWarnEnabled()) {
logger.warn("The protocol[{}] port of Dubbo service instance[host : {}] " +
"can't be resolved", protocol, host);
}
} else {
URL subscribedURL = new URL(protocol, host, port, exportedURL.getParameters());
subscribedURLs.add(subscribedURL);
} }
} else { });
URL subscribedURL = new URL(protocol, host, port, exportedURL.getParameters());
subscribedURLs.add(subscribedURL); if (logger.isDebugEnabled()) {
logger.debug("The subscribed URL[{}] will notify all URLs : {}", url, subscribedURLs);
} }
});
if (logger.isDebugEnabled()) { allSubscribedURLs.addAll(subscribedURLs);
logger.debug("The subscribed URL[{}] will notify all URLs : {}", url, subscribedURLs);
} }
allSubscribedURLs.addAll(subscribedURLs);
} }
listener.notify(allSubscribedURLs); listener.notify(allSubscribedURLs);
} }
private List<URL> emptyURLs(URL url) {
return asList(from(url).setProtocol(EMPTY_PROTOCOL).build());
}
private List<ServiceInstance> getServiceInstances(String serviceName) { private List<ServiceInstance> getServiceInstances(String serviceName) {
return hasText(serviceName) ? doGetServiceInstances(serviceName) : emptyList(); return hasText(serviceName) ? doGetServiceInstances(serviceName) : emptyList();
} }

View File

@ -70,7 +70,7 @@ public class ServiceInstancesChangedEvent extends ApplicationEvent {
/** /**
* Mark current event being processed * Mark current event being processed
*/ */
public void process() { public void processed() {
processed = true; processed = true;
} }