Consul安装

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

你直接下载文件,不要使用brew之类的安装。

Consul设置秘钥与启动

方式1

创建acl.hcl文件

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

启动脚本如下

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

方式二

创建acl.hcl文件

# ===============================
# Consul ACL 基础配置示例
# ===============================

# 节点所在数据中心标识(可自定义)
datacenter = "dc1"

# 日志等级:trace, debug, info, warn, err
log_level = "INFO"

# WAL 或 KV 数据保存目录,与启动参数中的 -data-dir 对应
data_dir = "./consuldata"

# 启用 ACL 功能
acl {
  enabled = true
  default_policy = "allow"   # 默认为 allow,生产环境建议为 "deny"
  enable_token_persistence = true

  # 启动时自动生成 root token 并在日志中打印出来
  tokens {
    master = "root-token-example-123456" # 可自定义或使用 random UUID
  }
}

# 启用 UI 管理界面(配合命令行参数 -ui)
ui_config {
  enabled = true
}

# 启用脚本健康检查(与参数 -enable-script-checks=true 一致)
enable_script_checks = true

# 本机客户端可访问的接口
addresses {
  http = "0.0.0.0"
  https = "0.0.0.0"
  grpc = "0.0.0.0"
}

# 保证集群通信(本地单机测试即可)
bind_addr = "0.0.0.0"
client_addr = "0.0.0.0"

启动脚本如下

./consul agent \
  -server \
  -bootstrap-expect=1 \
  -config-file=./acl.hcl \
  -ui

启动可能遇到的报错

==> Multiple private IPv4 addresses found. Please configure one with 'bind' and/or 'advertise'.

Consul 在启动时发现你的机器有多个私有 IPv4 地址(例如同时有 Wi‑Fi、以太网、虚拟网卡、Docker 或 VPN 等)。

它不知道该用哪个地址来与其他节点通信,所以建议你 显式指定一个 IP。

先查看你机器的局域网 IP(常用命令):

ifconfig | grep inet

你通常会看到类似:

inet 192.168.31.100 netmask 0xffffff00 broadcast 192.168.31.255
inet 127.0.0.1 netmask 0xff000000

将bind参数改为:127.0.0.1 或者 192.168.31.100 即可

Consul启动脚本讲解

启动脚本详解

  • 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-file=./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!

Consul没移除无效服务

consul注册中心没销毁最好的解决办法:停止consul服务,进入consul运行期间在其本地存储的信息consuldata,删除consuldata下的checks文件夹,启动consul。这样consul重启后会自动发现注册现有的服务。

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

免责声明:
本站文章旨在总结学习互联网技术过程中的经验与见解。任何人不得将其用于违法或违规活动!所有违规内容均由个人自行承担,与作者无关。