微服务组件:服务注册与发现consul

consul集群安装

1. consul介绍

       consul是一个服务发现与配置工具,在微服务架构中起着服务注册与发现、分布式配置管理的作用。

       ① 服务注册与发现;

       ② 健康检查;

       ③ 分布式配置,key/value存储;

       ④ 多数据中心;

       ⑤ 与docker无缝集成;

       ⑥ 支持Ribbon,实现客户端负载均衡;

       ⑦ 支持Zuul,实现动态路由和过滤;

2. consul架构图


 1)server的状态存在candidate(竞选中)、leader(竞选获胜)、follower(竞选未获胜)三种状态;

 2)consul集群间使用gossip协议通信;

 3)同一个数据中心的所有节点都必须加入gossip协议;

 4)client不保存数据,将接收到的请求转发给响应的server;

 5)leader收到client的请求,会给client发送RPC响应,同时存储client的状态信息;

 6)follower收到client的请求,会转发leader;

 7)client会定时向server发送RPC请求,汇报自己的运行状态;

 8)每个server都是Raft节点集群的一部分,他们一起工作并选出一个leader; 

3. consul集群搭建

3.1 下载

https://www.consul.io/downloads.html

unzip consul_0.7.1_darwin_amd64.zip
sudo scp consul /usr/local/bin/
  

3.2 集群机器环境

准备3台服务器,作为consul集群,并检查机器8301,8300端口状态

172.20.10.2,consul-server-mk02

172.20.10.8,consul-server-mk08

172.20.10.9,consul-server-mk09

8300:client向server的RPC通信端口,不打开无法同步

8301:节点间的通信端口,否则无法加入;

3.3 启动consul-server-mk08服务



 

#!/bin/bash

nohup consul agent -ui -config-dir=/home/hyy044101331/java_tools/consul/bootstrap   1>  /home/hyy044101331/java_tools/consul/consul.log 2>&1 &

 3.4 启动consul-server-mk02服务

172.20.10.2

 新增consul配置项

{
 "bootstrap":false,
 "server":true,
 "datacenter":"dc1",
 "client_addr": "0.0.0.0",
 "bind_addr": "172.20.10.2",
 "data_dir":"/var/consul",
 "bootstrap_expect": 3, 
 "encrypt":"Cd9sOxpE5XD9RfHOt3YmEQ==",
 "log_level":"INFO",
 "node_name": "consul-server-mk02",
 "enable_syslog":true,
 "start_join":["172.20.10.2","172.20.10.9"]
}
 #!/bin/bash
  
 consul agent -join 172.20.10.8 -config-dir /Users/hyy044101331/java_tools/consul/server

 3.5 启动consul-server-mk09服务

172.20.10.9

{
 "bootstrap":false,
 "server":true,
 "datacenter":"dc1",
 "client_addr": "0.0.0.0",
 "bind_addr": "172.20.10.9",
 "data_dir":"/var/consul",
 "bootstrap_expect": 3, 
 "encrypt":"Cd9sOxpE5XD9RfHOt3YmEQ==",
 "log_level":"INFO",
 "node_name": "consul-server-mk09",
 "enable_syslog":true,
 "start_join":["172.20.10.2","172.20.10.9"]
}
#!/bin/bash

consul agent -join 172.20.10.8 -config-dir /home/hyy044101331/java_tools/consul/server

 3.6 查看consul集群状态

[root@localhost hyy044101331]# consul members
Node                Address           Status  Type    Build  Protocol  DC
consul-server-mk02  172.20.10.2:8301  alive  server  0.8.1  2         dc1
consul-server-mk08  172.20.10.8:8301  alive   server  0.8.1  2         dc1
consul-server-mk09  172.20.10.9:8301  alive   server  0.8.1  2         dc1
[root@localhost hyy044101331]# consul operator raft -list-peers
Node                ID                Address           State     Voter
consul-server-mk08  172.20.10.8:8300  172.20.10.8:8300  leader    true
consul-server-mk09  172.20.10.9:8300  172.20.10.9:8300  follower  true
consul-server-mk02  172.20.10.2:8300  172.20.10.2:8300  follower  true



  

 

猜你喜欢

转载自hyy044101331.iteye.com/blog/2383497