mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
commit
fa21bc19ce
@ -20,6 +20,7 @@ import com.alibaba.cloud.nacos.ConditionalOnNacosDiscoveryEnabled;
|
||||
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
|
||||
import com.alibaba.cloud.nacos.NacosServiceManager;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@ -31,6 +32,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
@ -56,8 +58,10 @@ public class NacosDiscoveryClientConfiguration {
|
||||
@ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled",
|
||||
matchIfMissing = true)
|
||||
public NacosWatch nacosWatch(NacosServiceManager nacosServiceManager,
|
||||
NacosDiscoveryProperties nacosDiscoveryProperties) {
|
||||
return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties);
|
||||
NacosDiscoveryProperties nacosDiscoveryProperties,
|
||||
ObjectProvider<TaskScheduler> taskExecutorObjectProvider) {
|
||||
return new NacosWatch(nacosServiceManager, nacosDiscoveryProperties,
|
||||
taskExecutorObjectProvider);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -38,13 +38,12 @@ public class NacosServiceDiscovery {
|
||||
|
||||
private NacosDiscoveryProperties discoveryProperties;
|
||||
|
||||
private NamingService namingService;
|
||||
private NacosServiceManager nacosServiceManager;
|
||||
|
||||
public NacosServiceDiscovery(NacosDiscoveryProperties discoveryProperties,
|
||||
NacosServiceManager nacosServiceManager) {
|
||||
this.discoveryProperties = discoveryProperties;
|
||||
this.namingService = nacosServiceManager
|
||||
.getNamingService(discoveryProperties.getNacosProperties());
|
||||
this.nacosServiceManager = nacosServiceManager;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -55,7 +54,8 @@ public class NacosServiceDiscovery {
|
||||
*/
|
||||
public List<ServiceInstance> getInstances(String serviceId) throws NacosException {
|
||||
String group = discoveryProperties.getGroup();
|
||||
List<Instance> instances = namingService.selectInstances(serviceId, group, true);
|
||||
List<Instance> instances = namingService().selectInstances(serviceId, group,
|
||||
true);
|
||||
return hostToServiceInstanceList(instances, serviceId);
|
||||
}
|
||||
|
||||
@ -66,7 +66,7 @@ public class NacosServiceDiscovery {
|
||||
*/
|
||||
public List<String> getServices() throws NacosException {
|
||||
String group = discoveryProperties.getGroup();
|
||||
ListView<String> services = namingService.getServicesOfServer(1,
|
||||
ListView<String> services = namingService().getServicesOfServer(1,
|
||||
Integer.MAX_VALUE, group);
|
||||
return services.getData();
|
||||
}
|
||||
@ -111,4 +111,9 @@ public class NacosServiceDiscovery {
|
||||
return nacosServiceInstance;
|
||||
}
|
||||
|
||||
private NamingService namingService() {
|
||||
return nacosServiceManager
|
||||
.getNamingService(discoveryProperties.getNacosProperties());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@ -35,10 +36,13 @@ import com.alibaba.nacos.api.naming.pojo.Instance;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
@ -56,14 +60,27 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
||||
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
private ScheduledFuture<?> watchFuture;
|
||||
|
||||
private NacosServiceManager nacosServiceManager;
|
||||
|
||||
private final NacosDiscoveryProperties properties;
|
||||
|
||||
private final TaskScheduler taskScheduler;
|
||||
|
||||
public NacosWatch(NacosServiceManager nacosServiceManager,
|
||||
NacosDiscoveryProperties properties) {
|
||||
NacosDiscoveryProperties properties,
|
||||
ObjectProvider<TaskScheduler> taskScheduler) {
|
||||
this.nacosServiceManager = nacosServiceManager;
|
||||
this.properties = properties;
|
||||
this.taskScheduler = taskScheduler.getIfAvailable(NacosWatch::getTaskScheduler);
|
||||
}
|
||||
|
||||
private static ThreadPoolTaskScheduler getTaskScheduler() {
|
||||
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
||||
taskScheduler.setBeanName("Nacos-Watch-Task-Scheduler");
|
||||
taskScheduler.initialize();
|
||||
return taskScheduler;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -97,8 +114,6 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
||||
instanceOptional.ifPresent(currentInstance -> {
|
||||
resetIfNeeded(currentInstance);
|
||||
});
|
||||
publisher.publishEvent(new HeartbeatEvent(NacosWatch.this,
|
||||
nacosWatchIndex.getAndIncrement()));
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -112,6 +127,9 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
||||
catch (Exception e) {
|
||||
log.error("namingService subscribe failed, properties:{}", properties, e);
|
||||
}
|
||||
|
||||
this.watchFuture = this.taskScheduler.scheduleWithFixedDelay(
|
||||
this::nacosServicesWatch, this.properties.getWatchDelay());
|
||||
}
|
||||
}
|
||||
|
||||
@ -135,10 +153,17 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
||||
@Override
|
||||
public void stop() {
|
||||
if (this.running.compareAndSet(true, false)) {
|
||||
if (this.watchFuture != null) {
|
||||
// shutdown current user-thread,
|
||||
// then the other daemon-threads will terminate automatic.
|
||||
((ThreadPoolTaskScheduler) this.taskScheduler).shutdown();
|
||||
this.watchFuture.cancel(true);
|
||||
}
|
||||
|
||||
EventListener eventListener = listenerMap.get(buildKey());
|
||||
NamingService namingService = nacosServiceManager
|
||||
.getNamingService(properties.getNacosProperties());
|
||||
try {
|
||||
NamingService namingService = nacosServiceManager
|
||||
.getNamingService(properties.getNacosProperties());
|
||||
namingService.unsubscribe(properties.getService(), properties.getGroup(),
|
||||
Arrays.asList(properties.getClusterName()), eventListener);
|
||||
}
|
||||
@ -159,4 +184,12 @@ public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycl
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void nacosServicesWatch() {
|
||||
|
||||
// nacos doesn't support watch now , publish an event every 30 seconds.
|
||||
this.publisher.publishEvent(
|
||||
new HeartbeatEvent(this, nacosWatchIndex.getAndIncrement()));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user