From 2545e11c06a10c93ba1089442ecb422e0d8948ec Mon Sep 17 00:00:00 2001 From: liuchunguang Date: Fri, 20 Mar 2020 15:01:28 +0800 Subject: [PATCH 1/5] fix #1259 --- .../registry/AbstractSpringCloudRegistry.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java b/spring-cloud-starter-alibaba/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java index 998ff6cd..23fbb8cb 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java @@ -51,6 +51,7 @@ 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.SIDE_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; +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.ADMIN_PROTOCOL; import static org.springframework.util.StringUtils.hasText; @@ -226,10 +227,11 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { // fix https://github.com/alibaba/spring-cloud-alibaba/issues/753 // Re-obtain the latest list of available metadata address here, ip or port may change. // by https://github.com/wangzihaogithub - dubboMetadataConfigServiceProxy.removeProxy(serviceName); - repository.removeMetadataAndInitializedService(serviceName); - dubboGenericServiceFactory.destroy(serviceName); - repository.initializeMetadata(serviceName); + // When the last service provider is closed, 【fix 1259】while close the channel,when up a new provider then repository.initializeMetadata(serviceName) will throw Exception. + // dubboMetadataConfigServiceProxy.removeProxy(serviceName); + // repository.removeMetadataAndInitializedService(serviceName); + // dubboGenericServiceFactory.destroy(serviceName); + // repository.initializeMetadata(serviceName); if (CollectionUtils.isEmpty(serviceInstances)) { if (logger.isWarnEnabled()) { logger.warn( @@ -237,6 +239,9 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { + "available , please make sure the further impact", serviceName, url.getServiceKey()); } + dubboMetadataConfigServiceProxy.removeProxy(serviceName); + repository.removeMetadataAndInitializedService(serviceName); + dubboGenericServiceFactory.destroy(serviceName); /** * URLs with {@link RegistryConstants#EMPTY_PROTOCOL} */ @@ -299,7 +304,9 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { } private List emptyURLs(URL url) { - return asList(from(url).setProtocol(EMPTY_PROTOCOL).build()); + // issue : When the last service provider is closed, the client still periodically connects to the last provider.n + // fix https://github.com/alibaba/spring-cloud-alibaba/issues/1259 + return asList(from(url).setProtocol(EMPTY_PROTOCOL).removeParameter(CATEGORY_KEY).build()); } private List getServiceInstances(String serviceName) { From b56464c5c7271b2547245ba7ba3949be8bd5abe0 Mon Sep 17 00:00:00 2001 From: liuchunguang Date: Wed, 25 Mar 2020 18:16:13 +0800 Subject: [PATCH 2/5] fix #1259 #1260 #753 --- .../DubboServiceMetadataRepository.java | 117 ++++++++++++------ .../registry/AbstractSpringCloudRegistry.java | 42 ++++--- 2 files changed, 104 insertions(+), 55 deletions(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java index abd27b7f..d560f008 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java @@ -16,18 +16,6 @@ package com.alibaba.cloud.dubbo.metadata.repository; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import javax.annotation.PostConstruct; - import com.alibaba.cloud.dubbo.env.DubboCloudProperties; import com.alibaba.cloud.dubbo.http.matcher.RequestMetadataMatcher; import com.alibaba.cloud.dubbo.metadata.DubboRestServiceMetadata; @@ -58,15 +46,16 @@ import org.springframework.stereotype.Repository; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import javax.annotation.PostConstruct; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import static com.alibaba.cloud.dubbo.env.DubboCloudProperties.ALL_DUBBO_SERVICES; import static com.alibaba.cloud.dubbo.http.DefaultHttpRequest.builder; import static java.lang.String.format; import static java.lang.String.valueOf; -import static java.util.Collections.emptyList; -import static java.util.Collections.emptySet; -import static java.util.Collections.unmodifiableList; -import static java.util.Collections.unmodifiableMap; -import static java.util.Collections.unmodifiableSet; +import static java.util.Collections.*; import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; import static org.springframework.util.CollectionUtils.isEmpty; @@ -289,9 +278,12 @@ public class DubboServiceMetadataRepository serviceName); } - initSubscribedDubboMetadataService(serviceName); - // mark this service name having been initialized - initializedServices.add(serviceName); + + if (initSubscribedDubboMetadataService(serviceName)){ + // mark this service name having been initialized + initializedServices.add(serviceName); + } + } } } @@ -301,11 +293,17 @@ public class DubboServiceMetadataRepository * service instance. * @param serviceName the service name */ - public void removeMetadataAndInitializedService(String serviceName) { + public void removeMetadataAndInitializedService(String serviceName, URL url) { synchronized (monitor) { initializedServices.remove(serviceName); dubboRestServiceMetadataRepository.remove(serviceName); - subscribedDubboMetadataServiceURLs.remove(serviceName); + // fix #1260 if the subscribedDubboMetadataServiceURLs removed fail,old meta information will be retained + if( DubboMetadataService.class + .getName().equals(url.getServiceInterface() )) { + String serviceKey = url.getServiceKey(); + subscribedDubboMetadataServiceURLs.remove(serviceKey); + } + } } @@ -614,24 +612,66 @@ public class DubboServiceMetadataRepository subscribedServices.remove(currentApplicationName); } - protected void initSubscribedDubboMetadataService(String serviceName) { - metadataServiceInstanceSelector.choose(discoveryClient.getInstances(serviceName)) + protected Boolean initSubscribedDubboMetadataService(String serviceName) { + // this need to judge whether the initialization is successful or not. The failed initialization will not change the initializedServices + Optional optionalServiceInstance = metadataServiceInstanceSelector.choose(discoveryClient.getInstances(serviceName)); + if(!optionalServiceInstance.isPresent() ){ + return false; + } + ServiceInstance serviceInstance = optionalServiceInstance.get(); + if(null == serviceInstance ){ + return false; + } + List dubboMetadataServiceURLs = getDubboMetadataServiceURLs(serviceInstance); + if(dubboMetadataServiceURLs.isEmpty()){ + return false; + } + for(URL dubboMetadataServiceURL : dubboMetadataServiceURLs){ + try { + initSubscribedDubboMetadataServiceURL( + dubboMetadataServiceURL); + DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy.getProxy(serviceName); + if(dubboMetadataService == null){ + dubboMetadataService = initDubboMetadataServiceProxy(dubboMetadataServiceURL); + } + + if(dubboMetadataService == null){ + removeMetadataAndInitializedService(serviceName, dubboMetadataServiceURL); + return false; + } + } + catch (Throwable e) { + if (logger.isErrorEnabled()) { + logger.error(e.getMessage(), e); + } + } + } + /*metadataServiceInstanceSelector.choose(discoveryClient.getInstances(serviceName)) .map(this::getDubboMetadataServiceURLs) .ifPresent(dubboMetadataServiceURLs -> { - dubboMetadataServiceURLs.forEach(dubboMetadataServiceURL -> { - try { - initSubscribedDubboMetadataServiceURL( - dubboMetadataServiceURL); - initDubboMetadataServiceProxy(dubboMetadataServiceURL); - } - catch (Throwable e) { - if (logger.isErrorEnabled()) { - logger.error(e.getMessage(), e); + if( dubboMetadataServiceURLs.isEmpty()){ + initializedServices.remove(serviceName); + }else{ + dubboMetadataServiceURLs.forEach(dubboMetadataServiceURL -> { + try { + initSubscribedDubboMetadataServiceURL( + dubboMetadataServiceURL); + DubboMetadataService dubboMetadataService = initDubboMetadataServiceProxy(dubboMetadataServiceURL); + if(dubboMetadataService == null){ + initializedServices.remove(serviceName); + } } - } - }); - }); + catch (Throwable e) { + if (logger.isErrorEnabled()) { + logger.error(e.getMessage(), e); + } + } + }); + } + + });*/ initDubboRestServiceMetadataRepository(serviceName); + return true; } private void initSubscribedDubboMetadataServiceURL(URL dubboMetadataServiceURL) { @@ -640,11 +680,12 @@ public class DubboServiceMetadataRepository subscribedDubboMetadataServiceURLs.add(serviceKey, dubboMetadataServiceURL); } - private void initDubboMetadataServiceProxy(URL dubboMetadataServiceURL) { + private DubboMetadataService initDubboMetadataServiceProxy(URL dubboMetadataServiceURL) { String serviceName = dubboMetadataServiceURL.getParameter(APPLICATION_KEY); String version = dubboMetadataServiceURL.getParameter(VERSION_KEY); // Initialize DubboMetadataService with right version - dubboMetadataConfigServiceProxy.initProxy(serviceName, version); + return dubboMetadataConfigServiceProxy.initProxy(serviceName, version); + } @Override diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java index d9ae1eae..a5f8ab8e 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java @@ -16,14 +16,6 @@ package com.alibaba.cloud.dubbo.registry; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.function.Function; -import java.util.stream.Collectors; - import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository; import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent; import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory; @@ -36,21 +28,20 @@ import org.apache.dubbo.registry.RegistryFactory; import org.apache.dubbo.registry.support.FailbackRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.util.CollectionUtils; +import java.util.*; +import java.util.function.Function; +import java.util.stream.Collectors; + import static java.util.Arrays.asList; 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.PROTOCOL_KEY; -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.VERSION_KEY; +import static org.apache.dubbo.common.constants.CommonConstants.*; 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.ADMIN_PROTOCOL; @@ -162,6 +153,11 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { } else if (isDubboMetadataServiceURL(url)) { // for DubboMetadataService subscribeDubboMetadataServiceURLs(url, listener); + if( from(url).getParameter(CATEGORY_KEY) != null && from(url).getParameter(CATEGORY_KEY).contains(PROVIDER)){ + // Fix #1259 and #753 Listene meta service change events to remove useless clients + registerServiceInstancesChangedEventListener(url, listener); + } + } else { // for general Dubbo Services subscribeDubboServiceURLs(url, listener); @@ -241,9 +237,16 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { + "available , please make sure the further impact", serviceName, url.getServiceKey()); } - dubboMetadataConfigServiceProxy.removeProxy(serviceName); - repository.removeMetadataAndInitializedService(serviceName); - dubboGenericServiceFactory.destroy(serviceName); + if(isDubboMetadataServiceURL(url)){ + // if meta service change, and serviceInstances is zero, will clean up information about this client + dubboMetadataConfigServiceProxy.removeProxy(serviceName); + repository.removeMetadataAndInitializedService(serviceName, url); + dubboGenericServiceFactory.destroy(serviceName); + String listenerId = generateId(url); + // The metaservice will restart the new listener. It needs to be optimized to see whether the original listener can be reused. + this.registerListeners.remove(listenerId); + } + /** * URLs with {@link RegistryConstants#EMPTY_PROTOCOL} */ @@ -255,6 +258,11 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { listener.notify(allSubscribedURLs); return; } + if( isDubboMetadataServiceURL(url) ){ + // Prevent duplicate generation of DubboMetadataService + return; + } + repository.initializeMetadata(serviceName); DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy .getProxy(serviceName); From 0485c6d06d9ce0b7aaaa9f5cc90b97ffd62e78e5 Mon Sep 17 00:00:00 2001 From: liuchunguang Date: Thu, 26 Mar 2020 09:33:28 +0800 Subject: [PATCH 3/5] fix #1259 #1260 #753 checkStyle --- .../DubboServiceMetadataRepository.java | 66 +++++++------------ .../registry/AbstractSpringCloudRegistry.java | 26 +++++--- 2 files changed, 40 insertions(+), 52 deletions(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java index d560f008..fc5c0c29 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java @@ -278,8 +278,7 @@ public class DubboServiceMetadataRepository serviceName); } - - if (initSubscribedDubboMetadataService(serviceName)){ + if (initSubscribedDubboMetadataService(serviceName)) { // mark this service name having been initialized initializedServices.add(serviceName); } @@ -297,9 +296,9 @@ public class DubboServiceMetadataRepository synchronized (monitor) { initializedServices.remove(serviceName); dubboRestServiceMetadataRepository.remove(serviceName); - // fix #1260 if the subscribedDubboMetadataServiceURLs removed fail,old meta information will be retained - if( DubboMetadataService.class - .getName().equals(url.getServiceInterface() )) { + // fix #1260 if the subscribedDubboMetadataServiceURLs removed fail,old meta + // information will be retained + if (DubboMetadataService.class.getName().equals(url.getServiceInterface())) { String serviceKey = url.getServiceKey(); subscribedDubboMetadataServiceURLs.remove(serviceKey); } @@ -613,30 +612,34 @@ public class DubboServiceMetadataRepository } protected Boolean initSubscribedDubboMetadataService(String serviceName) { - // this need to judge whether the initialization is successful or not. The failed initialization will not change the initializedServices - Optional optionalServiceInstance = metadataServiceInstanceSelector.choose(discoveryClient.getInstances(serviceName)); - if(!optionalServiceInstance.isPresent() ){ + // this need to judge whether the initialization is successful or not. The failed + // initialization will not change the initializedServices + Optional optionalServiceInstance = metadataServiceInstanceSelector + .choose(discoveryClient.getInstances(serviceName)); + if (!optionalServiceInstance.isPresent()) { return false; } ServiceInstance serviceInstance = optionalServiceInstance.get(); - if(null == serviceInstance ){ + if (null == serviceInstance) { return false; } List dubboMetadataServiceURLs = getDubboMetadataServiceURLs(serviceInstance); - if(dubboMetadataServiceURLs.isEmpty()){ + if (dubboMetadataServiceURLs.isEmpty()) { return false; } - for(URL dubboMetadataServiceURL : dubboMetadataServiceURLs){ + for (URL dubboMetadataServiceURL : dubboMetadataServiceURLs) { try { - initSubscribedDubboMetadataServiceURL( - dubboMetadataServiceURL); - DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy.getProxy(serviceName); - if(dubboMetadataService == null){ - dubboMetadataService = initDubboMetadataServiceProxy(dubboMetadataServiceURL); + initSubscribedDubboMetadataServiceURL(dubboMetadataServiceURL); + DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy + .getProxy(serviceName); + if (dubboMetadataService == null) { + dubboMetadataService = initDubboMetadataServiceProxy( + dubboMetadataServiceURL); } - if(dubboMetadataService == null){ - removeMetadataAndInitializedService(serviceName, dubboMetadataServiceURL); + if (dubboMetadataService == null) { + removeMetadataAndInitializedService(serviceName, + dubboMetadataServiceURL); return false; } } @@ -646,30 +649,6 @@ public class DubboServiceMetadataRepository } } } - /*metadataServiceInstanceSelector.choose(discoveryClient.getInstances(serviceName)) - .map(this::getDubboMetadataServiceURLs) - .ifPresent(dubboMetadataServiceURLs -> { - if( dubboMetadataServiceURLs.isEmpty()){ - initializedServices.remove(serviceName); - }else{ - dubboMetadataServiceURLs.forEach(dubboMetadataServiceURL -> { - try { - initSubscribedDubboMetadataServiceURL( - dubboMetadataServiceURL); - DubboMetadataService dubboMetadataService = initDubboMetadataServiceProxy(dubboMetadataServiceURL); - if(dubboMetadataService == null){ - initializedServices.remove(serviceName); - } - } - catch (Throwable e) { - if (logger.isErrorEnabled()) { - logger.error(e.getMessage(), e); - } - } - }); - } - - });*/ initDubboRestServiceMetadataRepository(serviceName); return true; } @@ -680,7 +659,8 @@ public class DubboServiceMetadataRepository subscribedDubboMetadataServiceURLs.add(serviceKey, dubboMetadataServiceURL); } - private DubboMetadataService initDubboMetadataServiceProxy(URL dubboMetadataServiceURL) { + private DubboMetadataService initDubboMetadataServiceProxy( + URL dubboMetadataServiceURL) { String serviceName = dubboMetadataServiceURL.getParameter(APPLICATION_KEY); String version = dubboMetadataServiceURL.getParameter(VERSION_KEY); // Initialize DubboMetadataService with right version diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java index a5f8ab8e..8fe31778 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java @@ -153,8 +153,10 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { } else if (isDubboMetadataServiceURL(url)) { // for DubboMetadataService subscribeDubboMetadataServiceURLs(url, listener); - if( from(url).getParameter(CATEGORY_KEY) != null && from(url).getParameter(CATEGORY_KEY).contains(PROVIDER)){ - // Fix #1259 and #753 Listene meta service change events to remove useless clients + if (from(url).getParameter(CATEGORY_KEY) != null + && from(url).getParameter(CATEGORY_KEY).contains(PROVIDER)) { + // Fix #1259 and #753 Listene meta service change events to remove useless + // clients registerServiceInstancesChangedEventListener(url, listener); } @@ -225,7 +227,9 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { // Re-obtain the latest list of available metadata address here, ip or port may // change. // by https://github.com/wangzihaogithub - // When the last service provider is closed, 【fix 1259】while close the channel,when up a new provider then repository.initializeMetadata(serviceName) will throw Exception. + // When the last service provider is closed, 【fix 1259】while close the + // channel,when up a new provider then repository.initializeMetadata(serviceName) + // will throw Exception. // dubboMetadataConfigServiceProxy.removeProxy(serviceName); // repository.removeMetadataAndInitializedService(serviceName); // dubboGenericServiceFactory.destroy(serviceName); @@ -237,13 +241,15 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { + "available , please make sure the further impact", serviceName, url.getServiceKey()); } - if(isDubboMetadataServiceURL(url)){ - // if meta service change, and serviceInstances is zero, will clean up information about this client + if (isDubboMetadataServiceURL(url)) { + // if meta service change, and serviceInstances is zero, will clean up + // information about this client dubboMetadataConfigServiceProxy.removeProxy(serviceName); repository.removeMetadataAndInitializedService(serviceName, url); dubboGenericServiceFactory.destroy(serviceName); String listenerId = generateId(url); - // The metaservice will restart the new listener. It needs to be optimized to see whether the original listener can be reused. + // The metaservice will restart the new listener. It needs to be optimized + // to see whether the original listener can be reused. this.registerListeners.remove(listenerId); } @@ -258,7 +264,7 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { listener.notify(allSubscribedURLs); return; } - if( isDubboMetadataServiceURL(url) ){ + if (isDubboMetadataServiceURL(url)) { // Prevent duplicate generation of DubboMetadataService return; } @@ -314,9 +320,11 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry { } private List emptyURLs(URL url) { - // issue : When the last service provider is closed, the client still periodically connects to the last provider.n + // issue : When the last service provider is closed, the client still periodically + // connects to the last provider.n // fix https://github.com/alibaba/spring-cloud-alibaba/issues/1259 - return asList(from(url).setProtocol(EMPTY_PROTOCOL).removeParameter(CATEGORY_KEY).build()); + return asList(from(url).setProtocol(EMPTY_PROTOCOL).removeParameter(CATEGORY_KEY) + .build()); } private List getServiceInstances(String serviceName) { From 07475573301bbfdf84c437fb7b54fa524299c360 Mon Sep 17 00:00:00 2001 From: liuchunguang Date: Thu, 26 Mar 2020 10:07:46 +0800 Subject: [PATCH 4/5] fix #1259 #1260 #753 checkStyle --- .../DubboServiceMetadataRepository.java | 27 ++++++++++++++----- .../registry/AbstractSpringCloudRegistry.java | 20 ++++++++++---- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java index fc5c0c29..fdc779b5 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java @@ -16,6 +16,19 @@ package com.alibaba.cloud.dubbo.metadata.repository; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import javax.annotation.PostConstruct; + import com.alibaba.cloud.dubbo.env.DubboCloudProperties; import com.alibaba.cloud.dubbo.http.matcher.RequestMetadataMatcher; import com.alibaba.cloud.dubbo.metadata.DubboRestServiceMetadata; @@ -46,16 +59,15 @@ import org.springframework.stereotype.Repository; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import javax.annotation.PostConstruct; -import java.util.*; -import java.util.stream.Collectors; -import java.util.stream.Stream; - import static com.alibaba.cloud.dubbo.env.DubboCloudProperties.ALL_DUBBO_SERVICES; import static com.alibaba.cloud.dubbo.http.DefaultHttpRequest.builder; import static java.lang.String.format; import static java.lang.String.valueOf; -import static java.util.Collections.*; +import static java.util.Collections.emptyList; +import static java.util.Collections.emptySet; +import static java.util.Collections.unmodifiableList; +import static java.util.Collections.unmodifiableMap; +import static java.util.Collections.unmodifiableSet; import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; import static org.springframework.util.CollectionUtils.isEmpty; @@ -291,6 +303,7 @@ public class DubboServiceMetadataRepository * Remove the metadata and initialized service of Dubbo Services if no there is no * service instance. * @param serviceName the service name + * @param url the meta service url */ public void removeMetadataAndInitializedService(String serviceName, URL url) { synchronized (monitor) { @@ -616,7 +629,7 @@ public class DubboServiceMetadataRepository // initialization will not change the initializedServices Optional optionalServiceInstance = metadataServiceInstanceSelector .choose(discoveryClient.getInstances(serviceName)); - if (!optionalServiceInstance.isPresent()) { + if (!((Optional) optionalServiceInstance).isPresent()) { return false; } ServiceInstance serviceInstance = optionalServiceInstance.get(); diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java index 8fe31778..8676085c 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/registry/AbstractSpringCloudRegistry.java @@ -16,6 +16,14 @@ package com.alibaba.cloud.dubbo.registry; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository; import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent; import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory; @@ -28,20 +36,22 @@ import org.apache.dubbo.registry.RegistryFactory; import org.apache.dubbo.registry.support.FailbackRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; + import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.util.CollectionUtils; -import java.util.*; -import java.util.function.Function; -import java.util.stream.Collectors; - import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.apache.dubbo.common.URLBuilder.from; -import static org.apache.dubbo.common.constants.CommonConstants.*; +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.PROVIDER; +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.VERSION_KEY; 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.ADMIN_PROTOCOL; From 264e208da706d66481ee89142003b4fcc1e2df77 Mon Sep 17 00:00:00 2001 From: liuchunguang Date: Thu, 26 Mar 2020 20:06:12 +0800 Subject: [PATCH 5/5] initSubscribedDubboMetadataService Exception return false --- .../metadata/repository/DubboServiceMetadataRepository.java | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java index fdc779b5..9089919e 100644 --- a/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java +++ b/spring-cloud-alibaba-starters/spring-cloud-starter-dubbo/src/main/java/com/alibaba/cloud/dubbo/metadata/repository/DubboServiceMetadataRepository.java @@ -660,6 +660,7 @@ public class DubboServiceMetadataRepository if (logger.isErrorEnabled()) { logger.error(e.getMessage(), e); } + return false; } } initDubboRestServiceMetadataRepository(serviceName);