Commit 01fbac34 authored by jiaorz's avatar jiaorz

修改代码

parent 61e9b976
...@@ -28,8 +28,8 @@ ...@@ -28,8 +28,8 @@
</dependency> </dependency>
<!--<dependency>--> <!--<dependency>-->
<!--<groupId>org.springframework.cloud</groupId>--> <!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>spring-cloud-starter-consul-all</artifactId>--> <!--<artifactId>spring-cloud-starter-consul-all</artifactId>-->
<!--</dependency>--> <!--</dependency>-->
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
......
...@@ -39,85 +39,85 @@ import java.util.concurrent.atomic.AtomicLong; ...@@ -39,85 +39,85 @@ import java.util.concurrent.atomic.AtomicLong;
*/ */
public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycle { public class NacosWatch implements ApplicationEventPublisherAware, SmartLifecycle {
private static final Logger log = LoggerFactory.getLogger(NacosWatch.class); private static final Logger log = LoggerFactory.getLogger(NacosWatch.class);
private final NacosDiscoveryProperties properties; private final NacosDiscoveryProperties properties;
private long watchDelay = 30000; private long watchDelay = 30000;
private final TaskScheduler taskScheduler; private final TaskScheduler taskScheduler;
private final AtomicLong nacosWatchIndex = new AtomicLong(0); private final AtomicLong nacosWatchIndex = new AtomicLong(0);
private final AtomicBoolean running = new AtomicBoolean(false); private final AtomicBoolean running = new AtomicBoolean(false);
private ApplicationEventPublisher publisher; private ApplicationEventPublisher publisher;
private ScheduledFuture<?> watchFuture; private ScheduledFuture<?> watchFuture;
private Set<String> cacheServices = new HashSet<>(); private Set<String> cacheServices = new HashSet<>();
private HashMap<String, EventListener> subscribeListeners = new HashMap<>(); private HashMap<String, EventListener> subscribeListeners = new HashMap<>();
public NacosWatch(NacosDiscoveryProperties properties) { public NacosWatch(NacosDiscoveryProperties properties) {
this(properties, getTaskScheduler()); this(properties, getTaskScheduler());
} }
public NacosWatch(NacosDiscoveryProperties properties, TaskScheduler taskScheduler) { public NacosWatch(NacosDiscoveryProperties properties, TaskScheduler taskScheduler) {
this.properties = properties; this.properties = properties;
this.taskScheduler = taskScheduler; this.taskScheduler = taskScheduler;
} }
private static ThreadPoolTaskScheduler getTaskScheduler() { private static ThreadPoolTaskScheduler getTaskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.initialize(); taskScheduler.initialize();
return taskScheduler; return taskScheduler;
} }
@Override @Override
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.publisher = publisher; this.publisher = publisher;
} }
@Override @Override
public boolean isAutoStartup() { public boolean isAutoStartup() {
return true; return true;
} }
@Override @Override
public void stop(Runnable callback) { public void stop(Runnable callback) {
this.stop(); this.stop();
callback.run(); callback.run();
} }
@Override @Override
public void start() { public void start() {
if (this.running.compareAndSet(false, true)) { if (this.running.compareAndSet(false, true)) {
this.watchFuture = this.taskScheduler.scheduleWithFixedDelay( this.watchFuture = this.taskScheduler.scheduleWithFixedDelay(
this::nacosServicesWatch, watchDelay); this::nacosServicesWatch, watchDelay);
} }
} }
@Override @Override
public void stop() { public void stop() {
if (this.running.compareAndSet(true, false) && this.watchFuture != null) { if (this.running.compareAndSet(true, false) && this.watchFuture != null) {
this.watchFuture.cancel(true); this.watchFuture.cancel(true);
} }
} }
@Override @Override
public boolean isRunning() { public boolean isRunning() {
return false; return false;
} }
@Override @Override
public int getPhase() { public int getPhase() {
return 0; return 0;
} }
public void nacosServicesWatch() { public void nacosServicesWatch() {
// nacos doesn't support watch now , publish an event every 30 seconds. // nacos doesn't support watch now , publish an event every 30 seconds.
this.publisher.publishEvent( this.publisher.publishEvent(
new HeartbeatEvent(this, nacosWatchIndex.getAndIncrement())); new HeartbeatEvent(this, nacosWatchIndex.getAndIncrement()));
} }
} }
...@@ -8,11 +8,11 @@ import org.springframework.context.annotation.Configuration; ...@@ -8,11 +8,11 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class NacosWatchAutoConfiguration { public class NacosWatchAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled", matchIfMissing = true) @ConditionalOnProperty(value = "spring.cloud.nacos.discovery.watch.enabled", matchIfMissing = true)
public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties) { public NacosWatch nacosWatch(NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosWatch(nacosDiscoveryProperties); return new NacosWatch(nacosDiscoveryProperties);
} }
} }
\ No newline at end of file
spring: spring:
application: application:
name: ace-monitor name: ace-monitor
cloud: cloud:
nacos: nacos:
discovery: discovery:
server-addr: 127.0.0.1:8848 server-addr: 127.0.0.1:8848
server: server:
port: 8764 #启动端口 port: 8764 #启动端口
# #
#eureka: #eureka:
# instance: # instance:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment