Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of spring cloud's ConsulCatalogWatch?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the function of spring cloud's ConsulCatalogWatch". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what is the role of spring cloud's ConsulCatalogWatch?"

Order

This paper mainly studies the ConsulCatalogWatch of spring cloud.

ConsulCatalogWatch

SpringMurray cloudhouse consortium house discoveryMuray 2.1.2.RELEASEMethsources.jarroomUniverse org.springframeworkqcloudUniverse consultio discoveryUnix ConsulogWatch.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 AtomicReference catalogServicesIndex = 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 response = this.consul.getCatalogServices (new QueryParams (this.properties.getCatalogServicesWatchTimeout (), index), this.properties.getAclToken ()) Long consulIndex = response.getConsulIndex (); if (consulIndex! = null) {this.catalogServicesIndex.set (BigInteger.valueOf (consulIndex)) } if (log.isTraceEnabled ()) {log.trace ("Received services update from consul:" + response.getValue () + ", index:" + consulIndex) } this.publisher.publishEvent (new HeartbeatEvent (this, consulIndex));} catch (Exception e) {log.error ("Error watching Consul CatalogServices", e);}

The ConsulCatalogWatch constructor receives ConsulDiscoveryProperties, ConsulClient, and TaskScheduler; whose start method registers the scheduled task of catalogServicesWatch with taskScheduler.scheduleWithFixedDelay; the stop method cancel the scheduled task; the catalogServicesWatch method uses the consul.getCatalogServices method to get the consulIndex and then updates the local catalogServicesIndex to publish the HeartbeatEvent

ConsulDiscoveryClientConfiguration

SpringMurray cloudhouse consortium house discoveryMuray 2.1.2.RELEASEMethsources.jarroomUniverse org.springframeworkqcloudlash consultio discoveryplac DiscoveryClientConfiguration.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 registers ConsulCatalogWatch, which uses a taskScheduler; named catalogWatchTaskScheduler, which is created here as ThreadPoolTaskScheduler

Summary

The ConsulCatalogWatch constructor receives ConsulDiscoveryProperties, ConsulClient, and TaskScheduler; whose start method registers the scheduled task of catalogServicesWatch with taskScheduler.scheduleWithFixedDelay; the stop method cancel the scheduled task; the catalogServicesWatch method uses the consul.getCatalogServices method to get the consulIndex and then updates the local catalogServicesIndex to publish the HeartbeatEvent

At this point, I believe you have a deeper understanding of "what is the role of spring cloud's ConsulCatalogWatch". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report