十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
本篇内容主要讲解“spring cloud的ConsulCatalogWatch有什么作用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring cloud的ConsulCatalogWatch有什么作用”吧!
我们注重客户提出的每个要求,我们充分考虑每一个细节,我们积极的做好做网站、成都做网站服务,我们努力开拓更好的视野,通过不懈的努力,创新互联赢得了业内的良好声誉,这一切,也不断的激励着我们更好的服务客户。 主要业务:网站建设,网站制作,网站设计,重庆小程序开发,网站开发,技术开发实力,DIV+CSS,PHP及ASP,ASP.Net,SQL数据库的技术开发工程师。
本文主要研究一下spring cloud的ConsulCatalogWatch
spring-cloud-consul-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/discovery/ConsulCatalogWatch.java
public class ConsulCatalogWatch implements ApplicationEventPublisherAware, SmartLifecycle { private static final Log log = LogFactory.getLog(ConsulDiscoveryClient.class); private final ConsulDiscoveryProperties properties; private final ConsulClient consul; private final TaskScheduler taskScheduler; private final AtomicReferencecatalogServicesIndex = new AtomicReference<>(); private final AtomicBoolean running = new AtomicBoolean(false); private ApplicationEventPublisher publisher; private ScheduledFuture> watchFuture; public ConsulCatalogWatch(ConsulDiscoveryProperties properties, ConsulClient consul) { this(properties, consul, getTaskScheduler()); } public ConsulCatalogWatch(ConsulDiscoveryProperties properties, ConsulClient consul, TaskScheduler taskScheduler) { this.properties = properties; this.consul = consul; this.taskScheduler = taskScheduler; } private static ThreadPoolTaskScheduler getTaskScheduler() { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.initialize(); return taskScheduler; } @Override public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { this.publisher = publisher; } @Override public boolean isAutoStartup() { return true; } @Override public void stop(Runnable callback) { this.stop(); callback.run(); } @Override public void start() { if (this.running.compareAndSet(false, true)) { this.watchFuture = this.taskScheduler.scheduleWithFixedDelay( this::catalogServicesWatch, this.properties.getCatalogServicesWatchDelay()); } } @Override public void stop() { if (this.running.compareAndSet(true, false) && this.watchFuture != null) { this.watchFuture.cancel(true); } } @Override public boolean isRunning() { return false; } @Override public int getPhase() { return 0; } @Timed("consul.watch-catalog-services") public void catalogServicesWatch() { try { long index = -1; if (this.catalogServicesIndex.get() != null) { index = this.catalogServicesIndex.get().longValue(); } Response
ConsulCatalogWatch构造器接收ConsulDiscoveryProperties、ConsulClient、TaskScheduler;其start方法会使用taskScheduler.scheduleWithFixedDelay注册catalogServicesWatch的定时任务;stop方法则是cancel掉这个定时任务;catalogServicesWatch方法使用consul.getCatalogServices方法获取consulIndex然后更新本地的catalogServicesIndex,发布HeartbeatEvent
spring-cloud-consul-discovery-2.1.2.RELEASE-sources.jar!/org/springframework/cloud/consul/discovery/ConsulDiscoveryClientConfiguration.java
@Configuration @ConditionalOnConsulEnabled @ConditionalOnProperty(value = "spring.cloud.consul.discovery.enabled", matchIfMissing = true) @ConditionalOnDiscoveryEnabled @EnableConfigurationProperties @AutoConfigureBefore({ SimpleDiscoveryClientAutoConfiguration.class, CommonsClientAutoConfiguration.class }) public class ConsulDiscoveryClientConfiguration { /** * Name of the catalog watch task scheduler bean. */ public static final String CATALOG_WATCH_TASK_SCHEDULER_NAME = "catalogWatchTaskScheduler"; @Autowired private ConsulClient consulClient; @Bean @ConditionalOnMissingBean @ConditionalOnProperty("spring.cloud.consul.discovery.heartbeat.enabled") // TODO: move to service-registry for Edgware public TtlScheduler ttlScheduler(HeartbeatProperties heartbeatProperties) { return new TtlScheduler(heartbeatProperties, this.consulClient); } @Bean @ConditionalOnMissingBean // TODO: move to service-registry for Edgware public HeartbeatProperties heartbeatProperties() { return new HeartbeatProperties(); } @Bean @ConditionalOnMissingBean // TODO: Split appropriate values to service-registry for Edgware public ConsulDiscoveryProperties consulDiscoveryProperties(InetUtils inetUtils) { return new ConsulDiscoveryProperties(inetUtils); } @Bean @ConditionalOnMissingBean public ConsulDiscoveryClient consulDiscoveryClient( ConsulDiscoveryProperties discoveryProperties) { return new ConsulDiscoveryClient(this.consulClient, discoveryProperties); } @Bean @ConditionalOnMissingBean @ConditionalOnProperty(name = "spring.cloud.consul.discovery.catalog-services-watch.enabled", matchIfMissing = true) public ConsulCatalogWatch consulCatalogWatch( ConsulDiscoveryProperties discoveryProperties, @Qualifier(CATALOG_WATCH_TASK_SCHEDULER_NAME) TaskScheduler taskScheduler) { return new ConsulCatalogWatch(discoveryProperties, this.consulClient, taskScheduler); } @Bean(name = CATALOG_WATCH_TASK_SCHEDULER_NAME) @ConditionalOnProperty(name = "spring.cloud.consul.discovery.catalog-services-watch.enabled", matchIfMissing = true) public TaskScheduler catalogWatchTaskScheduler() { return new ThreadPoolTaskScheduler(); } }
ConsulDiscoveryClientConfiguration会注册ConsulCatalogWatch,其使用了名为catalogWatchTaskScheduler的taskScheduler;这里创建的是ThreadPoolTaskScheduler
ConsulCatalogWatch构造器接收ConsulDiscoveryProperties、ConsulClient、TaskScheduler;其start方法会使用taskScheduler.scheduleWithFixedDelay注册catalogServicesWatch的定时任务;stop方法则是cancel掉这个定时任务;catalogServicesWatch方法使用consul.getCatalogServices方法获取consulIndex然后更新本地的catalogServicesIndex,发布HeartbeatEvent
到此,相信大家对“spring cloud的ConsulCatalogWatch有什么作用”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!