consul三节点的集群搭建

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值