注册中心Consul

https://www.consul.io/

https://developer.hashicorp.com/consul/commands

https://developer.hashicorp.com/consul/docs/agent/config – agent 配置

https://developer.hashicorp.com/consul/docs/agent/config/cli-flags – agent 命令行参考

https://developer.hashicorp.com/consul/docs/agent/config/config-files – agent 配置文件参考

Linux部署

解压

1
2
3
mkdir /opt/local/consul
unzip /opt/local/consul_1.20.3_linux_amd64.zip -d /opt/local/consul
chmod +x /opt/local/consul/consul

启动(agent配置文件)

1
2
3
4
BASE_DIR=/opt/local/consul
mkdir -p $BASE_DIR/data
mkdir -p $BASE_DIR/config
mkdir -p $BASE_DIR/log

vim $BASE_DIR/config/server.json,配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"node_name": "node-name-1",
"datacenter": "dc-1",
"client_addr": "0.0.0.0",
"bind_addr": "192.168.0.7",
"data_dir": "/opt/local/consul/data",
"log_file": "/opt/local/consul/log/",
"log_level": "INFO",
"ui_config": {
"enabled": true
},
"server": true,
"bootstrap_expect": 1
}

client_addr: 默认127.0.0.1。Consul 将绑定客户端接口的地址,即哪些 IP 可以注册。

bind_addr:默认0.0.0.0。应绑定到用于内部集群通信的地址(能被集群中所有其他节点访问的IP地址)。如果有多个可用的私有IPv4地址,Consul将在启动时退出并出现错误。

bootstrap:此标志用于控制服务器是否处于“引导”模式。每个数据中心在此模式下运行的服务器不得超过一台。单例模式下,才需要配置 bootstrap=truebootstrap_expect=1 。

bootstrap_expect:此标志提供数据中心中预期的服务器数量。不应提供此值,或者该值必须与集群中的其他服务器一致。此标志需要 -server 模式。

1
2
# 启动
/opt/local/consul/consul agent -config-file=/opt/local/consul/config/server.json

启动(agent命令行)

注意:编写的启动脚本,必须以类似 #!/bin/bash 作为开头,无法使用systemd。

vim /opt/local/consul/consul-startup.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/bin/bash
BASE_DIR=/opt/local/consul

mkdir -p $BASE_DIR/data
mkdir -p $BASE_DIR/log

$BASE_DIR/consul agent \
-node=node-name-1 \
-datacenter=dc-1 \
-client 0.0.0.0 \
-bind=192.168.0.7 \
-data-dir=$BASE_DIR/data \
-log-file=$BASE_DIR/log/ \
-log-level=info \
-ui \
-server \
-bootstrap-expect=1

启动

1
2
chmod +x /opt/local/consul/consul-startup.sh
/opt/local/consul/consul-startup.sh

开机自启

vim /etc/systemd/system/consul.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=Consul
After=network.target

[Service]
# Type=forking 会导致 systemctl start service 阻塞
Type=simple
User=root
Group=root
ExecStart=/opt/local/consul/consul agent -config-file=/opt/local/consul/config/server.json
Restart=on-failure

[Install]
WantedBy=multi-user.target

systemctl 管理

1
2
3
4
systemctl daemon-reload
systemctl start consul
systemctl stop consul
systemctl enable consul

windows启动脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
title %~nx0

set BASE_DIR=X:\deploy\consul_1.20.3_windows_386
call %BASE_DIR%/consul agent ^
-node=node-name-1 ^
-datacenter=dc-1 ^
-client 0.0.0.0 ^
-bind=192.168.0.3 ^
-data-dir=%BASE_DIR%/data ^
-config-dir=%BASE_DIR%/config ^
-log-file=%BASE_DIR%/log/ ^
-log-level=info ^
-ui ^
-bootstrap-expect=1 ^
-server

pause