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 principle and function of TtlScheduler in spring cloud consul

2025-01-18 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 principle and function of TtlScheduler in spring cloud consul". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the principle and function of TtlScheduler in spring cloud consul".

Order

This paper mainly studies the TtlScheduler of spring cloud consul.

TtlScheduler

SpringMurray cloudhouse consortium house discoveryMuray 2.1.2.RELEASEMethsources.jarlemet org.springframeworkqcloudlxxxxxxxxxxxxx

Public class TtlScheduler {private static final Log log = LogFactory.getLog (ConsulDiscoveryClient.class); private final Map serviceHeartbeats = new ConcurrentHashMap (); private final TaskScheduler scheduler = new ConcurrentTaskScheduler (Executors.newSingleThreadScheduledExecutor ()); private HeartbeatProperties configuration; private ConsulClient client; public TtlScheduler (HeartbeatProperties configuration, ConsulClient client) {this.configuration = configuration; this.client = client } @ Deprecated public void add (final NewService service) {add (service.getId ());} / * * Add a service to the checks loop. * @ param instanceId instance id * / public void add (String instanceId) {ScheduledFuture task = this.scheduler.scheduleAtFixedRate (new ConsulHeartbeatTask (instanceId), this.configuration .computeHearbeatInterval () .toStandardDuration () .getMillis ()); ScheduledFuture previousTask = this.serviceHeartbeats.put (instanceId, task) If (previousTask! = null) {previousTask.cancel (true);}} public void remove (String instanceId) {ScheduledFuture task = this.serviceHeartbeats.get (instanceId); if (task! = null) {task.cancel (true) } this.serviceHeartbeats.remove (instanceId);} private class ConsulHeartbeatTask implements Runnable {private String checkId; ConsulHeartbeatTask (String serviceId) {this.checkId = serviceId If (! this.checkId.startsWith ("service:")) {this.checkId = "service:" + this.checkId;}} @ Override public void run () {TtlScheduler.this.client.agentCheckPass (this.checkId) If (log.isDebugEnabled ()) {log.debug ("Sending consul heartbeat for:" + this.checkId);}}

The constructor of TtlScheduler receives HeartbeatProperties, and the ConsulClient;add method registers a timed execution ConsulHeartbeatTask with scheduler, and the execution interval is calculated by ttlValue and intervalRatio of HeartbeatProperties. If the previousTask is not null, cancel calls ScheduledFuture if it is not null, and then removes it from serviceHeartbeats. The run method of ConsulHeartbeatTask executes client.agentCheckPass.

HeartbeatProperties

SpringMurray cloudhouse consortium house discoverylue 2.1.2.RELEASEMethsources.jarlemachorgframeworkqcloudlyconsultsdiscoveryhip HeartbeatProperties.java

@ ConfigurationProperties (prefix = "spring.cloud.consul.discovery.heartbeat") @ Validatedpublic class HeartbeatProperties {private static final Log log = org.apache.commons.logging.LogFactory .getLog (HeartbeatProperties.class); / / TODO: change enabled to default to true when I stop seeing messages like / / [WARN] agent: Check 'service:testConsulApp:xtest:8080' missed TTL, is now critical boolean enabled = false @ Min (1) private int ttlValue = 30; @ NotNull private String ttlUnit = "s"; @ DecimalMin ("0.1") @ DecimalMax (" 0.9 ") private double intervalRatio = 2.0 / 3.0; / / TODO: did heartbeatInterval need to bea field? Protected Period computeHearbeatInterval () {/ / heartbeat rate at ratio * ttl, but no later than ttl-1s and, (under lesser / / priority), no sooner than 1s from now double interval = this.ttlValue * this.intervalRatio; double max = Math.max (interval, 1); int ttlMinus1 = this.ttlValue-1; double min = Math.min (ttlMinus1, max) Period heartbeatInterval = new Period (Math.round (1000 * min)); log.debug ("Computed heartbeatInterval:" + heartbeatInterval); return heartbeatInterval;} public String getTtl () {return this.ttlValue + this.ttlUnit;} public boolean isEnabled () {return this.enabled } public void setEnabled (boolean enabled) {this.enabled = enabled;} public @ Min (1) int getTtlValue () {return this.ttlValue;} public void setTtlValue (@ Min (1) int ttlValue) {this.ttlValue = ttlValue } public @ NotNull String getTtlUnit () {return this.ttlUnit;} public void setTtlUnit (@ NotNull String ttlUnit) {this.ttlUnit = ttlUnit;} public @ DecimalMin ("0.1") @ DecimalMax (" 0.9 ") double getIntervalRatio () {return this.intervalRatio } public void setIntervalRatio (@ DecimalMin ("0.1") @ DecimalMax ("0.9") double intervalRatio) {this.intervalRatio = intervalRatio } @ Override public String toString () {return new ToStringCreator (this) .append ("enabled", this.enabled) .append ("ttlValue", this.ttlValue) .append ("ttlUnit", this.ttlUnit) .append ("intervalRatio", this.intervalRatio). ToString ();}}

HeartbeatProperties has ttlValue, intervalRatio and other attributes, and its computeHearbeatInterval method takes the minimum values of ttlMinus1 and Math.max (interval, 1) as heartbeatInterval.

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 automatically registers ttlScheduler under @ ConditionalOnMissingBean and @ ConditionalOnProperty ("spring.cloud.consul.discovery.heartbeat.enabled").

Summary

The constructor of TtlScheduler receives HeartbeatProperties, and the ConsulClient;add method registers a timed execution ConsulHeartbeatTask with scheduler, and the execution interval is calculated by ttlValue and intervalRatio of HeartbeatProperties. If the previousTask is not null, cancel calls ScheduledFuture if it is not null, and then removes it from serviceHeartbeats. The run method of ConsulHeartbeatTask executes client.agentCheckPass.

Thank you for your reading, the above is the content of "what is the principle and function of TtlScheduler in spring cloud consul". After the study of this article, I believe you have a deeper understanding of what is the principle and function of TtlScheduler in spring cloud consul. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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