使用Consul解决了哪些问题:
- 是否在为不同环境来维护不同项目配置而发愁
- 是否有因为配置的更改,导致代码还要进行修改、发布,因为客流量大了还要规避开高峰期等到半夜来发布
- 微服务架构下,应用的分解,业务系统与服务系统之间的调用管理
以上只是列举的笔者曾经遇到的几点问题,当然问题还不止于这些,下面介绍的Consul可以有效解决这些问题,当然还有一些其它的优点,让我们一起期待下文的Consul的讲解。
Consul的四大核心特性:
-
服务发现:可以方便的实现服务注册,通过DNS或者HTTP应用程序可以很容易的找到他所依赖的服务. -
Key/Value存储:使用Key/Value进行数据存储。 -
多数据中心:Consul支持开箱即用的多数据中心。这意味着用户不需要担心建立额外的抽象层让业务扩展到多个区域 -
健康检查:可以对指定服务进行健康检查例如,Response Status是否为200,避免将流量转发到不健康的服务上。
Consul架构
图片来自官网 Consul Architecture

上图很好的展示了Consul对于多数据中心的支持,另外在两个数据中心之间只有Service层可以相互通信。
Consul是一个分布式高可用的系统,一个发现和配置服务的工具。客户端可以利用它提供的API注册和发现服务,及监控检测功能实现服务的高可用,深入的架构描述具体细节,可以参考官网Consul Architecture,下面开始进入实战操作部分。
安装部署
本处主要以linux来讲解,其他操作平台见官网Download Consul
下载 wget https://releases.hashicorp.com/consul/1.4.0/consul_1.4.0_linux_amd64.zip
解压 unzip consul_1.4.0_linux_amd64.zip 得到目录consul
复制 consul到你的系统的任何一个地方,如果你想使用命令行直接访问,确保复制的目录在你的PATH里 cp consul /usr/local/bin/
验证consul是否安装成功,出现以下窗口则安装成功
[root@iZbp1isjfk2rw8fpnxx8wgZ ~]# consul
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
agent Runs a Consul agent
catalog Interact with the catalog
connect Interact with Consul Connect
debug Records a debugging archive for operators
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
intention Interact with Connect service intentions
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
services Interact with services
snapshot Saves, restores and inspects snapshots of Consul server state
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
复制代码
Consul Agent
执行consul agent -dev,启动开发模式,这个模式会快速启动一个单节点的Consul。注意,这个模式不能数据持久化,因此,不能用于生产环境
启动命令简介:
-
-server:定义agent运行在server模式,每个数据中心的Server建议在3~5个避免失败情况下数据的丢失 -
-client:定义agent运行在client模式 -
-bootstrap-expect:在一个datacenter中期望提供的server节点数目,当该值提供的时候,consul一直等到达到指定sever数目的时候才会引导整个集群 -
-bind:节点的ip地址一般是0.0.0.0或云服务内网地址,用于被集群中的其他节点所访问 -
-node:指定节点在集群中的唯一名称,默认为机器的hostname -
-config-dir:配置文件目录,里面所有以.json结尾的文件都会被加载 - 更多可选参数参考Consul Command-line Options
Start Agent
[root@iZbp1isjfk2rw8fpnxx8wgZ ~]# consul agent -dev
==> Starting Consul agent...
==> Consul agent running!
Version: 'v1.4.0'
Node ID: '9e05f4d6-56c1-e57c-c726-15d9ab1c0dd5'
Node name: 'iZbp1isjfk2rw8fpnxx8wgZ'
Datacenter: 'dc1' (Segment: '<all>')
Server: true (Bootstrap: false)
Client Addr: [127.0.0.1] (HTTP: 8500, HTTPS: -1, gRPC: 8502, 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:
复制代码
看下 consul agent 输出的几个重要信息:
-
Node name:代理的唯一名称,默认是机器的hostname,可以通过-node标志自定义,例如:consul agent -dev -node myNode -
Datacenter:数据中心,Consul支持多个数据中心,为了有效的工作,每个节点必须被配置且上报到数据中心,-datacenter标志用来设置数据中心,对于单一的DC配置,这个代理默认为dc1 -
Server:表示代理是以服务器还是客户端的模式来运行。 -
Client Addr:用于代理的客户端接口地址。 -
Cluster Addr:用于集群中的Consul代理之间通信的地址和端口集,改地址必须可供其它节点访问。
查看集群成员
打开一个新终端执行consul members