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

Merge pull request #1286 from lcg72/master

Fixes #1259 #1260 #753 #1253 Solve the problem of meta data update
This commit is contained in:
Mercy Ma 2020-04-01 11:37:49 +08:00 committed by GitHub
commit a3c8f2a037
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 29 deletions

View File

@ -22,6 +22,7 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -289,9 +290,11 @@ public class DubboServiceMetadataRepository
serviceName); serviceName);
} }
initSubscribedDubboMetadataService(serviceName); if (initSubscribedDubboMetadataService(serviceName)) {
// mark this service name having been initialized // mark this service name having been initialized
initializedServices.add(serviceName); initializedServices.add(serviceName);
}
} }
} }
} }
@ -300,12 +303,19 @@ public class DubboServiceMetadataRepository
* Remove the metadata and initialized service of Dubbo Services if no there is no * Remove the metadata and initialized service of Dubbo Services if no there is no
* service instance. * service instance.
* @param serviceName the service name * @param serviceName the service name
* @param url the meta service url
*/ */
public void removeMetadataAndInitializedService(String serviceName) { public void removeMetadataAndInitializedService(String serviceName, URL url) {
synchronized (monitor) { synchronized (monitor) {
initializedServices.remove(serviceName); initializedServices.remove(serviceName);
dubboRestServiceMetadataRepository.remove(serviceName); dubboRestServiceMetadataRepository.remove(serviceName);
subscribedDubboMetadataServiceURLs.remove(serviceName); // fix #1260 if the subscribedDubboMetadataServiceURLs removed failold meta
// information will be retained
if (DubboMetadataService.class.getName().equals(url.getServiceInterface())) {
String serviceKey = url.getServiceKey();
subscribedDubboMetadataServiceURLs.remove(serviceKey);
}
} }
} }
@ -614,24 +624,47 @@ public class DubboServiceMetadataRepository
subscribedServices.remove(currentApplicationName); subscribedServices.remove(currentApplicationName);
} }
protected void initSubscribedDubboMetadataService(String serviceName) { protected Boolean initSubscribedDubboMetadataService(String serviceName) {
metadataServiceInstanceSelector.choose(discoveryClient.getInstances(serviceName)) // this need to judge whether the initialization is successful or not. The failed
.map(this::getDubboMetadataServiceURLs) // initialization will not change the initializedServices
.ifPresent(dubboMetadataServiceURLs -> { Optional<ServiceInstance> optionalServiceInstance = metadataServiceInstanceSelector
dubboMetadataServiceURLs.forEach(dubboMetadataServiceURL -> { .choose(discoveryClient.getInstances(serviceName));
try { if (!((Optional) optionalServiceInstance).isPresent()) {
initSubscribedDubboMetadataServiceURL( return false;
dubboMetadataServiceURL); }
initDubboMetadataServiceProxy(dubboMetadataServiceURL); ServiceInstance serviceInstance = optionalServiceInstance.get();
} if (null == serviceInstance) {
catch (Throwable e) { return false;
if (logger.isErrorEnabled()) { }
logger.error(e.getMessage(), e); List<URL> 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);
}
return false;
}
}
initDubboRestServiceMetadataRepository(serviceName); initDubboRestServiceMetadataRepository(serviceName);
return true;
} }
private void initSubscribedDubboMetadataServiceURL(URL dubboMetadataServiceURL) { private void initSubscribedDubboMetadataServiceURL(URL dubboMetadataServiceURL) {
@ -640,11 +673,13 @@ public class DubboServiceMetadataRepository
subscribedDubboMetadataServiceURLs.add(serviceKey, dubboMetadataServiceURL); subscribedDubboMetadataServiceURLs.add(serviceKey, dubboMetadataServiceURL);
} }
private void initDubboMetadataServiceProxy(URL dubboMetadataServiceURL) { private DubboMetadataService initDubboMetadataServiceProxy(
URL dubboMetadataServiceURL) {
String serviceName = dubboMetadataServiceURL.getParameter(APPLICATION_KEY); String serviceName = dubboMetadataServiceURL.getParameter(APPLICATION_KEY);
String version = dubboMetadataServiceURL.getParameter(VERSION_KEY); String version = dubboMetadataServiceURL.getParameter(VERSION_KEY);
// Initialize DubboMetadataService with right version // Initialize DubboMetadataService with right version
dubboMetadataConfigServiceProxy.initProxy(serviceName, version); return dubboMetadataConfigServiceProxy.initProxy(serviceName, version);
} }
@Override @Override

View File

@ -48,9 +48,11 @@ import static java.util.Collections.emptyList;
import static org.apache.dubbo.common.URLBuilder.from; 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;
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.CATEGORY_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL; 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;
@ -161,6 +163,13 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
} }
else if (isDubboMetadataServiceURL(url)) { // for DubboMetadataService else if (isDubboMetadataServiceURL(url)) { // for DubboMetadataService
subscribeDubboMetadataServiceURLs(url, listener); 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 else { // for general Dubbo Services
subscribeDubboServiceURLs(url, listener); subscribeDubboServiceURLs(url, listener);
@ -228,10 +237,13 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
// Re-obtain the latest list of available metadata address here, ip or port may // Re-obtain the latest list of available metadata address here, ip or port may
// change. // change.
// by https://github.com/wangzihaogithub // by https://github.com/wangzihaogithub
dubboMetadataConfigServiceProxy.removeProxy(serviceName); // When the last service provider is closed, fix 1259while close the
repository.removeMetadataAndInitializedService(serviceName); // channelwhen up a new provider then repository.initializeMetadata(serviceName)
dubboGenericServiceFactory.destroy(serviceName); // will throw Exception.
repository.initializeMetadata(serviceName); // dubboMetadataConfigServiceProxy.removeProxy(serviceName);
// repository.removeMetadataAndInitializedService(serviceName);
// dubboGenericServiceFactory.destroy(serviceName);
// repository.initializeMetadata(serviceName);
if (CollectionUtils.isEmpty(serviceInstances)) { if (CollectionUtils.isEmpty(serviceInstances)) {
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
logger.warn( logger.warn(
@ -239,6 +251,18 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
+ "available , please make sure the further impact", + "available , please make sure the further impact",
serviceName, url.getServiceKey()); serviceName, url.getServiceKey());
} }
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} * URLs with {@link RegistryConstants#EMPTY_PROTOCOL}
*/ */
@ -250,6 +274,11 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
listener.notify(allSubscribedURLs); listener.notify(allSubscribedURLs);
return; return;
} }
if (isDubboMetadataServiceURL(url)) {
// Prevent duplicate generation of DubboMetadataService
return;
}
repository.initializeMetadata(serviceName);
DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy
.getProxy(serviceName); .getProxy(serviceName);
@ -301,7 +330,11 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
} }
private List<URL> emptyURLs(URL url) { private List<URL> 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<ServiceInstance> getServiceInstances(String serviceName) { private List<ServiceInstance> getServiceInstances(String serviceName) {