一、consul:搭建高可用的consul注册中心

简介

如何搭建一个高可用的consul注册中心环境,以保证consul的正常运行,和高容灾。

下载

consul官方下载地址:https://releases.hashicorp.com/consul
版本选择:这里使用的是最新稳定版本(Stable)0.8.1

get https://releases.hashicorp.com/consul/0.8.1/consul_0.8.1_linux_amd64.zip
unzip consul_0.8.1_linux_amd64.zip
mkdir -p /data/consul/config
mkdir -p /data/consul/data
mkdir -p /data/logs/consul

server1配置

{
  "datacenter": "dc1",
  "node_name": "consul-node1",
  "server": true,
  "bootstrap" : true,
  "data_dir": "/data/consul/data",
  "addresses": {"http": "0.0.0.0"}
}
consul agent -config-file=/data/consul/config/server.json > /data/logs/consul/consul.log 2>&1 &
# 等待server2与server3启动后,去除server1的bootstrap,然后leave,最后重启server1
consul leave
{
  "datacenter": "dc1",
  "node_name": "server1",
  "server": true,
  "data_dir": "/data/consul/data",
  "addresses": {"http": "0.0.0.0"},
  "retry_join" : ["***.***.***.***:8301", "**********:8302"]
}
consul agent -config-file=/data/consul/config/server.json > /data/logs/consul/consul.log 2>&1 &
 
extend config
{
  "reconnect_timeout": "72h",
  "retry_interval": "30s",
  "session_ttl_min": "10s"
}

server2配置

{
  "datacenter": "dc1",
  "node_name": "consul-node3",
  "server": true,
  "data_dir": "/data/consul/data",
  "addresses": {"http": "0.0.0.0"},
  "retry_join" : ["***.***.***.***8302", "***********:8301"]
}
consul agent -config-file=/data/consul/config/server.json > /data/logs/consul/consul.log 2>&1 &

server3配置

{
  "datacenter": "dc1",
  "node_name": "consul-node3",
  "server": true,
  "data_dir": "/data/consul/data",
  "addresses": {"http": "0.0.0.0"},
  "retry_join" : ["***.***.***.***8303", "**********:8301"]
}
consul agent -config-file=/data/consul/config/server.json > /data/logs/consul/consul.log 2>&1 &

client1配置


{
  "datacenter": "dc1",
  "node_name": "consul-client01",
  "server": false,
  "data_dir": "/data/consul/data",
  "addresses": {"http": "0.0.0.0"},
  "retry_join" : ["***.***.***.***:8301", "***.***.***.***:8301", "***.***.***.***:8301"]
}
consul agent -config-file=/data/consul/config/client.json > /data/logs/consul/consul.log 2>&1 &

检查配置

consul members

注册服务

curl -XPUT 'http://localhost:8500/v1/agent/service/register' -d '
{
  "ID": "test-id",
  "Name": "test-name",
  "Tags": ["dev", "beta"],
  "Address": "***********",
  "Port": 8080,
  "Check": {
    "HTTP": "http://***.***.***.***:8080/health",
    "Interval": "10s",
    "Timeout": "10s",
    "DeregisterCriticalServiceAfter": "5m"
  }
}

发现服务

curl -XGET ‘http://localhost:8500/v1/catalog/services?wait=2s’

检测服务健康

curl -XGET ‘http://localhost:8500/v1/health/service/test-name’

注销服务

curl -XDELETE ‘http://localhost:8500/v1/agent/service/deregister/test-id’

consul节点切换步骤

1、启动新的consul client和consul server,加入旧的consul cluster
2、配置新的vip给新的consul client
3、修改dns解析,使client.consul.prd.adou.com指向新的vip
4、重启提供服务的应用
5、旧的consul client leave
6、旧的consul server leave

你可能感兴趣的:(middleware,consul,注册中心,高可用,springcloud,微服务)