如何进行consul注册acl token
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本篇文章给大家分享的是有关如何进行consul注册acl token,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。consul注册 ac
千家信息网最后更新 2026年11月02日如何进行consul注册acl token
本篇文章给大家分享的是有关如何进行consul注册acl token,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
consul注册 acl token 心跳 处理
已经验证 好使,心跳启动成功启动会有日志:Add Consul heartbeat for: xxxxxx
三个类放同意包下&在spring scanBasePackages 范围内
1.重写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 重新定义YJConsulHeartbeatAutoConfiguration * @Date 2019/10/8 11:40 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); }}2.重写心跳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 2019/10/3 4:54 下午 * @Description 重写心跳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() {//调用ConsulClient api 带token方法 YJTtlScheduler.this.client.agentCheckPass(this.checkId, null, token);if (log.isDebugEnabled()) {log.debug("Sending consul heartbeat for: " + this.checkId + ", token: " + token); } } }} 3.自己配置HeartbeatProperties
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;@NotNull private String ttlUnit = "s";@DecimalMin("0.1")@DecimalMax("0.9")private double intervalRatio = 0.6666666666666666D;public YJHeartbeatProperties() { }protected Period computeHeartbeatInterval() {double interval = (double)this.ttlValue * this.intervalRatio;double max = Math.max(interval, 1.0D);int ttlMinus1 = this.ttlValue - 1;double 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("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(); }}以上就是如何进行consul注册acl token,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。
更多
知识
篇文章
实用
成功
三个
启动会
好使
就是
工作会
文章
方法
日志
看吧
知识点
范围
行业
详情
资讯
资讯频道
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技公司简介
为想互联网科技怎么样
科技互联网政务服务
互联网品科技
没上市的科技互联网公司
北京互联网科技公司
奇酷互联网科技公司
互联网科技媒体
亿策互联网科技
火传媒上海互联网科技
福州新华互联网科技学校 知乎
互联网大会黑科技排名
河南猎农互联网科技有限公司
花神(南京)互联网科技有限公司
宜昌元轩互联网科技有限公司
苏州铭阳互联网科技有限公司
华夏恒生科技与互联网科技
互联网科技圈年度人物
互联网时代的科技公司
梅州GM互联网络科技有限公司
互联网科技公司
北京互联网络科技有限公司
上海喔噻互联网科技
汇久创客互联网科技
掌上梦互联网科技
互联网科技美容
滨湖区互联网智能科技产品供应
互联网科技生活的内容
互联网公司会议桌创意科技软装饰
武汉亿视互联网科技有限公司