本篇內容主要講解“spring cloud的ConsulCatalogWatch有什么作用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“spring cloud的ConsulCatalogWatch有什么作用”吧!
我們注重客戶提出的每個要求,我們充分考慮每一個細節(jié),我們積極的做好做網站、成都做網站服務,我們努力開拓更好的視野,通過不懈的努力,創(chuàng)新互聯(lián)贏得了業(yè)內的良好聲譽,這一切,也不斷的激勵著我們更好的服務客戶。 主要業(yè)務:網站建設,網站制作,網站設計,重慶小程序開發(fā),網站開發(fā),技術開發(fā)實力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術開發(fā)工程師。
本文主要研究一下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,發(fā)布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;這里創(chuàng)建的是ThreadPoolTaskScheduler
ConsulCatalogWatch構造器接收ConsulDiscoveryProperties、ConsulClient、TaskScheduler;其start方法會使用taskScheduler.scheduleWithFixedDelay注冊catalogServicesWatch的定時任務;stop方法則是cancel掉這個定時任務;catalogServicesWatch方法使用consul.getCatalogServices方法獲取consulIndex然后更新本地的catalogServicesIndex,發(fā)布HeartbeatEvent
到此,相信大家對“spring cloud的ConsulCatalogWatch有什么作用”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!