mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #813 from fangjian0423/master
Dubbo spring cloud dynamic service discovery
This commit is contained in:
commit
8832644850
@ -43,7 +43,6 @@ import java.util.stream.Stream;
|
|||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
import org.apache.dubbo.common.URL;
|
import org.apache.dubbo.common.URL;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.SmartInitializingSingleton;
|
import org.springframework.beans.factory.SmartInitializingSingleton;
|
||||||
@ -70,6 +69,7 @@ import com.alibaba.cloud.dubbo.service.DubboMetadataService;
|
|||||||
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceExporter;
|
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceExporter;
|
||||||
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
|
import com.alibaba.cloud.dubbo.service.DubboMetadataServiceProxy;
|
||||||
import com.alibaba.cloud.dubbo.util.JSONUtils;
|
import com.alibaba.cloud.dubbo.util.JSONUtils;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||||
|
|
||||||
@ -291,6 +291,16 @@ public class DubboServiceMetadataRepository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the metadata of Dubbo Services if no there is no service instance
|
||||||
|
* @param serviceName the service name
|
||||||
|
*/
|
||||||
|
public void removeInitializedService(String serviceName) {
|
||||||
|
synchronized (monitor) {
|
||||||
|
initializedServices.remove(serviceName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the metadata {@link Map} of {@link DubboMetadataService}
|
* Get the metadata {@link Map} of {@link DubboMetadataService}
|
||||||
*
|
*
|
||||||
@ -565,7 +575,7 @@ public class DubboServiceMetadataRepository
|
|||||||
if (object == null) {
|
if (object == null) {
|
||||||
if (logger.isWarnEnabled()) {
|
if (logger.isWarnEnabled()) {
|
||||||
logger.warn(
|
logger.warn(
|
||||||
"DubboServiceMetadata can't be found in the Spring application [%s] and %s",
|
"DubboServiceMetadata can't be found in the Spring application [{}] and {}",
|
||||||
serviceName, requestMetadata);
|
serviceName, requestMetadata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -640,6 +650,10 @@ public class DubboServiceMetadataRepository
|
|||||||
dubboMetadataConfigServiceProxy.initProxy(serviceName, version);
|
dubboMetadataConfigServiceProxy.initProxy(serviceName, version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removeServiceMetadata(String serviceName) {
|
||||||
|
dubboRestServiceMetadataRepository.remove(serviceName);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setApplicationEventPublisher(
|
public void setApplicationEventPublisher(
|
||||||
ApplicationEventPublisher applicationEventPublisher) {
|
ApplicationEventPublisher applicationEventPublisher) {
|
||||||
|
@ -40,7 +40,6 @@ import org.apache.dubbo.common.URL;
|
|||||||
import org.apache.dubbo.registry.NotifyListener;
|
import org.apache.dubbo.registry.NotifyListener;
|
||||||
import org.apache.dubbo.registry.RegistryFactory;
|
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;
|
||||||
@ -215,6 +214,33 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
|||||||
generateId(url), serviceName);
|
generateId(url), serviceName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<URL> allSubscribedURLs = new LinkedList<>();
|
||||||
|
|
||||||
|
Collection<ServiceInstance> serviceInstances = serviceInstancesFunction
|
||||||
|
.apply(serviceName);
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(serviceInstances)) {
|
||||||
|
dubboMetadataConfigServiceProxy.removeProxy(serviceName);
|
||||||
|
repository.removeInitializedService(serviceName);
|
||||||
|
repository.removeServiceMetadata(serviceName);
|
||||||
|
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));
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("The subscribed URL[{}] will notify all URLs : {}", url,
|
||||||
|
allSubscribedURLs);
|
||||||
|
}
|
||||||
|
listener.notify(allSubscribedURLs);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy
|
DubboMetadataService dubboMetadataService = dubboMetadataConfigServiceProxy
|
||||||
.getProxy(serviceName);
|
.getProxy(serviceName);
|
||||||
|
|
||||||
@ -239,50 +265,30 @@ public abstract class AbstractSpringCloudRegistry extends FailbackRegistry {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Collection<ServiceInstance> serviceInstances = serviceInstancesFunction
|
List<URL> exportedURLs = getExportedURLs(dubboMetadataService, url);
|
||||||
.apply(serviceName);
|
|
||||||
|
|
||||||
List<URL> allSubscribedURLs = new LinkedList<>();
|
for (URL exportedURL : exportedURLs) {
|
||||||
|
String protocol = exportedURL.getProtocol();
|
||||||
if (CollectionUtils.isEmpty(serviceInstances)) {
|
List<URL> subscribedURLs = new LinkedList<>();
|
||||||
if (logger.isWarnEnabled()) {
|
serviceInstances.forEach(serviceInstance -> {
|
||||||
logger.warn(
|
Integer port = repository.getDubboProtocolPort(serviceInstance, protocol);
|
||||||
"There is no instance from service[name : {}], and then Dubbo Service[key : {}] will not be "
|
String host = serviceInstance.getHost();
|
||||||
+ "available , please make sure the further impact",
|
if (port == null) {
|
||||||
serviceName, url.getServiceKey());
|
if (logger.isWarnEnabled()) {
|
||||||
}
|
logger.warn(
|
||||||
/**
|
"The protocol[{}] port of Dubbo service instance[host : {}] "
|
||||||
* URLs with {@link RegistryConstants#EMPTY_PROTOCOL}
|
+ "can't be resolved",
|
||||||
*/
|
protocol, host);
|
||||||
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,
|
else {
|
||||||
exportedURL.getParameters());
|
URL subscribedURL = new URL(protocol, host, port,
|
||||||
subscribedURLs.add(subscribedURL);
|
exportedURL.getParameters());
|
||||||
}
|
subscribedURLs.add(subscribedURL);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
|
||||||
allSubscribedURLs.addAll(subscribedURLs);
|
allSubscribedURLs.addAll(subscribedURLs);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
|
@ -52,6 +52,14 @@ public class DubboMetadataServiceProxy implements BeanClassLoaderAware, Disposab
|
|||||||
name -> newProxy(name, version));
|
name -> newProxy(name, version));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove {@link DubboMetadataService}'s Proxy by service name
|
||||||
|
* @param serviceName the service name
|
||||||
|
*/
|
||||||
|
public void removeProxy(String serviceName) {
|
||||||
|
dubboMetadataServiceCache.remove(serviceName);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a proxy instance of {@link DubboMetadataService} via the specified service name
|
* Get a proxy instance of {@link DubboMetadataService} via the specified service name
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user