Consul安装

Consul官网下载地址:https://developer.hashicorp.com/consul/install

Consul设置秘钥

{
    "acl": {
        "enabled": true,
        "default_policy": "deny",
        "tokens": {
            "master": "xxxxxxxxx-xxxxxxxxx-xaaaaaa"
        }
    }
}

Consul启动脚本

nohup ./consul agent -server -bootstrap-expect=1 -data.dir=./consuldata -node=consul-server-1 -bind=0.0.0.0 -config-dir=./acl.hcl -enable-script-checks=true -client=0.0.0.0 -ui &

启动脚本详解

  • nohup: 这个命令用于在后台运行程序,即使终端关闭也不会中断程序的执行。
  • ./consul agent: 这启动了Consul代理程序。
  • -server: 这指定了Consul代理作为服务器运行。
  • -bootstrap-expect=1: 这个参数指定了在启动期间预期的服务器节点数。在这种情况下,只有一个服务器节点。
  • -data.dir=./consuldata: 这个参数指定了Consul数据存储的目录。
  • -node=consul-server-1: 这定义了Consul服务器的节点名称。
  • -bind=0.0.0.0: 这指定了Consul绑定到的IP地址。
  • -config-dir=./acl.hcl: 这指定了ACL配置文件的目录。
  • -enable-script-checks=true: 这启用了脚本检查功能。
  • -client=0.0.0.0: 这指定了Consul绑定到的客户端IP地址。
  • -ui: 这启用了Consul的Web用户界面。

Consul配置权限

Spring配置文件

Consul的aclTokne秘钥添加

添加配置前,先看看aclToken的字段吧。

package com.tencent.tsf.discovery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;

@ConfigurationProperties("tsf.discovery")
public class TsfDiscoveryProperties {
    protected static final String MANAGEMENT = "management";
    private InetUtils.HostInfo hostInfo;
    @Value("${tsf_token:${consul.token:${CONSUL_TOKEN:${spring.cloud.consul.token:${SPRING_CLOUD_CONSUL_TOKEN:}}}}}")
    private String aclToken;
    private List<String> tags;
    private boolean enabled;
......
}

源码分析如下:tsf.discovery.aclToken。或者tsf_token、或者consul.token。

所以就知道了注册中心的配置了。我使用的是consul.token!

特殊说明:
上述文章均是作者实际操作后产出。烦请各位,请勿直接盗用!转载记得标注原文链接:www.zanglikun.com
第三方平台不会及时更新本文最新内容。如果发现本文资料不全,可访问本人的Java博客搜索:标题关键字。以获取全部资料 ❤