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

Format Code using Spring Boot Code Conventions

This commit is contained in:
mercyblitz
2019-08-01 14:08:30 +08:00
parent 12efadd61f
commit 3df938d229
86 changed files with 5646 additions and 5270 deletions

View File

@@ -16,32 +16,6 @@
*/
package com.alibaba.cloud.dubbo.registry;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.RegistryFactory;
import org.apache.dubbo.registry.support.FailbackRegistry;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent;
import com.alibaba.cloud.dubbo.service.DubboMetadataService;
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
import com.alibaba.cloud.dubbo.util.JSONUtils;
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.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 static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.apache.dubbo.common.URLBuilder.from;
@@ -54,281 +28,337 @@ 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;
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 org.apache.dubbo.common.URL;
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.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 com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancesChangedEvent;
import com.alibaba.cloud.dubbo.service.DubboMetadataService;
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
import com.alibaba.cloud.dubbo.util.JSONUtils;
/**
* Abstract Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration abstraction, whose protocol is "spring-cloud"
* Abstract Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration
* abstraction, whose protocol is "spring-cloud"
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
/**
* The parameter name of {@link #servicesLookupInterval}
*/
public static final String SERVICES_LOOKUP_INTERVAL_PARAM_NAME = "dubbo.services.lookup.interval";
/**
* The parameter name of {@link #servicesLookupInterval}
*/
public static final String SERVICES_LOOKUP_INTERVAL_PARAM_NAME = "dubbo.services.lookup.interval";
protected static final String DUBBO_METADATA_SERVICE_CLASS_NAME = DubboMetadataService.class.getName();
/**
* Caches the IDs of {@link ApplicationListener}
*/
private static final Set<String> registerListeners = new HashSet<>();
protected static final String DUBBO_METADATA_SERVICE_CLASS_NAME = DubboMetadataService.class
.getName();
/**
* Caches the IDs of {@link ApplicationListener}
*/
private static final Set<String> registerListeners = new HashSet<>();
protected final Logger logger = LoggerFactory.getLogger(getClass());
protected final Logger logger = LoggerFactory.getLogger(getClass());
/**
* The interval in second of lookup service names(only for Dubbo-OPS)
*/
private final long servicesLookupInterval;
/**
* The interval in second of lookup service names(only for Dubbo-OPS)
*/
private final long servicesLookupInterval;
private final DiscoveryClient discoveryClient;
private final DiscoveryClient discoveryClient;
private final DubboServiceMetadataRepository repository;
private final DubboServiceMetadataRepository repository;
private final DubboMetadataServiceProxy dubboMetadataConfigServiceProxy;
private final DubboMetadataServiceProxy dubboMetadataConfigServiceProxy;
private final JSONUtils jsonUtils;
private final JSONUtils jsonUtils;
private final ConfigurableApplicationContext applicationContext;
private final ConfigurableApplicationContext applicationContext;
public AbstractSpringCloudRegistry(URL url,
DiscoveryClient discoveryClient,
DubboServiceMetadataRepository dubboServiceMetadataRepository,
DubboMetadataServiceProxy dubboMetadataConfigServiceProxy,
JSONUtils jsonUtils,
ConfigurableApplicationContext applicationContext) {
super(url);
this.servicesLookupInterval = url.getParameter(SERVICES_LOOKUP_INTERVAL_PARAM_NAME, 60L);
this.discoveryClient = discoveryClient;
this.repository = dubboServiceMetadataRepository;
this.dubboMetadataConfigServiceProxy = dubboMetadataConfigServiceProxy;
this.jsonUtils = jsonUtils;
this.applicationContext = applicationContext;
}
public AbstractSpringCloudRegistry(URL url, DiscoveryClient discoveryClient,
DubboServiceMetadataRepository dubboServiceMetadataRepository,
DubboMetadataServiceProxy dubboMetadataConfigServiceProxy,
JSONUtils jsonUtils, ConfigurableApplicationContext applicationContext) {
super(url);
this.servicesLookupInterval = url
.getParameter(SERVICES_LOOKUP_INTERVAL_PARAM_NAME, 60L);
this.discoveryClient = discoveryClient;
this.repository = dubboServiceMetadataRepository;
this.dubboMetadataConfigServiceProxy = dubboMetadataConfigServiceProxy;
this.jsonUtils = jsonUtils;
this.applicationContext = applicationContext;
}
protected boolean shouldRegister(URL url) {
String side = url.getParameter(SIDE_KEY);
protected boolean shouldRegister(URL url) {
String side = url.getParameter(SIDE_KEY);
boolean should = PROVIDER_SIDE.equals(side); // Only register the Provider.
boolean should = PROVIDER_SIDE.equals(side); // Only register the Provider.
if (!should) {
if (logger.isDebugEnabled()) {
logger.debug("The URL[{}] should not be registered.", url.toString());
}
}
if (!should) {
if (logger.isDebugEnabled()) {
logger.debug("The URL[{}] should not be registered.", url.toString());
}
}
return should;
}
return should;
}
@Override
public final void doRegister(URL url) {
if (!shouldRegister(url)) {
return;
}
doRegister0(url);
}
@Override
public final void doRegister(URL url) {
if (!shouldRegister(url)) {
return;
}
doRegister0(url);
}
/**
* The sub-type should implement to register
*
* @param url {@link URL}
*/
protected abstract void doRegister0(URL url);
/**
* The sub-type should implement to register
*
* @param url {@link URL}
*/
protected abstract void doRegister0(URL url);
@Override
public final void doUnregister(URL url) {
if (!shouldRegister(url)) {
return;
}
doUnregister0(url);
}
@Override
public final void doUnregister(URL url) {
if (!shouldRegister(url)) {
return;
}
doUnregister0(url);
}
/**
* The sub-type should implement to unregister
*
* @param url {@link URL}
*/
protected abstract void doUnregister0(URL url);
/**
* The sub-type should implement to unregister
*
* @param url {@link URL}
*/
protected abstract void doUnregister0(URL url);
@Override
public final void doSubscribe(URL url, NotifyListener listener) {
@Override
public final void doSubscribe(URL url, NotifyListener listener) {
if (isAdminURL(url)) {
// TODO in future
} else if (isDubboMetadataServiceURL(url)) { // for DubboMetadataService
subscribeDubboMetadataServiceURLs(url, listener);
} else { // for general Dubbo Services
subscribeDubboServiceURLs(url, listener);
}
}
if (isAdminURL(url)) {
// TODO in future
}
else if (isDubboMetadataServiceURL(url)) { // for DubboMetadataService
subscribeDubboMetadataServiceURLs(url, listener);
}
else { // for general Dubbo Services
subscribeDubboServiceURLs(url, listener);
}
}
protected void subscribeDubboServiceURLs(URL url, NotifyListener listener) {
protected void subscribeDubboServiceURLs(URL url, NotifyListener listener) {
doSubscribeDubboServiceURLs(url, listener);
doSubscribeDubboServiceURLs(url, listener);
registerServiceInstancesChangedEventListener(url, listener);
}
registerServiceInstancesChangedEventListener(url, listener);
}
/**
* Register a {@link ApplicationListener listener} for {@link ServiceInstancesChangedEvent}
*
* @param url {@link URL}
* @param listener {@link NotifyListener}
*/
private void registerServiceInstancesChangedEventListener(URL url, NotifyListener listener) {
String listenerId = generateId(url);
if (registerListeners.add(listenerId)) {
applicationContext.addApplicationListener(new ApplicationListener<ServiceInstancesChangedEvent>() {
@Override
public void onApplicationEvent(ServiceInstancesChangedEvent event) {
String serviceName = event.getServiceName();
Collection<ServiceInstance> serviceInstances = event.getServiceInstances();
subscribeDubboServiceURL(url, listener, serviceName, s -> serviceInstances);
}
});
}
}
/**
* Register a {@link ApplicationListener listener} for
* {@link ServiceInstancesChangedEvent}
*
* @param url {@link URL}
* @param listener {@link NotifyListener}
*/
private void registerServiceInstancesChangedEventListener(URL url,
NotifyListener listener) {
String listenerId = generateId(url);
if (registerListeners.add(listenerId)) {
applicationContext.addApplicationListener(
new ApplicationListener<ServiceInstancesChangedEvent>() {
@Override
public void onApplicationEvent(
ServiceInstancesChangedEvent event) {
String serviceName = event.getServiceName();
Collection<ServiceInstance> serviceInstances = event
.getServiceInstances();
subscribeDubboServiceURL(url, listener, serviceName,
s -> serviceInstances);
}
});
}
}
private void doSubscribeDubboServiceURLs(URL url, NotifyListener listener) {
private void doSubscribeDubboServiceURLs(URL url, NotifyListener listener) {
Set<String> subscribedServices = repository.getSubscribedServices();
// Sync
subscribedServices.forEach(service -> subscribeDubboServiceURL(url, listener, service, this::getServiceInstances));
}
Set<String> subscribedServices = repository.getSubscribedServices();
// Sync
subscribedServices.forEach(service -> subscribeDubboServiceURL(url, listener,
service, this::getServiceInstances));
}
protected void subscribeDubboServiceURL(URL url, NotifyListener listener, String serviceName,
Function<String, Collection<ServiceInstance>> serviceInstancesFunction) {
protected void subscribeDubboServiceURL(URL url, NotifyListener listener,
String serviceName,
Function<String, Collection<ServiceInstance>> serviceInstancesFunction) {
if (logger.isInfoEnabled()) {
logger.info("The Dubbo Service URL[ID : {}] is being subscribed for service[name : {}]",
generateId(url), serviceName);
}
if (logger.isInfoEnabled()) {
logger.info(
"The Dubbo Service URL[ID : {}] is being subscribed for service[name : {}]",
generateId(url), serviceName);
}
DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy.getProxy(serviceName);
DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy
.getProxy(serviceName);
if (dubboMetadataService == null) { // If not found, try to initialize
if (logger.isInfoEnabled()) {
logger.info("The metadata of Dubbo service[key : {}] can't be found when the subscribed service[name : {}], " +
"and then try to initialize it", url.getServiceKey(), serviceName);
}
repository.initializeMetadata(serviceName);
dubboMetadataService = dubboMetadataConfigServiceProxy.getProxy(serviceName);
}
if (dubboMetadataService == null) { // If not found, try to initialize
if (logger.isInfoEnabled()) {
logger.info(
"The metadata of Dubbo service[key : {}] can't be found when the subscribed service[name : {}], "
+ "and then try to initialize it",
url.getServiceKey(), serviceName);
}
repository.initializeMetadata(serviceName);
dubboMetadataService = dubboMetadataConfigServiceProxy.getProxy(serviceName);
}
if (dubboMetadataService == null) { // It makes sure not-found, return immediately
if (logger.isWarnEnabled()) {
logger.warn("The metadata of Dubbo service[key : {}] still can't be found, it could effect the further " +
"Dubbo service invocation", url.getServiceKey());
}
return;
}
if (dubboMetadataService == null) { // It makes sure not-found, return immediately
if (logger.isWarnEnabled()) {
logger.warn(
"The metadata of Dubbo service[key : {}] still can't be found, it could effect the further "
+ "Dubbo service invocation",
url.getServiceKey());
}
return;
}
Collection<ServiceInstance> serviceInstances = serviceInstancesFunction.apply(serviceName);
Collection<ServiceInstance> serviceInstances = serviceInstancesFunction
.apply(serviceName);
List<URL> allSubscribedURLs = new LinkedList<>();
List<URL> allSubscribedURLs = new LinkedList<>();
if (CollectionUtils.isEmpty(serviceInstances)) {
if (logger.isWarnEnabled()) {
logger.warn("There is no instance from service[name : {}], and then Dubbo Service[key : {}] will not be " +
"available , please make sure the further impact", serviceName, url.getServiceKey());
}
/**
* URLs with {@link RegistryConstants#EMPTY_PROTOCOL}
*/
allSubscribedURLs.addAll(emptyURLs(url));
} else {
List<URL> exportedURLs = getExportedURLs(dubboMetadataService, url);
if (CollectionUtils.isEmpty(serviceInstances)) {
if (logger.isWarnEnabled()) {
logger.warn(
"There is no instance from service[name : {}], and then Dubbo Service[key : {}] will not be "
+ "available , please make sure the further impact",
serviceName, url.getServiceKey());
}
/**
* URLs with {@link RegistryConstants#EMPTY_PROTOCOL}
*/
allSubscribedURLs.addAll(emptyURLs(url));
}
else {
List<URL> exportedURLs = getExportedURLs(dubboMetadataService, url);
for (URL exportedURL : exportedURLs) {
String protocol = exportedURL.getProtocol();
List<URL> subscribedURLs = new LinkedList<>();
serviceInstances.forEach(serviceInstance -> {
Integer port = repository.getDubboProtocolPort(serviceInstance, protocol);
String host = serviceInstance.getHost();
if (port == null) {
if (logger.isWarnEnabled()) {
logger.warn("The protocol[{}] port of Dubbo service instance[host : {}] " +
"can't be resolved", protocol, host);
}
} else {
URL subscribedURL = new URL(protocol, host, port, exportedURL.getParameters());
subscribedURLs.add(subscribedURL);
}
});
for (URL exportedURL : exportedURLs) {
String protocol = exportedURL.getProtocol();
List<URL> subscribedURLs = new LinkedList<>();
serviceInstances.forEach(serviceInstance -> {
Integer port = repository.getDubboProtocolPort(serviceInstance,
protocol);
String host = serviceInstance.getHost();
if (port == null) {
if (logger.isWarnEnabled()) {
logger.warn(
"The protocol[{}] port of Dubbo service instance[host : {}] "
+ "can't be resolved",
protocol, host);
}
}
else {
URL subscribedURL = new URL(protocol, host, port,
exportedURL.getParameters());
subscribedURLs.add(subscribedURL);
}
});
allSubscribedURLs.addAll(subscribedURLs);
}
}
allSubscribedURLs.addAll(subscribedURLs);
}
}
if (logger.isDebugEnabled()) {
logger.debug("The subscribed URL[{}] will notify all URLs : {}", url, allSubscribedURLs);
}
if (logger.isDebugEnabled()) {
logger.debug("The subscribed URL[{}] will notify all URLs : {}", url,
allSubscribedURLs);
}
listener.notify(allSubscribedURLs);
}
listener.notify(allSubscribedURLs);
}
private String generateId(URL url) {
return url.toString(VERSION_KEY, GROUP_KEY, PROTOCOL_KEY);
}
private String generateId(URL url) {
return url.toString(VERSION_KEY, GROUP_KEY, PROTOCOL_KEY);
}
private List<URL> emptyURLs(URL url) {
return asList(from(url).setProtocol(EMPTY_PROTOCOL).build());
}
private List<URL> emptyURLs(URL url) {
return asList(from(url).setProtocol(EMPTY_PROTOCOL).build());
}
private List<ServiceInstance> getServiceInstances(String serviceName) {
return hasText(serviceName) ? doGetServiceInstances(serviceName) : emptyList();
}
private List<ServiceInstance> getServiceInstances(String serviceName) {
return hasText(serviceName) ? doGetServiceInstances(serviceName) : emptyList();
}
private List<ServiceInstance> doGetServiceInstances(String serviceName) {
List<ServiceInstance> serviceInstances = emptyList();
try {
serviceInstances = discoveryClient.getInstances(serviceName);
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
}
return serviceInstances;
}
private List<ServiceInstance> doGetServiceInstances(String serviceName) {
List<ServiceInstance> serviceInstances = emptyList();
try {
serviceInstances = discoveryClient.getInstances(serviceName);
}
catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error(e.getMessage(), e);
}
}
return serviceInstances;
}
private List<URL> getExportedURLs(DubboMetadataService dubboMetadataService, URL url) {
String serviceInterface = url.getServiceInterface();
String group = url.getParameter(GROUP_KEY);
String version = url.getParameter(VERSION_KEY);
// The subscribed protocol may be null
String subscribedProtocol = url.getParameter(PROTOCOL_KEY);
String exportedURLsJSON = dubboMetadataService.getExportedURLs(serviceInterface, group, version);
return jsonUtils
.toURLs(exportedURLsJSON)
.stream()
.filter(exportedURL ->
subscribedProtocol == null || subscribedProtocol.equalsIgnoreCase(exportedURL.getProtocol())
).collect(Collectors.toList());
}
private List<URL> getExportedURLs(DubboMetadataService dubboMetadataService,
URL url) {
String serviceInterface = url.getServiceInterface();
String group = url.getParameter(GROUP_KEY);
String version = url.getParameter(VERSION_KEY);
// The subscribed protocol may be null
String subscribedProtocol = url.getParameter(PROTOCOL_KEY);
String exportedURLsJSON = dubboMetadataService.getExportedURLs(serviceInterface,
group, version);
return jsonUtils.toURLs(exportedURLsJSON).stream()
.filter(exportedURL -> subscribedProtocol == null
|| subscribedProtocol.equalsIgnoreCase(exportedURL.getProtocol()))
.collect(Collectors.toList());
}
private void subscribeDubboMetadataServiceURLs(URL url, NotifyListener listener) {
String serviceInterface = url.getServiceInterface();
String group = url.getParameter(GROUP_KEY);
String version = url.getParameter(VERSION_KEY);
String protocol = url.getParameter(PROTOCOL_KEY);
List<URL> urls = repository.findSubscribedDubboMetadataServiceURLs(serviceInterface, group, version, protocol);
listener.notify(urls);
}
private void subscribeDubboMetadataServiceURLs(URL url, NotifyListener listener) {
String serviceInterface = url.getServiceInterface();
String group = url.getParameter(GROUP_KEY);
String version = url.getParameter(VERSION_KEY);
String protocol = url.getParameter(PROTOCOL_KEY);
List<URL> urls = repository.findSubscribedDubboMetadataServiceURLs(
serviceInterface, group, version, protocol);
listener.notify(urls);
}
@Override
public final void doUnsubscribe(URL url, NotifyListener listener) {
if (isAdminURL(url)) {
}
}
@Override
public final void doUnsubscribe(URL url, NotifyListener listener) {
if (isAdminURL(url)) {
}
}
@Override
public boolean isAvailable() {
return !discoveryClient.getServices().isEmpty();
}
@Override
public boolean isAvailable() {
return !discoveryClient.getServices().isEmpty();
}
protected boolean isAdminURL(URL url) {
return ADMIN_PROTOCOL.equals(url.getProtocol());
}
protected boolean isAdminURL(URL url) {
return ADMIN_PROTOCOL.equals(url.getProtocol());
}
protected boolean isDubboMetadataServiceURL(URL url) {
return DUBBO_METADATA_SERVICE_CLASS_NAME.equals(url.getServiceInterface());
}
protected boolean isDubboMetadataServiceURL(URL url) {
return DUBBO_METADATA_SERVICE_CLASS_NAME.equals(url.getServiceInterface());
}
}

View File

@@ -16,57 +16,58 @@
*/
package com.alibaba.cloud.dubbo.registry;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;
import java.net.URI;
import java.util.Map;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;
/**
* The {@link Registration} of Dubbo uses an external of {@link ServiceInstance} instance as the delegate.
* The {@link Registration} of Dubbo uses an external of {@link ServiceInstance} instance
* as the delegate.
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
class DelegatingRegistration implements Registration {
private final ServiceInstance delegate;
private final ServiceInstance delegate;
public DelegatingRegistration(ServiceInstance delegate) {
this.delegate = delegate;
}
public DelegatingRegistration(ServiceInstance delegate) {
this.delegate = delegate;
}
@Override
public String getServiceId() {
return delegate.getServiceId();
}
@Override
public String getServiceId() {
return delegate.getServiceId();
}
@Override
public String getHost() {
return delegate.getHost();
}
@Override
public String getHost() {
return delegate.getHost();
}
@Override
public int getPort() {
return delegate.getPort();
}
@Override
public int getPort() {
return delegate.getPort();
}
@Override
public boolean isSecure() {
return delegate.isSecure();
}
@Override
public boolean isSecure() {
return delegate.isSecure();
}
@Override
public URI getUri() {
return delegate.getUri();
}
@Override
public URI getUri() {
return delegate.getUri();
}
@Override
public Map<String, String> getMetadata() {
return delegate.getMetadata();
}
@Override
public Map<String, String> getMetadata() {
return delegate.getMetadata();
}
@Override
public String getScheme() {
return delegate.getScheme();
}
@Override
public String getScheme() {
return delegate.getScheme();
}
}

View File

@@ -16,8 +16,6 @@
*/
package com.alibaba.cloud.dubbo.registry;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstanceRegisteredEvent;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@@ -26,6 +24,9 @@ import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstancePreRegisteredEvent;
import com.alibaba.cloud.dubbo.registry.event.ServiceInstanceRegisteredEvent;
/**
* Dubbo Service Registration Event-Publishing Aspect
*
@@ -34,28 +35,31 @@ import org.springframework.context.ApplicationEventPublisherAware;
* @see ServiceInstanceRegisteredEvent
*/
@Aspect
public class DubboServiceRegistrationEventPublishingAspect implements ApplicationEventPublisherAware {
public class DubboServiceRegistrationEventPublishingAspect
implements ApplicationEventPublisherAware {
/**
* The pointcut expression for {@link ServiceRegistry#register(Registration)}
*/
public static final String REGISTER_POINTCUT_EXPRESSION =
"execution(* org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(*)) && args(registration)";
/**
* The pointcut expression for {@link ServiceRegistry#register(Registration)}
*/
public static final String REGISTER_POINTCUT_EXPRESSION = "execution(* org.springframework.cloud.client.serviceregistry.ServiceRegistry.register(*)) && args(registration)";
private ApplicationEventPublisher applicationEventPublisher;
private ApplicationEventPublisher applicationEventPublisher;
@Before(REGISTER_POINTCUT_EXPRESSION)
public void beforeRegister(Registration registration) {
applicationEventPublisher.publishEvent(new ServiceInstancePreRegisteredEvent(registration));
}
@Before(REGISTER_POINTCUT_EXPRESSION)
public void beforeRegister(Registration registration) {
applicationEventPublisher
.publishEvent(new ServiceInstancePreRegisteredEvent(registration));
}
@After(REGISTER_POINTCUT_EXPRESSION)
public void afterRegister(Registration registration) {
applicationEventPublisher.publishEvent(new ServiceInstanceRegisteredEvent(registration));
}
@After(REGISTER_POINTCUT_EXPRESSION)
public void afterRegister(Registration registration) {
applicationEventPublisher
.publishEvent(new ServiceInstanceRegisteredEvent(registration));
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
@Override
public void setApplicationEventPublisher(
ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}

View File

@@ -19,37 +19,39 @@ package com.alibaba.cloud.dubbo.registry;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.registry.RegistryFactory;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
import com.alibaba.cloud.dubbo.util.JSONUtils;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
import com.alibaba.cloud.dubbo.util.JSONUtils;
/**
* Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration abstraction, whose protocol is "spring-cloud"
* Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration abstraction, whose
* protocol is "spring-cloud"
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class SpringCloudRegistry extends AbstractSpringCloudRegistry {
private final DubboServiceMetadataRepository dubboServiceMetadataRepository;
private final DubboServiceMetadataRepository dubboServiceMetadataRepository;
public SpringCloudRegistry(URL url, DiscoveryClient discoveryClient,
DubboServiceMetadataRepository dubboServiceMetadataRepository,
DubboMetadataServiceProxy dubboMetadataConfigServiceProxy,
JSONUtils jsonUtils,
ConfigurableApplicationContext applicationContext) {
super(url, discoveryClient, dubboServiceMetadataRepository, dubboMetadataConfigServiceProxy, jsonUtils, applicationContext);
this.dubboServiceMetadataRepository = dubboServiceMetadataRepository;
}
public SpringCloudRegistry(URL url, DiscoveryClient discoveryClient,
DubboServiceMetadataRepository dubboServiceMetadataRepository,
DubboMetadataServiceProxy dubboMetadataConfigServiceProxy,
JSONUtils jsonUtils, ConfigurableApplicationContext applicationContext) {
super(url, discoveryClient, dubboServiceMetadataRepository,
dubboMetadataConfigServiceProxy, jsonUtils, applicationContext);
this.dubboServiceMetadataRepository = dubboServiceMetadataRepository;
}
@Override
protected void doRegister0(URL url) {
dubboServiceMetadataRepository.exportURL(url);
}
@Override
protected void doRegister0(URL url) {
dubboServiceMetadataRepository.exportURL(url);
}
@Override
protected void doUnregister0(URL url) {
dubboServiceMetadataRepository.unexportURL(url);
}
@Override
protected void doUnregister0(URL url) {
dubboServiceMetadataRepository.unexportURL(url);
}
}

View File

@@ -16,20 +16,22 @@
*/
package com.alibaba.cloud.dubbo.registry;
import static java.lang.System.getProperty;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.RegistryFactory;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
import com.alibaba.cloud.dubbo.util.JSONUtils;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.ConfigurableApplicationContext;
import static java.lang.System.getProperty;
import com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository;
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
import com.alibaba.cloud.dubbo.util.JSONUtils;
/**
* Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration abstraction, whose protocol is "spring-cloud"
* Dubbo {@link RegistryFactory} uses Spring Cloud Service Registration abstraction, whose
* protocol is "spring-cloud"
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
* @see RegistryFactory
@@ -37,46 +39,51 @@ import static java.lang.System.getProperty;
*/
public class SpringCloudRegistryFactory implements RegistryFactory {
public static String PROTOCOL = "spring-cloud";
public static String PROTOCOL = "spring-cloud";
public static String ADDRESS = "localhost";
public static String ADDRESS = "localhost";
private static String SERVICES_LOOKUP_SCHEDULER_THREAD_NAME_PREFIX =
getProperty("dubbo.services.lookup.scheduler.thread.name.prefix ", "dubbo-services-lookup-");
private static String SERVICES_LOOKUP_SCHEDULER_THREAD_NAME_PREFIX = getProperty(
"dubbo.services.lookup.scheduler.thread.name.prefix ",
"dubbo-services-lookup-");
private static ConfigurableApplicationContext applicationContext;
private static ConfigurableApplicationContext applicationContext;
private DiscoveryClient discoveryClient;
private DiscoveryClient discoveryClient;
private DubboServiceMetadataRepository dubboServiceMetadataRepository;
private DubboServiceMetadataRepository dubboServiceMetadataRepository;
private DubboMetadataServiceProxy dubboMetadataConfigServiceProxy;
private DubboMetadataServiceProxy dubboMetadataConfigServiceProxy;
private JSONUtils jsonUtils;
private JSONUtils jsonUtils;
private volatile boolean initialized = false;
private volatile boolean initialized = false;
public SpringCloudRegistryFactory() {
}
public SpringCloudRegistryFactory() {
}
public static void setApplicationContext(ConfigurableApplicationContext applicationContext) {
SpringCloudRegistryFactory.applicationContext = applicationContext;
}
public static void setApplicationContext(
ConfigurableApplicationContext applicationContext) {
SpringCloudRegistryFactory.applicationContext = applicationContext;
}
protected void init() {
if (initialized || applicationContext == null) {
return;
}
this.discoveryClient = applicationContext.getBean(DiscoveryClient.class);
this.dubboServiceMetadataRepository = applicationContext.getBean(DubboServiceMetadataRepository.class);
this.dubboMetadataConfigServiceProxy = applicationContext.getBean(DubboMetadataServiceProxy.class);
this.jsonUtils = applicationContext.getBean(JSONUtils.class);
}
protected void init() {
if (initialized || applicationContext == null) {
return;
}
this.discoveryClient = applicationContext.getBean(DiscoveryClient.class);
this.dubboServiceMetadataRepository = applicationContext
.getBean(DubboServiceMetadataRepository.class);
this.dubboMetadataConfigServiceProxy = applicationContext
.getBean(DubboMetadataServiceProxy.class);
this.jsonUtils = applicationContext.getBean(JSONUtils.class);
}
@Override
public Registry getRegistry(URL url) {
init();
return new SpringCloudRegistry(url, discoveryClient, dubboServiceMetadataRepository,
dubboMetadataConfigServiceProxy, jsonUtils, applicationContext);
}
@Override
public Registry getRegistry(URL url) {
init();
return new SpringCloudRegistry(url, discoveryClient,
dubboServiceMetadataRepository, dubboMetadataConfigServiceProxy,
jsonUtils, applicationContext);
}
}

View File

@@ -22,18 +22,19 @@ import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationEvent;
/**
* The before-{@link ServiceRegistry#register(Registration) register} event for {@link ServiceInstance}
* The before-{@link ServiceRegistry#register(Registration) register} event for
* {@link ServiceInstance}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class ServiceInstancePreRegisteredEvent extends ApplicationEvent {
public ServiceInstancePreRegisteredEvent(Registration source) {
super(source);
}
public ServiceInstancePreRegisteredEvent(Registration source) {
super(source);
}
@Override
public Registration getSource() {
return (Registration) super.getSource();
}
@Override
public Registration getSource() {
return (Registration) super.getSource();
}
}

View File

@@ -21,18 +21,19 @@ import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
import org.springframework.context.ApplicationEvent;
/**
* The after-{@link ServiceRegistry#register(Registration) register} event for {@link Registration}
* The after-{@link ServiceRegistry#register(Registration) register} event for
* {@link Registration}
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class ServiceInstanceRegisteredEvent extends ApplicationEvent {
public ServiceInstanceRegisteredEvent(Registration source) {
super(source);
}
public ServiceInstanceRegisteredEvent(Registration source) {
super(source);
}
@Override
public Registration getSource() {
return (Registration) super.getSource();
}
@Override
public Registration getSource() {
return (Registration) super.getSource();
}
}

View File

@@ -16,72 +16,74 @@
*/
package com.alibaba.cloud.dubbo.registry.event;
import static java.util.Collections.unmodifiableCollection;
import java.util.Collection;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import java.util.Collection;
import static java.util.Collections.unmodifiableCollection;
/**
* An event raised after the {@link ServiceInstance instances} of one service has been changed.
* An event raised after the {@link ServiceInstance instances} of one service has been
* changed.
*
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
*/
public class ServiceInstancesChangedEvent extends ApplicationEvent {
private final String serviceName;
private final String serviceName;
private final Collection<ServiceInstance> serviceInstances;
private final Collection<ServiceInstance> serviceInstances;
/**
* Current event has been processed or not.
* Typically, Spring Event was based on sync {@link ApplicationEventMulticaster}
*
* @see SimpleApplicationEventMulticaster
*/
private boolean processed = false;
/**
* Current event has been processed or not. Typically, Spring Event was based on sync
* {@link ApplicationEventMulticaster}
*
* @see SimpleApplicationEventMulticaster
*/
private boolean processed = false;
/**
* @param serviceName The name of service that was changed
* @param serviceInstances all {@link ServiceInstance service instances}
* @throws IllegalArgumentException if source is null.
*/
public ServiceInstancesChangedEvent(String serviceName, Collection<ServiceInstance> serviceInstances) {
super(serviceName);
this.serviceName = serviceName;
this.serviceInstances = unmodifiableCollection(serviceInstances);
}
/**
* @param serviceName The name of service that was changed
* @param serviceInstances all {@link ServiceInstance service instances}
* @throws IllegalArgumentException if source is null.
*/
public ServiceInstancesChangedEvent(String serviceName,
Collection<ServiceInstance> serviceInstances) {
super(serviceName);
this.serviceName = serviceName;
this.serviceInstances = unmodifiableCollection(serviceInstances);
}
/**
* @return The name of service that was changed
*/
public String getServiceName() {
return serviceName;
}
/**
* @return The name of service that was changed
*/
public String getServiceName() {
return serviceName;
}
/**
* @return all {@link ServiceInstance service instances}
*/
public Collection<ServiceInstance> getServiceInstances() {
return serviceInstances;
}
/**
* @return all {@link ServiceInstance service instances}
*/
public Collection<ServiceInstance> getServiceInstances() {
return serviceInstances;
}
/**
* Mark current event being processed
*/
public void processed() {
processed = true;
}
/**
* Mark current event being processed
*/
public void processed() {
processed = true;
}
/**
* Current event has been processed or not
*
* @return if processed, return <code>true</code>, or <code>false</code>
*/
public boolean isProcessed() {
return processed;
}
/**
* Current event has been processed or not
*
* @return if processed, return <code>true</code>, or <code>false</code>
*/
public boolean isProcessed() {
return processed;
}
}

View File

@@ -16,12 +16,12 @@
*/
package com.alibaba.cloud.dubbo.registry.event;
import org.springframework.context.ApplicationEvent;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import org.springframework.context.ApplicationEvent;
/**
* {@link ApplicationEvent Event} raised when the subscribed services are changed
* <p>
@@ -31,35 +31,36 @@ import java.util.Set;
*/
public class SubscribedServicesChangedEvent extends ApplicationEvent {
private final Set<String> oldSubscribedServices;
private final Set<String> oldSubscribedServices;
private final Set<String> newSubscribedServices;
private final Set<String> newSubscribedServices;
private final boolean changed;
private final boolean changed;
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
* @param oldSubscribedServices the subscribed services before changed
* @param newSubscribedServices the subscribed services after changed
*/
public SubscribedServicesChangedEvent(Object source, Set<String> oldSubscribedServices, Set<String> newSubscribedServices) {
super(source);
this.oldSubscribedServices = new LinkedHashSet<>(oldSubscribedServices);
this.newSubscribedServices = new LinkedHashSet<>(newSubscribedServices);
this.changed = !Objects.equals(oldSubscribedServices, newSubscribedServices);
}
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
* @param oldSubscribedServices the subscribed services before changed
* @param newSubscribedServices the subscribed services after changed
*/
public SubscribedServicesChangedEvent(Object source,
Set<String> oldSubscribedServices, Set<String> newSubscribedServices) {
super(source);
this.oldSubscribedServices = new LinkedHashSet<>(oldSubscribedServices);
this.newSubscribedServices = new LinkedHashSet<>(newSubscribedServices);
this.changed = !Objects.equals(oldSubscribedServices, newSubscribedServices);
}
public Set<String> getOldSubscribedServices() {
return oldSubscribedServices;
}
public Set<String> getOldSubscribedServices() {
return oldSubscribedServices;
}
public Set<String> getNewSubscribedServices() {
return newSubscribedServices;
}
public Set<String> getNewSubscribedServices() {
return newSubscribedServices;
}
public boolean isChanged() {
return changed;
}
public boolean isChanged() {
return changed;
}
}