1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00
This commit is contained in:
liuchunguang 2020-03-25 18:16:13 +08:00
parent e5188e63d9
commit b56464c5c7
2 changed files with 104 additions and 55 deletions

View File

@ -16,18 +16,6 @@
package com.alibaba.cloud.dubbo.metadata.repository; 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.env.DubboCloudProperties;
import com.alibaba.cloud.dubbo.http.matcher.RequestMetadataMatcher; import com.alibaba.cloud.dubbo.http.matcher.RequestMetadataMatcher;
import com.alibaba.cloud.dubbo.metadata.DubboRestServiceMetadata; 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.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap; 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.env.DubboCloudProperties.ALL_DUBBO_SERVICES;
import static com.alibaba.cloud.dubbo.http.DefaultHttpRequest.builder; import static com.alibaba.cloud.dubbo.http.DefaultHttpRequest.builder;
import static java.lang.String.format; import static java.lang.String.format;
import static java.lang.String.valueOf; import static java.lang.String.valueOf;
import static java.util.Collections.emptyList; import static java.util.Collections.*;
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.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY; import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.springframework.util.CollectionUtils.isEmpty; import static org.springframework.util.CollectionUtils.isEmpty;
@ -289,9 +278,12 @@ public class DubboServiceMetadataRepository
serviceName); serviceName);
} }
initSubscribedDubboMetadataService(serviceName);
// mark this service name having been initialized if (initSubscribedDubboMetadataService(serviceName)){
initializedServices.add(serviceName); // mark this service name having been initialized
initializedServices.add(serviceName);
}
} }
} }
} }
@ -301,11 +293,17 @@ public class DubboServiceMetadataRepository
* service instance. * service instance.
* @param serviceName the service name * @param serviceName the service name
*/ */
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 +612,66 @@ 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 initialization will not change the initializedServices
Optional<ServiceInstance> optionalServiceInstance = metadataServiceInstanceSelector.choose(discoveryClient.getInstances(serviceName));
if(!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(
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) .map(this::getDubboMetadataServiceURLs)
.ifPresent(dubboMetadataServiceURLs -> { .ifPresent(dubboMetadataServiceURLs -> {
dubboMetadataServiceURLs.forEach(dubboMetadataServiceURL -> { if( dubboMetadataServiceURLs.isEmpty()){
try { initializedServices.remove(serviceName);
initSubscribedDubboMetadataServiceURL( }else{
dubboMetadataServiceURL); dubboMetadataServiceURLs.forEach(dubboMetadataServiceURL -> {
initDubboMetadataServiceProxy(dubboMetadataServiceURL); try {
} initSubscribedDubboMetadataServiceURL(
catch (Throwable e) { dubboMetadataServiceURL);
if (logger.isErrorEnabled()) { DubboMetadataService dubboMetadataService = initDubboMetadataServiceProxy(dubboMetadataServiceURL);
logger.error(e.getMessage(), e); if(dubboMetadataService == null){
initializedServices.remove(serviceName);
}
} }
} catch (Throwable e) {
}); if (logger.isErrorEnabled()) {
}); logger.error(e.getMessage(), e);
}
}
});
}
});*/
initDubboRestServiceMetadataRepository(serviceName); initDubboRestServiceMetadataRepository(serviceName);
return true;
} }
private void initSubscribedDubboMetadataServiceURL(URL dubboMetadataServiceURL) { private void initSubscribedDubboMetadataServiceURL(URL dubboMetadataServiceURL) {
@ -640,11 +680,12 @@ 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

@ -16,14 +16,6 @@
package com.alibaba.cloud.dubbo.registry; 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.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent; import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent;
import com.alibaba.cloud.dubbo.service.DubboGenericServiceFactory; 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.apache.dubbo.registry.support.FailbackRegistry;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance; 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 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.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.URLBuilder.from;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY; import static org.apache.dubbo.common.constants.CommonConstants.*;
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.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;
@ -162,6 +153,11 @@ 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);
@ -241,9 +237,16 @@ 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());
} }
dubboMetadataConfigServiceProxy.removeProxy(serviceName); if(isDubboMetadataServiceURL(url)){
repository.removeMetadataAndInitializedService(serviceName); // if meta service change, and serviceInstances is zero, will clean up information about this client
dubboGenericServiceFactory.destroy(serviceName); 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}
*/ */
@ -255,6 +258,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);