mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish alibaba/spring-cloud-alibaba/#1194 : [Enhancement] NacosWatch optimization
This commit is contained in:
parent
6b3b017209
commit
5b8d25ef9c
@ -19,6 +19,7 @@ package com.alibaba.cloud.nacos.discovery;
|
|||||||
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
|
import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
|
||||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
@ -30,6 +31,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|||||||
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
|
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.scheduling.TaskScheduler;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
@ -52,10 +54,10 @@ public class NacosDiscoveryClientConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled",
|
@ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled", matchIfMissing = true)
|
||||||
matchIfMissing = true)
|
public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties,
|
||||||
public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties) {
|
ObjectProvider<TaskScheduler> taskScheduler) {
|
||||||
return new NacosWatch(nacosDiscoveryProperties);
|
return new NacosWatch(nacosDiscoveryProperties, taskScheduler);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,18 +16,15 @@
|
|||||||
|
|
||||||
package com.alibaba.cloud.nacos.discovery;
|
package com.alibaba.cloud.nacos.discovery;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||||
import com.alibaba.nacos.api.naming.listener.EventListener;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
|
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.context.ApplicationEventPublisherAware;
|
import org.springframework.context.ApplicationEventPublisherAware;
|
||||||
@ -54,10 +51,6 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
|||||||
|
|
||||||
private ScheduledFuture<?> watchFuture;
|
private ScheduledFuture<?> watchFuture;
|
||||||
|
|
||||||
private Set<String> cacheServices = new HashSet<>();
|
|
||||||
|
|
||||||
private HashMap<String, EventListener> subscribeListeners = new HashMap<>();
|
|
||||||
|
|
||||||
public NacosWatch(NacosDiscoveryProperties properties) {
|
public NacosWatch(NacosDiscoveryProperties properties) {
|
||||||
this(properties, getTaskScheduler());
|
this(properties, getTaskScheduler());
|
||||||
}
|
}
|
||||||
@ -67,8 +60,21 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
|||||||
this.taskScheduler = taskScheduler;
|
this.taskScheduler = taskScheduler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constructor with {@link NacosDiscoveryProperties} bean and the optional
|
||||||
|
* {@link TaskScheduler} bean
|
||||||
|
* @param properties {@link NacosDiscoveryProperties} bean
|
||||||
|
* @param taskScheduler the optional {@link TaskScheduler} bean
|
||||||
|
* @since 2.2.0
|
||||||
|
*/
|
||||||
|
public NacosWatch(NacosDiscoveryProperties properties,
|
||||||
|
ObjectProvider<TaskScheduler> taskScheduler) {
|
||||||
|
this(properties, taskScheduler.getIfAvailable(NacosWatch::getTaskScheduler));
|
||||||
|
}
|
||||||
|
|
||||||
private static ThreadPoolTaskScheduler getTaskScheduler() {
|
private static ThreadPoolTaskScheduler getTaskScheduler() {
|
||||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||||
|
taskScheduler.setBeanName("Nacso-Watch-Task-Scheduler");
|
||||||
taskScheduler.initialize();
|
taskScheduler.initialize();
|
||||||
return taskScheduler;
|
return taskScheduler;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user