consul三节点的集群搭建

    xiaoxiao2025-11-14  2

    https://www.consul.io/                           下载安装 consul_0.6.4_darwin_amd64.zip 和 consul_0.6.4_web_ui.zip http://ju.outofmemory.cn/entry/263641  服务发现系统consul--配置 http://ju.outofmemory.cn/entry/189899  三节点的集群搭建

    在公司内网,用了3台mac-mini来实验并记录如下:

    n1(mac): 192.168.51.165  server mode  n2(mac): 192.168.51.65   server mode    with Consul Web UI  n3(mac): 192.168.51.207  client mode n1 ./consul agent -server -bootstrap-expect 2 -data-dir /tmp/consul -node=n1 -bind=192.168.51.165 -dc=dc1  n2 ./consul agent -server -bootstrap-expect 2 -data-dir /tmp/consul -node=n2 -bind=192.168.51.65 -ui-dir ./dist -dc=dc1  n1 ./consul join 192.168.51.65 n2 ./consul leave n1的/tmp/consul/raft/peers.json ["192.168.51.165:8300"] n2的/tmp/consul/raft/peers.json null 我们打开n1和n2的/tmp/consul/raft/peers.json,将其内容统一修改为: ["192.168.51.65:8300","192.168.51.165:8300"] 注意:如果失败,需要在n1和n2上 rm /tmp/consul/raft/peers.json 否则 这个文件的内容为null 会导致rejoin失败 n2 ./consul agent -server -bootstrap-expect 2 -data-dir /tmp/consul -node=n2 -bind=192.168.51.65 -ui-dir ./dist  -dc=dc1 -rejoin n1被选举为Leader ################################# 给cluster添加一个client ################################# n3 ./consul agent  -data-dir /tmp/consul -node=n3 -bind=192.168.51.207  -dc=dc1  n3  作为client可加入Cluster的任一server(n1/n2都可以) ./consul join 192.168.51.65 ################################# 服务注册 ################################# n3 ./consul leave n3 新建配置文件 conf/web3.json {  "service": {  "name": "web3",  "tags": ["master"],  "address": "127.0.0.1",  "port": 10000,  "checks": [  {  "http": "http://localhost:10000/health",  "interval": "10s"  }  ]  }  } n3 ./consul agent -data-dir /tmp/consul -node=n3 -bind=192.168.51.207 -dc=dc1 -config-dir=./conf vi web3.go package main import (  "fmt"  "net/http"  ) func handler(w http.ResponseWriter, r *http.Request) {  fmt.Println("hello Web3! This is n3")  fmt.Fprintf(w, "Hello Web3! This is n3")  } func healthHandler(w http.ResponseWriter, r *http.Request) {  fmt.Println("health check!")  } func main() {  http.HandleFunc("/", handler)  http.HandleFunc("/health", healthHandler)  http.ListenAndServe(":10000", nil)  } go run web3.go n2  跟n2一样新建web3.json 和 web3.go ./consul agent -server -bootstrap-expect 2 -data-dir /tmp/consul -node=n2 -bind=192.168.51.65 -ui-dir ./dist  -dc=dc1 -rejoin -config-dir=./conf go run web3.go n1 dig @127.0.0.1 -p 8600 web3.service.consul SRV ################################# 服务发现 ################################# $go get github.com/miekg/dns $vi servicediscovery.go $go run servicediscovery.go  n2.node.dc1.consul. – 192.168.51.65:10000  n3.node.dc1.consul. – 192.168.51.207:10000
    转载请注明原文地址: https://ju.6miu.com/read-1304192.html
    最新回复(0)