Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案,Consul的方案更“一站式” ,内置了服务注册与发现框架
介绍
据说使用起来也较 为简单。Consul使用Go语言编写,因此具有天然可移植性(支持Linux、windows和Mac OS X);安装包仅包含一个可执行文件,方便部署,与Docker等轻量级容器可无缝配合 。 基于 Mozilla Public License 2.0 的协议进行开源. Consul 支持健康检查,并允许 HTTP 和 DNS 协议调用 API 存储键值对. 一致性协议采用 Raft 算法,用来保证服务的高可用. 使用 GOSSIP 协议管理成员和广播消息, 并且支持 ACL 访问控制.
Consul 四大特性
- Service Discovery (服务发现)
- Health Check (健康检查)
- Multi Datacenter (多数据中心)
- Key/Value Storage
Consul Glossary(术语)
Agent
- Agent 是一个守护进程
- 运行在Consul集群的每个成员上
- 有Client 和 Server 两种模式
- 所有Agent都可以被调用DNS或者HTTP API,并负责检查和维护同步
Client
- Client 将所有RPC请求转发至Server
- Client 是相对无状态的
- Client 唯一做的就是参与LAN Gossip Pool
- Client 只消耗少量的资源和少量的网络带宽
Server
- 参与 Raft quorum(一致性判断)
- 响应RPC查询请求
- 维护集群的状态
- 转发查询到Leader 或 远程数据中心
Datacenter
顾名思义其为数据中心, 如何定义数据中心呢? 需要以下三点
- 私有的
- 低延迟
- 高带宽
故: 可以简单的理解为同属一个内网环境, 如北京机房和香港机房就不一定满足以上三个条件
Consensus (一致性)
Consul 使用consensus protocol 来提供CAP(一致性,高可用,分区容错性)
Gossip
一种协议: 用来保证 最终一致性 , 即: 无法保证在某个时刻, 所有节点状态一致, 但可以保证”最终”一致
LAN Gossip
Local Area Network Gossip, 位于同一个局域网或者数据中心的节点
WAN Gossip
Wide Area Network Gossip, 只用于Server之间, 分部在不同的数据中心或广域网
构架图
安装Consul
下载地址:https://www.consul.io/downloads.html
各种平台的都有下载:Mac OS X
、FreeBSD
、Linux
、Solaris
、Windows
我是在mac上使用,所以下了个mac版本的
下过来的zip,解压以后得到一个单独二级制文件
,我把它直接放到了/usr/local/bin
下
这样,在控制台中,我就直接能使用了,可以使用以下命令,来测试是否可运行
$ consul -v
Consul v1.0.3
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)
以调试模式启动Consul
可以用下面的命令启动Consul的开发模式:
$ consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.0.3'
Node ID: 'aef05660-c518-c124-f8bd-e2e8148b0fa2'
Node name: 'zhangxiaodeMacBook-Pro.local'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, DNS: 8600)
Cluster Addr: 127.0.0.1 (LAN: 8301, WAN: 8302)
Encrypt: Gossip: false, TLS-Outgoing: false, TLS-Incoming: false
==> Log data will now stream in as it occurs:
2018/02/03 15:37:32 [DEBUG] Using random ID "aef05660-c518-c124-f8bd-e2e8148b0fa2" as node ID
2018/02/03 15:37:32 [INFO] raft: Initial configuration (index=1): [{Suffrage:Voter ID:aef05660-c518-c124-f8bd-e2e8148b0fa2 Address:127.0.0.1:8300}]
2018/02/03 15:37:32 [INFO] raft: Node at 127.0.0.1:8300 [Follower] entering Follower state (Leader: "")
2018/02/03 15:37:32 [INFO] serf: EventMemberJoin: zhangxiaodeMacBook-Pro.local.dc1 127.0.0.1
2018/02/03 15:37:32 [INFO] serf: EventMemberJoin: zhangxiaodeMacBook-Pro.local 127.0.0.1
2018/02/03 15:37:32 [INFO] consul: Handled member-join event for server "zhangxiaodeMacBook-Pro.local.dc1" in area "wan"
2018/02/03 15:37:32 [INFO] consul: Adding LAN server zhangxiaodeMacBook-Pro.local (Addr: tcp/127.0.0.1:8300) (DC: dc1)
2018/02/03 15:37:32 [INFO] agent: Started DNS server 127.0.0.1:8600 (udp)
2018/02/03 15:37:32 [INFO] agent: Started DNS server 127.0.0.1:8600 (tcp)
2018/02/03 15:37:32 [INFO] agent: Started HTTP server on 127.0.0.1:8500 (tcp)
2018/02/03 15:37:32 [INFO] agent: started state syncer
2018/02/03 15:37:32 [WARN] raft: Heartbeat timeout from "" reached, starting election
2018/02/03 15:37:32 [INFO] raft: Node at 127.0.0.1:8300 [Candidate] entering Candidate state in term 2
2018/02/03 15:37:32 [DEBUG] raft: Votes needed: 1
2018/02/03 15:37:32 [DEBUG] raft: Vote granted from aef05660-c518-c124-f8bd-e2e8148b0fa2 in term 2. Tally: 1
2018/02/03 15:37:32 [INFO] raft: Election won. Tally: 1
2018/02/03 15:37:32 [INFO] raft: Node at 127.0.0.1:8300 [Leader] entering Leader state
2018/02/03 15:37:32 [INFO] consul: cluster leadership acquired
2018/02/03 15:37:32 [INFO] consul: New leader elected: zhangxiaodeMacBook-Pro.local
2018/02/03 15:37:32 [DEBUG] consul: Skipping self join check for "zhangxiaodeMacBook-Pro.local" since the cluster is too small
2018/02/03 15:37:32 [INFO] consul: member 'zhangxiaodeMacBook-Pro.local' joined, marking health alive
2018/02/03 15:37:33 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically
2018/02/03 15:37:33 [INFO] agent: Synced node info
2018/02/03 15:37:33 [DEBUG] agent: Node info in sync
2018/02/03 15:37:33 [DEBUG] Skipping remote check "serfHealth" since it is managed automatically
2018/02/03 15:37:33 [DEBUG] agent: Node info in sync
2018/02/03 15:37:38 [DEBUG] http: Request GET /v1/event/list?wait=5s&index=1 (5.001249189s) from=127.0.0.1:54222
启动一个开发模式的consul非常简单
但是在生产环境,我想并没有这么简单,后面再深入了解下
现在访问下:http://localhost:8500 ,可以看到consul的ui界面
docker-compose 参考
version: "2"
services:
app1:
build: ./python_app/
ports:
- "8080:8080"
app2:
build: ./python_app/
ports:
- "8081:8080"
consul1:
image: "progrium/consul:latest"
container_name: "consul1"
hostname: "consul1"
ports:
- "8400:8400"
- "8500:8500"
- "8600:53"
command: "-server -bootstrap-expect 3 -ui-dir /ui"
consul2:
image: "progrium/consul:latest"
container_name: "consul2"
hostname: "consul2"
expose:
- "8400"
- "8500"
- "8600"
command: "-server -join consul1"
depends_on:
- consul1
consul3:
image: "progrium/consul:latest"
container_name: "consul3"
hostname: "consul3"
expose:
- "8400"
- "8500"
- "8600"
command: "-server -join consul1"
depends_on:
- consul1
参考文章
《Install Consul》
《Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】》
《服务注册发现consul之一:spring cloud consul介绍及安装》
《Introduction to Consul》
《现有系统如何集成Consul服务发现》