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

init DubboCloudRegistry when subscribe or unsubscribe

This commit is contained in:
theonefx 2021-06-22 17:14:34 +08:00
parent 2a8647c263
commit e4f2f4150c
4 changed files with 34 additions and 21 deletions

View File

@ -10,7 +10,6 @@
</parent> </parent>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-dubbo-client-sample</artifactId> <artifactId>spring-cloud-dubbo-client-sample</artifactId>
<name>Spring Cloud Dubbo Client Sample</name> <name>Spring Cloud Dubbo Client Sample</name>

View File

@ -12,7 +12,11 @@ spring:
allow-bean-definition-overriding: true allow-bean-definition-overriding: true
cloud: cloud:
nacos: nacos:
discovery:
username: nacos username: nacos
password: nacos password: nacos
discovery:
server-addr: 127.0.0.1:8848 server-addr: 127.0.0.1:8848
namespace: public
server:
port: 8080

View File

@ -1,4 +1,6 @@
dubbo: dubbo:
cloud:
subscribed-services: ${spring.application.name}
scan: scan:
base-packages: com.alibaba.cloud.dubbo.bootstrap base-packages: com.alibaba.cloud.dubbo.bootstrap
protocol: protocol:

View File

@ -46,7 +46,6 @@ 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.context.event.ContextRefreshedEvent;
import static com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository.EXPORTED_SERVICES_REVISION_PROPERTY_NAME; import static com.alibaba.cloud.dubbo.metadata.repository.DubboServiceMetadataRepository.EXPORTED_SERVICES_REVISION_PROPERTY_NAME;
import static java.util.Collections.emptyList; import static java.util.Collections.emptyList;
@ -118,9 +117,6 @@ public class DubboCloudRegistry extends FailbackRegistry
this.applicationContext = applicationContext; this.applicationContext = applicationContext;
this.dubboMetadataUtils = getBean(DubboMetadataUtils.class); this.dubboMetadataUtils = getBean(DubboMetadataUtils.class);
this.reSubscribeManager = new ReSubscribeManager(this); this.reSubscribeManager = new ReSubscribeManager(this);
applicationContext.addApplicationListener(
(ApplicationListener<ContextRefreshedEvent>) event -> preInit());
} }
private void preInit() { private void preInit() {
@ -154,7 +150,8 @@ public class DubboCloudRegistry extends FailbackRegistry
urlSubscribeHandlerMap.forEach((url, handler) -> handler.init()); urlSubscribeHandlerMap.forEach((url, handler) -> handler.init());
repository.initializeMetadata(); repository.initializeMetadata();
// meke sure everything prepared, then can listening ServiceInstanceChangeEvent // meke sure everything prepared, then can listening
// ServiceInstanceChangeEvent
applicationContext.addApplicationListener(this); applicationContext.addApplicationListener(this);
logger.info("DubboCloudRegistry preInit Done."); logger.info("DubboCloudRegistry preInit Done.");
@ -165,39 +162,50 @@ public class DubboCloudRegistry extends FailbackRegistry
return this.applicationContext.getBean(beanClass); return this.applicationContext.getBean(beanClass);
} }
protected boolean shouldRegister(URL url) { protected boolean shouldNotRegister(URL url) {
String side = url.getParameter(SIDE_KEY); 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()) { if (logger.isDebugEnabled()) {
logger.debug("The URL[{}] should not be registered.", url.toString()); if (!should) {
logger.debug("The URL should NOT!! be registered & unregistered [{}] .",
url);
}
else {
logger.debug("The URL should be registered & unregistered [{}] .", url);
} }
} }
return should; return !should;
} }
@Override @Override
public final void doRegister(URL url) { public final void doRegister(URL url) {
if (!shouldRegister(url)) { synchronized (this) {
preInit();
if (shouldNotRegister(url)) {
return; return;
} }
repository.exportURL(url); repository.exportURL(url);
} }
}
@Override @Override
public final void doUnregister(URL url) { public final void doUnregister(URL url) {
if (!shouldRegister(url)) { synchronized (this) {
preInit();
if (shouldNotRegister(url)) {
return; return;
} }
repository.unexportURL(url); repository.unexportURL(url);
} }
}
@Override @Override
public final void doSubscribe(URL url, NotifyListener listener) { public final void doSubscribe(URL url, NotifyListener listener) {
synchronized (this) { synchronized (this) {
preInit();
if (isAdminURL(url)) { if (isAdminURL(url)) {
// TODO in future // TODO in future
if (logger.isWarnEnabled()) { if (logger.isWarnEnabled()) {
@ -452,7 +460,7 @@ public class DubboCloudRegistry extends FailbackRegistry
return metadata.containsKey(METADATA_SERVICE_URLS_PROPERTY_NAME); return metadata.containsKey(METADATA_SERVICE_URLS_PROPERTY_NAME);
} }
Set<String> getServices(URL url) { private Set<String> getServices(URL url) {
Set<String> subscribedServices = repository.getSubscribedServices(); Set<String> subscribedServices = repository.getSubscribedServices();
if (subscribedServices.contains("*")) { if (subscribedServices.contains("*")) {
subscribedServices = new HashSet<>(discoveryClient.getServices()); subscribedServices = new HashSet<>(discoveryClient.getServices());