微服务框架使用consul
###consul 是什么
consul 是提供有发现服务,配置,分段功能全特性控制面板的服务网络解决方案。它关键特性有服务发现,健康检查,kv保存,安全服务通信,多数据中心等。就不翻译文档了...
条件
consul 命令包括以下
``` sell
Usage: consul [--version] [--help] []
Available commands are:
agent Runs a Consul agent
catalog Interact with the catalog
connect Interact with Consul Connect
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
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
```
#### 运行 agent
agent : consul核心进程,管理成员信息,注册服务,运行检查服务,响应请求,等等。有两种模式,server 和client,
server 服务节点需要去执行Raft算法,需要更多的资源,而client则不用,把所有的,因此client更轻量,client构成集群的主体。
运行 agent
```shell
consul agent -data-dir=/tmp/consul
```
有几个比较重要的参数
* 节点名 (node name) : 要唯一,默认是本机主机名,可以通过 -node xxx 设置
* 数据中心(datacenter): agent所在的数据中心,consul支持多个数据中心,但是每个节点必须配置自己的数据中心,默认dc1
* 服务(-server): 加上这个参数,就会以ser