How to register acl token for consul
This article is to share with you about how to register acl token for consul. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Consul registration acl token heartbeat processing
It has been verified that it works. If the heartbeat starts successfully, there will be a log: Add Consul heartbeat for: xxxxxx
Three classes under the consent package & within the scope of spring scanBasePackages
1. Rewrite ConsulHeartbeatAutoConfiguration
Package com.support.consul;import com.ecwid.consul.v1.ConsulClient;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.boot.autoconfigure.AutoConfigureBefore;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;import org.springframework.cloud.consul.ConditionalOnConsulEnabled;import org.springframework.cloud.consul.discovery.ConsulDiscoveryClientConfiguration;import org.springframework.cloud.consul.discovery.TtlScheduler;import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistryAutoConfiguration Import org.springframework.cloud.consul.support.ConsulHeartbeatAutoConfiguration;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration / * * @ Author luhaijun * @ Description redefine YJConsulHeartbeatAutoConfiguration * @ Date 11:40 on 2019-10-8 AM * * / @ Configuration@ConditionalOnConsulEnabled@ConditionalOnProperty ({"spring.cloud.consul.discovery.heartbeat.enabled"}) @ ConditionalOnDiscoveryEnabled@AutoConfigureBefore ({ConsulServiceRegistryAutoConfiguration.class, ConsulHeartbeatAutoConfiguration.class}) @ AutoConfigureAfter ({ConsulDiscoveryClientConfiguration.class}) public class YJConsulHeartbeatAutoConfiguration {@ Value ("${spring.cloud.consul.discovery.acl-token:null}") private String token Public YJConsulHeartbeatAutoConfiguration () {} @ Bean public YJHeartbeatProperties yjHeartbeatProperties () {return new YJHeartbeatProperties ();} @ Bean public TtlScheduler ttlScheduler (YJHeartbeatProperties yjHeartbeatProperties, ConsulClient consulClient) {return new YJTtlScheduler (yjHeartbeatProperties, consulClient, token);}}
two。 Rewrite heartbeat Scheduler
Package com.support.consul;import com.ecwid.consul.v1.ConsulClient;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.cloud.consul.discovery.TtlScheduler;import org.springframework.scheduling.TaskScheduler;import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledFuture / * * @ Author lbm * @ Date 4:54 afternoon on 2019-10-3 * @ Description rewrite heartbeat Scheduler * * / public class YJTtlScheduler extends TtlScheduler {private static final Log log = LogFactory.getLog (YJTtlScheduler.class); private final Map serviceHeartbeats = new ConcurrentHashMap (); private final TaskScheduler scheduler = new ConcurrentTaskScheduler (Executors.newSingleThreadScheduledExecutor ()); private YJHeartbeatProperties configuration;private ConsulClient client;private String token;public YJTtlScheduler (YJHeartbeatProperties configuration, ConsulClient client, String token) {super (null, null); this.configuration = configuration;this.client = client This.token = token;} @ Override public void add (String instanceId) {ScheduledFuture task = this.scheduler.scheduleAtFixedRate (new YJTtlScheduler.ConsulHeartbeatTask (instanceId), this.configuration.computeHeartbeatInterval (). ToStandardDuration (). GetMillis ()); ScheduledFuture previousTask = (ScheduledFuture) this.serviceHeartbeats.put (instanceId, task); if (previousTask! = null) {previousTask.cancel (true);} log.info ("Add Consul heartbeat for:" + instanceId) @ Override public void remove (String instanceId) {ScheduledFuture task = (ScheduledFuture) 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 () {/ / call ConsulClient api with token method YJTtlScheduler.this.client.agentCheckPass (this.checkId, null, token); if (log.isDebugEnabled ()) {log.debug ("Sending consul heartbeat for:" + this.checkId + ", token:" + token);}
3. Configure HeartbeatProperties yourself
Package com.support.consul;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.joda.time.Period;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.core.style.ToStringCreator;import org.springframework.validation.annotation.Validated;import javax.validation.constraints.DecimalMax;import javax.validation.constraints.DecimalMin;import javax.validation.constraints.Min;import javax.validation.constraints.NotNull @ ConfigurationProperties (prefix = "spring.cloud.consul.discovery.heartbeat") @ Validatedpublic class YJHeartbeatProperties {private static final Log log = LogFactory.getLog (YJHeartbeatProperties.class); boolean enabled = false;@Min (1L) private int ttlValue = 30 boot NotNull private String ttlUnit = "s"; @ DecimalMin ("0.1") @ DecimalMax ("0.90") private double intervalRatio = 0.666666666666DX public YJHeartbeatProperties () {} protected Period computeHeartbeatInterval () {double interval = (double) this.ttlValue * this.intervalRatio Double max = Math.max (interval, 1.0D); int ttlMinus1 = this.ttlValue-1 max min = Math.min ((double) ttlMinus1, max); Period heartbeatInterval = new Period (Math.round (1000.0D * 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 } @ Min (1L) public int getTtlValue () {return this.ttlValue;} public void setTtlValue (@ Min (1L) int ttlValue) {this.ttlValue = ttlValue;} @ NotNull public String getTtlUnit () {return this.ttlUnit;} public void setTtlUnit (@ NotNull String ttlUnit) {this.ttlUnit = ttlUnit;} @ DecimalMin ("0.1") @ DecimalMax (" 0.9 ") public double getIntervalRatio () {return this.intervalRatio } public void setIntervalRatio (@ DecimalMin) @ DecimalMax 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 () }} the above is how to register acl token for consul. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.