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:
commit
a3c8f2a037
@ -22,6 +22,7 @@ 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;
|
||||
@ -289,10 +290,12 @@ public class DubboServiceMetadataRepository
|
||||
serviceName);
|
||||
}
|
||||
|
||||
initSubscribedDubboMetadataService(serviceName);
|
||||
if (initSubscribedDubboMetadataService(serviceName)) {
|
||||
// mark this service name having been initialized
|
||||
initializedServices.add(serviceName);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -300,12 +303,19 @@ 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) {
|
||||
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 +624,47 @@ public class DubboServiceMetadataRepository
|
||||
subscribedServices.remove(currentApplicationName);
|
||||
}
|
||||
|
||||
protected void initSubscribedDubboMetadataService(String serviceName) {
|
||||
metadataServiceInstanceSelector.choose(discoveryClient.getInstances(serviceName))
|
||||
.map(this::getDubboMetadataServiceURLs)
|
||||
.ifPresent(dubboMetadataServiceURLs -> {
|
||||
dubboMetadataServiceURLs.forEach(dubboMetadataServiceURL -> {
|
||||
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<ServiceInstance> optionalServiceInstance = metadataServiceInstanceSelector
|
||||
.choose(discoveryClient.getInstances(serviceName));
|
||||
if (!((Optional) optionalServiceInstance).isPresent()) {
|
||||
return false;
|
||||
}
|
||||
ServiceInstance serviceInstance = optionalServiceInstance.get();
|
||||
if (null == serviceInstance) {
|
||||
return false;
|
||||
}
|
||||
List<URL> dubboMetadataServiceURLs = getDubboMetadataServiceURLs(serviceInstance);
|
||||
if (dubboMetadataServiceURLs.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (URL dubboMetadataServiceURL : dubboMetadataServiceURLs) {
|
||||
try {
|
||||
initSubscribedDubboMetadataServiceURL(
|
||||
initSubscribedDubboMetadataServiceURL(dubboMetadataServiceURL);
|
||||
DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy
|
||||
.getProxy(serviceName);
|
||||
if (dubboMetadataService == null) {
|
||||
dubboMetadataService = initDubboMetadataServiceProxy(
|
||||
dubboMetadataServiceURL);
|
||||
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);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void initSubscribedDubboMetadataServiceURL(URL dubboMetadataServiceURL) {
|
||||
@ -640,11 +673,13 @@ 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
|
||||
|
@ -48,9 +48,11 @@ 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;
|
||||
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;
|
||||
@ -161,6 +163,13 @@ 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);
|
||||
@ -228,10 +237,13 @@ 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
|
||||
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(
|
||||
@ -239,6 +251,18 @@ 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
|
||||
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}
|
||||
*/
|
||||
@ -250,6 +274,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);
|
||||
@ -301,7 +330,11 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user