1、二进制文件安装

下载地址列表:
https://developer.hashicorp.com/consul/downloads

# linux下
wget https://releases.hashicorp.com/consul/1.15.1/consul_1.15.1_darwin_amd64.zip

# 创建文件夹
mkdir -p /usr/local/consul

# 解压到指定目录
unzip consul_1.15.1_darwin_amd64.zip /usr/local/consul/

cd /usr/local/consul/

# 开发使用,使用dev模式数据不会持久化
consul agent -dev -client 0.0.0.0

访问 127.0.0.1:8500 可以进入 consul 看板

2、Windows系统consul配置中心持久化

编写 consul_start.bat

@echo.service startup......
@echo off
@sc create Consul binpath= "F:\Program Files\consul\consul.exe agent -server -ui -bind=127.0.0.1 -client=0.0.0.0 -bootstrap-expect  1  -data-dir F:\Program Files\consul\data"
@net start Consul
@sc config Consul start= AUTO
@echo.Consul starting success!
@pause

修改binpath-data-dir目录为自己consul和脚本文件所在目录

修改-client为本地IP或所有地址均可访问(此0.0.0.0就是不限制)

以管理员身份执行脚本,执行成功以后就可以在Windows服务中查看到Consul服务了,并且此服务为开机自启动服务

Consul的key/value数据持久化
dev模式下不会持久化数据

cmd启动:

consul agent -config-file=config.json

# 配置名称
{
    "bind_addr": "127.0.0.1",
    "server": true,
    "bootstrap_expect": 1,
    "client_addr": "0.0.0.0",
    "data_dir": "D:\\tool\\consul_1.4.4_windows_amd64\\data",
    "log_level": "info",
    "ui": true,
    "rejoin_after_leave": true
}

Consul注册向windows注册服务

  • 1、向path里添加D:\tool\consul_1.4.4_windows_amd64

  • 2、以管理员身份启动命令提示符,执行

  • 3、sc.exe create “Consul” binPath=”consul agent -config-file=D:\tool\consul_1.4.4_windows_amd64\config.json”

  • 4、“运行“输入services.msc进入系统服务,将Consul服务设置为自动启动

windows删除Consul服务

通过管理员权限运行CMD,输入如下命令

net stop consul

sc delete consul

3、linux中持久化处理

  • 1、创建好data和config存储的地方

    mkdir /usr/local/consul/mydata
    mkdir /usr/local/consul/myconfig
  • 2、启动时候,指定存储的目录

    consul agent -server \
    -bootstrap-expect 1 \
    -ui -node=consul-dev  \
    -data-dir=/usr/local/consul/mydata/ \
    -config-dir=/usr/local/consul/myconfig/ \
    -client=10.10.1.72  \
    -bind=127.0.0.1

4、centos7中设置为服务

先创建服务配置文件

vi /usr/local/consul/config.json

{
    "bind_addr": "127.0.0.1",
    "server": true,
    "bootstrap_expect": 1,
    "client_addr": "0.0.0.0",
    "data_dir": "/usr/local/consul/mydata/",
    "config_dir": "/usr/local/consul/myconfig/",
    "log_level": "info",
    "ui": true,
    "node":"consul-dev",
    "rejoin_after_leave": true
}

再创建服务文件

vi /etc/systemd/system/consul.service

[Unit]
Description=Consul Server service
After=network.target

[Service]
Type=simple
WorkingDirectory=/usr/local/consul/
ExecStart=/usr/local/consul/consul agent -config-file=config.json
Restart=always

PrivateDevices=yes
PrivateTmp=yes
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target

启动/关闭/重启服务

# 开启
systemctl start consul.service

# 停止
systemctl stop consul.service

# 重启
systemctl restart consul.service

# 设置开机启动
systemctl enable consul.service
作者:admin  创建时间:2022-03-26 10:26
最后编辑:joker.liu  更新时间:2023-05-16 17:01