|
|
|
@ -18,44 +18,35 @@ package org.springframework.cloud.alibaba.dubbo.registry;
|
|
|
|
|
|
|
|
|
|
import org.apache.dubbo.common.Constants;
|
|
|
|
|
import org.apache.dubbo.common.URL;
|
|
|
|
|
import org.apache.dubbo.common.utils.NamedThreadFactory;
|
|
|
|
|
import org.apache.dubbo.common.utils.UrlUtils;
|
|
|
|
|
import org.apache.dubbo.registry.NotifyListener;
|
|
|
|
|
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.alibaba.dubbo.registry.handler.DubboRegistryServiceIdHandler;
|
|
|
|
|
import org.springframework.cloud.client.ServiceInstance;
|
|
|
|
|
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|
|
|
|
import org.springframework.cloud.client.serviceregistry.Registration;
|
|
|
|
|
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
import org.springframework.core.ResolvableType;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import static com.alibaba.dubbo.common.Constants.CONFIGURATORS_CATEGORY;
|
|
|
|
|
import static com.alibaba.dubbo.common.Constants.CONSUMERS_CATEGORY;
|
|
|
|
|
import static com.alibaba.dubbo.common.Constants.PROVIDERS_CATEGORY;
|
|
|
|
|
import static com.alibaba.dubbo.common.Constants.ROUTERS_CATEGORY;
|
|
|
|
|
import static java.lang.Long.getLong;
|
|
|
|
|
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
|
|
|
|
|
import static org.springframework.beans.BeanUtils.instantiateClass;
|
|
|
|
|
import static org.springframework.core.ResolvableType.forInstance;
|
|
|
|
|
import static org.springframework.core.ResolvableType.forType;
|
|
|
|
|
import static org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames;
|
|
|
|
|
import static org.springframework.util.ClassUtils.isPresent;
|
|
|
|
|
import static org.springframework.util.ClassUtils.resolveClassName;
|
|
|
|
|
import static java.util.Collections.singletonList;
|
|
|
|
|
import static org.apache.dubbo.common.Constants.CONFIGURATORS_CATEGORY;
|
|
|
|
|
import static org.apache.dubbo.common.Constants.CONSUMERS_CATEGORY;
|
|
|
|
|
import static org.apache.dubbo.common.Constants.PROVIDERS_CATEGORY;
|
|
|
|
|
import static org.apache.dubbo.common.Constants.PROVIDER_SIDE;
|
|
|
|
|
import static org.apache.dubbo.common.Constants.ROUTERS_CATEGORY;
|
|
|
|
|
import static org.apache.dubbo.common.Constants.SIDE_KEY;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration abstraction, whose protocol is "spring-cloud"
|
|
|
|
@ -64,169 +55,96 @@ import static org.springframework.util.ClassUtils.resolveClassName;
|
|
|
|
|
*/
|
|
|
|
|
public class SpringCloudRegistry extends FailbackRegistry {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The parameter name of {@link #allServicesLookupInterval}
|
|
|
|
|
*/
|
|
|
|
|
public static final String ALL_SERVICES_LOOKUP_INTERVAL_PARAM_NAME = "dubbo.all.services.lookup.interval";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The parameter name of {@link #registeredServicesLookupInterval}
|
|
|
|
|
*/
|
|
|
|
|
public static final String REGISTERED_SERVICES_LOOKUP_INTERVAL_PARAM_NAME = "dubbo.registered.services.lookup.interval";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* All supported categories
|
|
|
|
|
*/
|
|
|
|
|
private static final String[] ALL_SUPPORTED_CATEGORIES = of(
|
|
|
|
|
public static final String[] ALL_SUPPORTED_CATEGORIES = of(
|
|
|
|
|
PROVIDERS_CATEGORY,
|
|
|
|
|
CONSUMERS_CATEGORY,
|
|
|
|
|
ROUTERS_CATEGORY,
|
|
|
|
|
CONFIGURATORS_CATEGORY
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
private static final int CATEGORY_INDEX = 0;
|
|
|
|
|
|
|
|
|
|
private static final int SERVICE_INTERFACE_INDEX = CATEGORY_INDEX + 1;
|
|
|
|
|
|
|
|
|
|
private static final int SERVICE_VERSION_INDEX = SERVICE_INTERFACE_INDEX + 1;
|
|
|
|
|
|
|
|
|
|
private static final int SERVICE_GROUP_INDEX = SERVICE_VERSION_INDEX + 1;
|
|
|
|
|
|
|
|
|
|
private static final String WILDCARD = "*";
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The interval in second of lookup service names(only for Dubbo-OPS)
|
|
|
|
|
*/
|
|
|
|
|
private static final long ALL_SERVICES_LOOKUP_INTERVAL = getLong("dubbo.all.services.lookup.interval", 30);
|
|
|
|
|
private final long allServicesLookupInterval;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The interval in second of lookup regigered service instances
|
|
|
|
|
*/
|
|
|
|
|
private static final long REGISTERED_SERVICES_LOOKUP_INTERVAL = getLong("dubbo.registered.services.lookup.interval", 300);
|
|
|
|
|
private final long registeredServicesLookupInterval;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The {@link ScheduledExecutorService Scheduler} to lookup the registered services
|
|
|
|
|
*/
|
|
|
|
|
private static final ScheduledExecutorService registeredServicesLookupScheduler = newSingleThreadScheduledExecutor(new NamedThreadFactory("dubbo-registered-services-lookup-"));
|
|
|
|
|
private final ServiceRegistry<Registration> serviceRegistry;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The {@link ScheduledExecutorService Scheduler} to lookup all services (only for Dubbo-OPS)
|
|
|
|
|
*/
|
|
|
|
|
private static volatile ScheduledExecutorService allServicesLookupScheduler;
|
|
|
|
|
private final RegistrationFactory registrationFactory;
|
|
|
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
private final DiscoveryClient discoveryClient;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The separator for service name
|
|
|
|
|
*/
|
|
|
|
|
private static final String SERVICE_NAME_SEPARATOR = ":";
|
|
|
|
|
private final DubboRegistryServiceIdHandler dubboRegistryServiceIdHandler;
|
|
|
|
|
|
|
|
|
|
private final ApplicationContext applicationContext;
|
|
|
|
|
private final ScheduledExecutorService servicesLookupScheduler;
|
|
|
|
|
|
|
|
|
|
private final ServiceRegistry<Registration> serviceRegistry;
|
|
|
|
|
private final ConfigurableApplicationContext applicationContext;
|
|
|
|
|
|
|
|
|
|
private final DiscoveryClient discoveryClient;
|
|
|
|
|
|
|
|
|
|
private final RegistrationFactory registrationFactory;
|
|
|
|
|
|
|
|
|
|
public SpringCloudRegistry(URL url, ApplicationContext applicationContext) {
|
|
|
|
|
public SpringCloudRegistry(URL url,
|
|
|
|
|
ServiceRegistry<Registration> serviceRegistry,
|
|
|
|
|
RegistrationFactory registrationFactory,
|
|
|
|
|
DiscoveryClient discoveryClient,
|
|
|
|
|
ScheduledExecutorService servicesLookupScheduler,
|
|
|
|
|
ConfigurableApplicationContext applicationContext) {
|
|
|
|
|
super(url);
|
|
|
|
|
this.allServicesLookupInterval = url.getParameter(ALL_SERVICES_LOOKUP_INTERVAL_PARAM_NAME, 30L);
|
|
|
|
|
this.registeredServicesLookupInterval = url.getParameter(REGISTERED_SERVICES_LOOKUP_INTERVAL_PARAM_NAME, 300L);
|
|
|
|
|
this.serviceRegistry = serviceRegistry;
|
|
|
|
|
this.registrationFactory = registrationFactory;
|
|
|
|
|
this.discoveryClient = discoveryClient;
|
|
|
|
|
this.dubboRegistryServiceIdHandler = applicationContext.getBean(DubboRegistryServiceIdHandler.class);
|
|
|
|
|
this.applicationContext = applicationContext;
|
|
|
|
|
this.serviceRegistry = applicationContext.getBean(ServiceRegistry.class);
|
|
|
|
|
this.registrationFactory = buildRegistrationFactory(serviceRegistry, applicationContext.getClassLoader());
|
|
|
|
|
this.discoveryClient = applicationContext.getBean(DiscoveryClient.class);
|
|
|
|
|
applicationContext.getClassLoader();
|
|
|
|
|
this.servicesLookupScheduler = servicesLookupScheduler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RegistrationFactory buildRegistrationFactory(ServiceRegistry<Registration> serviceRegistry,
|
|
|
|
|
ClassLoader classLoader) {
|
|
|
|
|
RegistrationFactory registrationFactory = null;
|
|
|
|
|
List<String> factoryClassNames = loadFactoryNames(RegistrationFactory.class, classLoader);
|
|
|
|
|
|
|
|
|
|
ResolvableType serviceRegistryType = forInstance(serviceRegistry);
|
|
|
|
|
// Get first generic Class
|
|
|
|
|
Class<?> registrationClass = resolveGenericClass(serviceRegistryType, ServiceRegistry.class, 0);
|
|
|
|
|
|
|
|
|
|
for (String factoryClassName : factoryClassNames) {
|
|
|
|
|
if (isPresent(factoryClassName, classLoader)) { // ignore compilation issue
|
|
|
|
|
Class<?> factoryClass = resolveClassName(factoryClassName, classLoader);
|
|
|
|
|
ResolvableType registrationFactoryType = forType(factoryClass);
|
|
|
|
|
Class<?> actualRegistrationClass = resolveGenericClass(registrationFactoryType, RegistrationFactory.class, 0);
|
|
|
|
|
if (registrationClass.equals(actualRegistrationClass)) {
|
|
|
|
|
registrationFactory = (RegistrationFactory) instantiateClass(registrationFactoryType.getRawClass());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (registrationFactory == null) {
|
|
|
|
|
|
|
|
|
|
if (logger.isWarnEnabled()) {
|
|
|
|
|
logger.warn("{} implementation can't be resolved by ServiceRegistry[{}]",
|
|
|
|
|
registrationClass.getSimpleName(), serviceRegistry.getClass().getName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registrationFactory = new DefaultRegistrationFactory();
|
|
|
|
|
} else {
|
|
|
|
|
if (logger.isInfoEnabled()) {
|
|
|
|
|
logger.info("{} has been resolved by ServiceRegistry[{}]",
|
|
|
|
|
registrationFactory.getClass().getName(), serviceRegistry.getClass().getName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return registrationFactory;
|
|
|
|
|
protected boolean shouldRegister(Registration registration) {
|
|
|
|
|
Map<String, String> metadata = registration.getMetadata();
|
|
|
|
|
String side = metadata.get(SIDE_KEY);
|
|
|
|
|
return PROVIDER_SIDE.equals(side); // Only register the Provider.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Class<?> resolveGenericClass(ResolvableType implementedType, Class<?> interfaceClass, int index) {
|
|
|
|
|
|
|
|
|
|
ResolvableType resolvableType = implementedType;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
OUTER:
|
|
|
|
|
while (true) {
|
|
|
|
|
|
|
|
|
|
ResolvableType[] interfaceTypes = resolvableType.getInterfaces();
|
|
|
|
|
|
|
|
|
|
for (ResolvableType interfaceType : interfaceTypes) {
|
|
|
|
|
if (interfaceType.resolve().equals(interfaceClass)) {
|
|
|
|
|
resolvableType = interfaceType;
|
|
|
|
|
break OUTER;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ResolvableType superType = resolvableType.getSuperType();
|
|
|
|
|
|
|
|
|
|
Class<?> superClass = superType.resolve();
|
|
|
|
|
|
|
|
|
|
if (Object.class.equals(superClass)) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resolvableType = superType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (Throwable e) {
|
|
|
|
|
resolvableType = ResolvableType.forType(void.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resolvableType.resolveGeneric(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void doRegister(URL url) {
|
|
|
|
|
final String serviceName = getServiceName(url);
|
|
|
|
|
final Registration registration = createRegistration(serviceName, url);
|
|
|
|
|
serviceRegistry.register(registration);
|
|
|
|
|
final Registration registration = createRegistration(url);
|
|
|
|
|
if (shouldRegister(registration)) {
|
|
|
|
|
serviceRegistry.register(registration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void doUnregister(URL url) {
|
|
|
|
|
final String serviceName = getServiceName(url);
|
|
|
|
|
final Registration registration = createRegistration(serviceName, url);
|
|
|
|
|
this.serviceRegistry.deregister(registration);
|
|
|
|
|
final Registration registration = createRegistration(url);
|
|
|
|
|
if (shouldRegister(registration)) {
|
|
|
|
|
this.serviceRegistry.deregister(registration);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void doSubscribe(URL url, NotifyListener listener) {
|
|
|
|
|
List<String> serviceNames = getServiceNames(url, listener);
|
|
|
|
|
doSubscribe(url, listener, serviceNames);
|
|
|
|
|
this.registeredServicesLookupScheduler.scheduleAtFixedRate(new Runnable() {
|
|
|
|
|
this.servicesLookupScheduler.scheduleAtFixedRate(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
doSubscribe(url, listener, serviceNames);
|
|
|
|
|
}
|
|
|
|
|
}, REGISTERED_SERVICES_LOOKUP_INTERVAL, REGISTERED_SERVICES_LOOKUP_INTERVAL, TimeUnit.SECONDS);
|
|
|
|
|
}, registeredServicesLookupInterval, registeredServicesLookupInterval, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -234,136 +152,34 @@ public class SpringCloudRegistry extends FailbackRegistry {
|
|
|
|
|
if (isAdminProtocol(url)) {
|
|
|
|
|
shutdownServiceNamesLookup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (registeredServicesLookupScheduler != null) {
|
|
|
|
|
// registeredServicesLookupScheduler.shutdown();
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean isAvailable() {
|
|
|
|
|
return false;
|
|
|
|
|
return !discoveryClient.getServices().isEmpty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void shutdownServiceNamesLookup() {
|
|
|
|
|
if (allServicesLookupScheduler != null) {
|
|
|
|
|
allServicesLookupScheduler.shutdown();
|
|
|
|
|
if (servicesLookupScheduler != null) {
|
|
|
|
|
servicesLookupScheduler.shutdown();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Registration createRegistration(String serviceName, URL url) {
|
|
|
|
|
return registrationFactory.create(serviceName, url, applicationContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getServiceName(URL url) {
|
|
|
|
|
String category = url.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
|
|
|
|
|
return getServiceName(url, category);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static String getServiceName(URL url, String category) {
|
|
|
|
|
StringBuilder serviceNameBuilder = new StringBuilder(category);
|
|
|
|
|
appendIfPresent(serviceNameBuilder, url, Constants.INTERFACE_KEY);
|
|
|
|
|
appendIfPresent(serviceNameBuilder, url, Constants.VERSION_KEY);
|
|
|
|
|
appendIfPresent(serviceNameBuilder, url, Constants.GROUP_KEY);
|
|
|
|
|
return serviceNameBuilder.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void appendIfPresent(StringBuilder target, URL url, String parameterName) {
|
|
|
|
|
String parameterValue = url.getParameter(parameterName);
|
|
|
|
|
appendIfPresent(target, parameterValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void appendIfPresent(StringBuilder target, String parameterValue) {
|
|
|
|
|
if (StringUtils.hasText(parameterValue)) {
|
|
|
|
|
target.append(SERVICE_NAME_SEPARATOR).append(parameterValue);
|
|
|
|
|
}
|
|
|
|
|
private Registration createRegistration(URL url) {
|
|
|
|
|
return registrationFactory.create(url, applicationContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void filterServiceNames(List<String> serviceNames, URL url) {
|
|
|
|
|
|
|
|
|
|
final String[] categories = getCategories(url);
|
|
|
|
|
|
|
|
|
|
final String targetServiceInterface = url.getServiceInterface();
|
|
|
|
|
|
|
|
|
|
final String targetVersion = url.getParameter(Constants.VERSION_KEY);
|
|
|
|
|
|
|
|
|
|
final String targetGroup = url.getParameter(Constants.GROUP_KEY);
|
|
|
|
|
|
|
|
|
|
private void filterServiceNames(List<String> serviceNames) {
|
|
|
|
|
filter(serviceNames, new Filter<String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean accept(String serviceName) {
|
|
|
|
|
// split service name to segments
|
|
|
|
|
// (required) segments[0] = category
|
|
|
|
|
// (required) segments[1] = serviceInterface
|
|
|
|
|
// (required) segments[2] = version
|
|
|
|
|
// (optional) segments[3] = group
|
|
|
|
|
String[] segments = getServiceSegments(serviceName);
|
|
|
|
|
int length = segments.length;
|
|
|
|
|
if (length < SERVICE_GROUP_INDEX) { // must present 4 segments or more
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String category = getCategory(segments);
|
|
|
|
|
if (Arrays.binarySearch(categories, category) > -1) { // no match category
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String serviceInterface = getServiceInterface(segments);
|
|
|
|
|
if (!WILDCARD.equals(targetServiceInterface) &&
|
|
|
|
|
!Objects.equals(targetServiceInterface, serviceInterface)) { // no match service interface
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String version = getServiceVersion(segments);
|
|
|
|
|
if (!WILDCARD.equals(targetVersion) &&
|
|
|
|
|
!Objects.equals(targetVersion, version)) { // no match service version
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String group = getServiceGroup(segments);
|
|
|
|
|
if (group != null && !WILDCARD.equals(targetGroup)
|
|
|
|
|
&& !Objects.equals(targetGroup, group)) { // no match service group
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
return dubboRegistryServiceIdHandler.supports(serviceName);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String[] getServiceSegments(String serviceName) {
|
|
|
|
|
return StringUtils.delimitedListToStringArray(serviceName, SERVICE_NAME_SEPARATOR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getCategory(String[] segments) {
|
|
|
|
|
return segments[CATEGORY_INDEX];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getServiceInterface(String[] segments) {
|
|
|
|
|
return segments[SERVICE_INTERFACE_INDEX];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getServiceVersion(String[] segments) {
|
|
|
|
|
return segments[SERVICE_VERSION_INDEX];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getServiceGroup(String[] segments) {
|
|
|
|
|
return segments.length > SERVICE_GROUP_INDEX ? segments[SERVICE_GROUP_INDEX] : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the categories from {@link URL}
|
|
|
|
|
*
|
|
|
|
|
* @param url {@link URL}
|
|
|
|
|
* @return non-null array
|
|
|
|
|
*/
|
|
|
|
|
private String[] getCategories(URL url) {
|
|
|
|
|
return Constants.ANY_VALUE.equals(url.getServiceInterface()) ?
|
|
|
|
|
ALL_SUPPORTED_CATEGORIES : of(Constants.DEFAULT_CATEGORY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<String> getAllServiceNames() {
|
|
|
|
|
return discoveryClient.getServices();
|
|
|
|
|
return new LinkedList<>(discoveryClient.getServices());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -378,7 +194,7 @@ public class SpringCloudRegistry extends FailbackRegistry {
|
|
|
|
|
initAllServicesLookupScheduler(url, listener);
|
|
|
|
|
return getServiceNamesForOps(url);
|
|
|
|
|
} else {
|
|
|
|
|
return doGetServiceNames(url);
|
|
|
|
|
return singletonList(dubboRegistryServiceIdHandler.createServiceId(url));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -388,30 +204,14 @@ public class SpringCloudRegistry extends FailbackRegistry {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initAllServicesLookupScheduler(final URL url, final NotifyListener listener) {
|
|
|
|
|
if (allServicesLookupScheduler == null) {
|
|
|
|
|
allServicesLookupScheduler = newSingleThreadScheduledExecutor(new NamedThreadFactory("dubbo-all-services-lookup-"));
|
|
|
|
|
allServicesLookupScheduler.scheduleAtFixedRate(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
List<String> serviceNames = getAllServiceNames();
|
|
|
|
|
filter(serviceNames, new Filter<String>() {
|
|
|
|
|
@Override
|
|
|
|
|
public boolean accept(String serviceName) {
|
|
|
|
|
boolean accepted = false;
|
|
|
|
|
for (String category : ALL_SUPPORTED_CATEGORIES) {
|
|
|
|
|
String prefix = category + SERVICE_NAME_SEPARATOR;
|
|
|
|
|
if (StringUtils.startsWithIgnoreCase(serviceName, prefix)) {
|
|
|
|
|
accepted = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return accepted;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
doSubscribe(url, listener, serviceNames);
|
|
|
|
|
}
|
|
|
|
|
}, ALL_SERVICES_LOOKUP_INTERVAL, ALL_SERVICES_LOOKUP_INTERVAL, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
servicesLookupScheduler.scheduleAtFixedRate(new Runnable() {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
List<String> serviceNames = getAllServiceNames();
|
|
|
|
|
filterServiceNames(serviceNames);
|
|
|
|
|
doSubscribe(url, listener, serviceNames);
|
|
|
|
|
}
|
|
|
|
|
}, allServicesLookupInterval, allServicesLookupInterval, TimeUnit.SECONDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void doSubscribe(final URL url, final NotifyListener listener, final List<String> serviceNames) {
|
|
|
|
@ -421,16 +221,6 @@ public class SpringCloudRegistry extends FailbackRegistry {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<String> doGetServiceNames(URL url) {
|
|
|
|
|
String[] categories = getCategories(url);
|
|
|
|
|
List<String> serviceNames = new ArrayList<String>(categories.length);
|
|
|
|
|
for (String category : categories) {
|
|
|
|
|
final String serviceName = getServiceName(url, category);
|
|
|
|
|
serviceNames.add(serviceName);
|
|
|
|
|
}
|
|
|
|
|
return serviceNames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Notify the Healthy {@link ServiceInstance service instance} to subscriber.
|
|
|
|
|
*
|
|
|
|
@ -487,7 +277,7 @@ public class SpringCloudRegistry extends FailbackRegistry {
|
|
|
|
|
*/
|
|
|
|
|
private List<String> getServiceNamesForOps(URL url) {
|
|
|
|
|
List<String> serviceNames = getAllServiceNames();
|
|
|
|
|
filterServiceNames(serviceNames, url);
|
|
|
|
|
filterServiceNames(serviceNames);
|
|
|
|
|
return serviceNames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -508,7 +298,7 @@ public class SpringCloudRegistry extends FailbackRegistry {
|
|
|
|
|
/**
|
|
|
|
|
* A filter
|
|
|
|
|
*/
|
|
|
|
|
private interface Filter<T> {
|
|
|
|
|
public interface Filter<T> {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests whether or not the specified data should be accepted.
|
|
|
|
|