一:consul介绍

consul用于提供服务发现和服务配置的工具。有以下特性:
1. 服务发现
consul的客户端提供一个服务,比如api或者mysql,另外一个客户端就可以去发现指定服务的服务提供者。通过DNS或者HTTP应用程序可以容易找到所依赖的服务
2. 健康检查
consul 可以提供健康检查服务(比如:webserver是否返回了200 ok状态码)或者使用本地节点(比如:内存使用大于90%)。这个信息可以监视集群的健康。可以用来避免将流量发送到不健康的主机
3.key/value 存储
应用程序可以使用consul的层级的key/value存储,比如动态配置,协调服务。直接可以用HTTP API来操作
4.多数据中心
consul支持开箱即用的多数据中心。

二:consul安装配置

consul的安装非常容易,直接到 https://www.consul.io/downloads.html 下载你所在平台的安装包,可以直接下载二进制包, 然后unzip解压,会得到一个 consul 的文件,然后把它 cp 到/usr/local/bin 目录下
验证安装:在终端下直接输入 consul ,输出下面的内容说明安装成功

Usage: consul [--version] [--help] <command> [<args>]

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

三:consul启动

完成consul的安装后,必须运行agent. agent可以运行为 server 或者 client模式。还有个开发模式dev。 每个数据中心至少必须拥有一台server。建议在一个集群中有3或者5个server。部署单一server,在出现失败时,会不可避免的出现数据丢失。

以dev模式启动consul
还有一个特殊的运行模式 -dev的模式,命令如下
./consul agent -dev
说明:
-dev 表示开发模式运行,默认客户端地址是在127.0.0.1 上
-dev(该节点的启动不能用于生产环境,因为该模式下不会持久化任何状态),该启动模式仅仅是为了快速便捷的启动单节点consul

以server模式启动consul
consul agent -server -bind=192.168.0.109 -data-dir=/etc/consul.d
其实没有 -server,默认就是以client模式启动的

以client模式启动consul
consul agent -client=0.0.0.0 -bind=192.168.0.109 -data-dir=/etc/consul.d

四:consul的常用命令

https://www.consul.io/docs/commands/index.html  官方命令大全地址

consul常用命令+常用选项
agent
作用:运行一个consul agent

join
作用:将agent加入到consul cluster

members
作用:列出consul cluster集群中的members

常用选项option:
-data-dir
作用:指定agent储存状态的数据目录
这是所有agent都必须的
对于server尤其重要,因为他们必须持久化集群的状态

-config-dir
作用:指定service的配置文件和检查定义所在的位置
通常会指定为"某一个路径/consul.d"(通常情况下,.d表示一系列配置文件存放的目录)

-config-file
作用:指定一个要装载的配置文件
该选项可以配置多次,进而配置多个配置文件(后边的会合并前边的,相同的值覆盖)

-dev
作用:创建一个开发环境下的server节点
该参数配置下,不会有任何持久化操作,即不会有任何数据写入到磁盘
这种模式不能用于生产环境(因为第二条)

-bootstrap-expect
作用:该命令通知consul server我们现在准备加入的server节点个数,该参数是为了延迟日志复制的启动直到我们指定数量的server节点成功的加入后启动。

-node
作用:指定节点在集群中的名称
该名称在集群中必须是唯一的(默认采用机器的host)
推荐:直接采用机器的IP

-bind
作用:指明节点的IP地址

-server
作用:指定节点为server
每个数据中心(DC)的server数推荐为3或5(理想的是,最多不要超过5)
所有的server都采用raft一致性算法来确保事务的一致性和线性化,事务修改了集群的状态,且集群的状态保存在每一台server上保证可用性
server也是与其他DC交互的门面(gateway)

-client
作用:指定节点为client
若不指定为-server,其实就是-client

-join
作用:将节点加入到集群

-domain
-dc
作用:指定机器加入到哪一个dc中

查看consul的命令 ./consul

# ./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
tls Builtin helpers for creating CAs and certificates
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul

查看某一个子命令的使用方法

比如查看 catalog 的使用方法,使用下面的命令:

./consul catalog help

Usage: consul catalog <subcommand> [options] [args]

  This command has subcommands for interacting with Consul's catalog. The
catalog should not be confused with the agent, although the APIs and
responses may be similar. Here are some simple examples, and more detailed examples are available
in the subcommands or the documentation. List all datacenters: $ consul catalog datacenters List all nodes: $ consul catalog nodes List all services: $ consul catalog services For more examples, ask for subcommand help or view the documentation. Subcommands:
datacenters Lists all known datacenters
nodes Lists all nodes in the given datacenter
services Lists all registered services in a datacenter

查看组成consul服务的node各种命令

a: 命令行查询

[root@localhost]# ./consul catalog nodes
Node ID Address DC
localhost.localdomain 901b6ebb 192.168.0.109 dc1

b: http接口查询,用 curl 来查询

[root@localhost ]# curl localhost:/v1/catalog/nodes | python -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
--:--:-- --:--:-- --:--:--
[
{
"Address": "192.168.0.109",
"CreateIndex": ,
"Datacenter": "dc1",
"ID": "901b6ebb-8a9b-1930-62e2-65a264ff0fd1",
"Meta": {
"consul-network-segment": ""
},
"ModifyIndex": ,
"Node": "localhost.localdomain",
"TaggedAddresses": {
"lan": "192.168.0.109",
"wan": "192.168.0.109"
}
}
]

c: 通过dns查询成员node的地址,默认后缀是 node.consul

root@localhost]# dig @127.0.0.1 -p  192.168.0.109.node.cosul

; <<>> DiG 9.9.-RedHat-9.9.-.el7 <<>> @127.0.0.1 -p  192.168.0.109.node.cosul
; ( server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: SERVFAIL, id:
;; flags: qr rd; QUERY: , ANSWER: , AUTHORITY: , ADDITIONAL:
;; WARNING: recursion requested but not available ;; QUESTION SECTION:
;192.168.0.109.node.cosul. IN A ;; Query time: msec
;; SERVER: 127.0.0.1#(127.0.0.1)
;; MSG SIZE rcvd:

五:添加,查询服务:(服务注册,服务发现)

下面我们以dev的模式启动consul

5.1 添加一个服务(也叫 服务注册)

1. 新建一个文件夹 mkdir /etc/consul.d
2. 写入一个json格式信息到 /etc/consul.d/web.json 文件中
echo '{"service": {"name": "web", "tags": ["rails"], "port": 80}}' | sudo tee /etc/consul.d/web.json
3. 重新启动 cosul
consul agent -dev -config-dir=/etc/consul.d

或者通过 api 来注册服务, api是:agent/service:

1. 先编写一个json文件,把它命名为web2.json

vi web2.json

{
"Name": "web2",
"Tags": [
"rails"
],
"Address": "",
"Port": 81,
"ServiceEnableTagOverride": false
}

2. 然后用curl命令把这个json文件通过http接口写入到consul里

curl --request PUT --data @web2.json http://127.0.0.1:8500/v1/agent/service/register

查询刚才注册的服务, 看看服务是否注册成功

[root@localhost]# ./consul catalog services
consul
web2

5.2 查询定义服务 (也叫 服务发现)
第一:通过DNS来查询

我们可以用DNS API 来查询服务,服务名默认为: NAME.service.consul

eg1: 查询名字为 web 的服务
dig @127.0.0.1 -p 8600 web.service.consul

; OPT PSEUDOSECTION:
; EDNS: version: , flags:; udp:
;; QUESTION SECTION:
;web.service.consul. IN A ;; ANSWER SECTION:
web.service.consul. IN A 127.0.0.1

eg2: 查询名字为 web2 的服务

查询类型要指定为为srv,才能看到服务端口.  我们来查询web2服务

[root@localhost]# dig @127.0.0.1 -p  web2.service.consul srv

; <<>> DiG 9.9.-RedHat-9.9.-.el7 <<>> @127.0.0.1 -p  web2.service.consul srv
; ( server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id:
;; flags: qr aa rd; QUERY: , ANSWER: , AUTHORITY: , ADDITIONAL:
;; WARNING: recursion requested but not available ;; OPT PSEUDOSECTION:
; EDNS: version: , flags:; udp:
;; QUESTION SECTION:
;web2.service.consul. IN SRV ;; ANSWER SECTION:
web2.service.consul. IN SRV localhost.localdomain.node.dc1.consul. ;; ADDITIONAL SECTION:
localhost.localdomain.node.dc1.consul. IN A 192.168.0.109
localhost.localdomain.node.dc1.consul. IN TXT "consul-network-segment=" ;; Query time: msec
;; SERVER: 127.0.0.1#(127.0.0.1)
;; MSG SIZE rcvd:

第二:通过http接口查询

也可以用HTTP API来查询服务

eg1: 查询 web 服务
curl http://localhost:8500/v1/catalog/service/web | python -m json.tool

[
{
"ID": "5901c710-c6c4-f8f8-a9eb-77e76e39f034",
"Node": "localhost.localdomain",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceKind": "",
"ServiceID": "web",
"ServiceName": "web",
"ServiceTags": [
"rails"
],
"ServiceAddress": "",
"ServiceMeta": { },
"ServicePort": 80,
"ServiceEnableTagOverride": false,
"ServiceProxyDestination": "",
"ServiceConnect": {
"Native": false,
"Proxy": null
},
"CreateIndex": 10,
"ModifyIndex": 10
}
]

eg2:查询web2服务

curl 命令后面的  python -m json.tool 是格式化返回的json,不然返回就是一串字符串,不便于阅读

[root@localhost]# curl http://127.0.0.1:8500/v1/catalog/service/web2 | python -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
394k --:--:-- --:--:-- --:--:-- 529k
[
{
"Address": "192.168.0.109",
"CreateIndex": ,
"Datacenter": "dc1",
"ID": "901b6ebb-8a9b-1930-62e2-65a264ff0fd1",
"ModifyIndex": ,
"Node": "localhost.localdomain",
"NodeMeta": {
"consul-network-segment": ""
},
"ServiceAddress": "",
"ServiceConnect": {},
"ServiceEnableTagOverride": false,
"ServiceID": "web2",
"ServiceKind": "",
"ServiceMeta": {},
"ServiceName": "web2",
"ServicePort": ,
"ServiceProxy": {},
"ServiceProxyDestination": "",
"ServiceTags": [
"rails"
],
"ServiceWeights": {
"Passing": ,
"Warning":
},
"TaggedAddresses": {
"lan": "192.168.0.109",
"wan": "192.168.0.109"
}
}
]

5.3 查询服务健康状况
curl http://localhost:8500/v1/health/service/web?passing

[
{
"Node": {
"ID": "5901c710-c6c4-f8f8-a9eb-77e76e39f034",
"Node": "localhost.localdomain",
"Address": "127.0.0.1",
"Datacenter": "dc1",
"TaggedAddresses": {
"lan": "127.0.0.1",
"wan": "127.0.0.1"
},
"Meta": {
"consul-network-segment": ""
},
"CreateIndex": 9,
"ModifyIndex": 10
},
"Service": {
"ID": "web",
"Service": "web",
"Tags": [
"rails"
],
"Address": "",
"Meta": null,
"Port": 80,
"EnableTagOverride": false,
"ProxyDestination": "",
"Connect": {
"Native": false,
"Proxy": null
},
"CreateIndex": 10,
"ModifyIndex": 10
},
"Checks": [
{
"Node": "localhost.localdomain",
"CheckID": "serfHealth",
"Name": "Serf Health Status",
"Status": "passing",
"Notes": "",
"Output": "Agent alive and reachable",
"ServiceID": "",
"ServiceName": "",
"ServiceTags": [ ],
"Definition": { },
"CreateIndex": 9,
"ModifyIndex": 9
}
]
}
]

5.4 查询agent上所有的服务

[root@localhost]# curl http://127.0.0.1:8500/v1/agent/services | python -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
171k --:--:-- --:--:-- --:--:-- 291k
{
"web": {
"Address": "",
"EnableTagOverride": false,
"ID": "web",
"Meta": {},
"Port": ,
"Service": "web",
"Tags": [
"rails"
],
"Weights": {
"Passing": ,
"Warning":
}
},
"web2": {
"Address": "",
"EnableTagOverride": false,
"ID": "web2",
"Meta": {},
"Port": ,
"Service": "web2",
"Tags": [
"rails"
],
"Weights": {
"Passing": ,
"Warning":
}
}
}

六: 删除服务

删除名字为 web 的服务

curl --request PUT  http://127.0.0.1:8500/v1/agent/service/deregister/web

七: kv操作

官方地址: https://www.consul.io/api/kv.html

1. 写入一个名为 “key1” 的key, 值为 hello

命令:

curl -X PUT --data "hello" http://127.0.0.1:8500/v1/kv/key1

2: 查询 key1:

命令:

# curl http://127.0.0.1:8500/v1/kv/key1 |python -m json.tool
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
--:--:-- --:--:-- --:--:--
[
{
"CreateIndex": ,
"Flags": ,
"Key": "key1",
"LockIndex": ,
"ModifyIndex": ,
"Value": "aGVsbG8=" //base64编码
}
]

3: 查询指定路径下的所有key:

命令:

# curl 127.0.0.1:/v1/kv/key1?keys
["key1"]

4:删除key

curl -X DELETE http://127.0.0.1:8500/v1/kv/key1

八:查看consul中的每一个consul的节点信息

# consul members

Node Address Status Type Build Protocol DC
localhost.localdomain 127.0.0.1: alive server 0.7. dc1

说明:
Address:节点地址
Status:alive表示节点健康
Type:server运行状态是server状态
DC:dc1表示该节点属于DataCenter1
members命令的输出是基于gossip协议的,并且是最终一致的(也就是说,某一个时刻你去运用该命令查到的consul节点的状态信息可能是有误的)

参考:
https://www.consul.io/intro/getting-started/agent.html
https://www.consul.io/docs/index.html
https://book-consul-guide.vnzmi.com/
http://www.liangxiansen.cn/2017/04/06/consul/

consul配置和使用的更多相关文章

  1. consul 配置

    Eureka 2.0 开源工作宣告停止,对于注册中心来说 Consul 是个更好的选择. 在本场 Chat 中你可以学到的: 了解和搭建 Consul 服务:Spring Cloud Consul 服 ...

  2. python 读取consul配置

    自动化通过rcp client调用远端服务接口时,都需要将远端测试服务ip.端口记录在配置文件. 但由于,服务发布或重启会导致ip.端口变动. 以下将通过python-consul 自动去读取cons ...

  3. Spring Boot 配置 - Consul 配置中心

    ▶ Spring Boot 依赖与配置 Maven 依赖 <dependencyManagement> <dependencies> <dependency> &l ...

  4. Python微服务实践-集成Consul配置中心

    A litmus test for whether an app has all config correctly factored out of the code is whether the co ...

  5. Spring Boot实战系列(7)集成Consul配置中心

    本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要 ...

  6. consul配置

    参考:https://blog.csdn.net/achenyuan/article/details/80389410 gcp: consul安装目录:/usr/local/bin/consul co ...

  7. Spring Cloud 系列之 Consul 配置中心

    前面我们已经学习过 Spring Cloud Config 了: Spring Cloud 系列之 Config 配置中心(一) Spring Cloud 系列之 Config 配置中心(二) Spr ...

  8. consul配置参数大全、详解、总结

    命令行选项 以下选项全部在命令行中指定. -advertise - 通告地址用于更改我们通告给集群中其他节点的地址.默认情况下,-bind地址是通告的.但是,在某些情况下,可能存在无法绑定的可路由地址 ...

  9. .Net Core with 微服务 - Consul 配置中心

    上一次我们介绍了Elastic APM组件.这一次我们继续介绍微服务相关组件配置中心的使用方法.本来打算介绍下携程开源的重型配置中心框架 apollo 但是体系实在是太过于庞大,还是让我爱不起来.因为 ...

随机推荐

  1. 实用的几个JS新特性(es 2016)

    在Chrome 55下测试,可用. 1.箭头函数(arrow function) 以前写的匿名函数是这样的 function(){}, 现在可以简单写成这样()=>{} 如果直接return,没 ...

  2. 【python练习题】程序15

    #题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. n = input('请输入成绩 :') n = int(n) if ...

  3. BZOJ2275[Coci2010]HRPA——斐波那契博弈

    题目描述 N个石子,A和B轮流取,A先.每个人每次最少取一个,最多不超过上一个人的个数的2倍.取到最后一个石子的人胜出,如果A要有必胜策略,第一次他至少要取多少个. 输入 第一行给出数字N,N< ...

  4. P1601 A+B Problem(高精)

    原题链接 https://www.luogu.org/problemnew/show/P1601 这个题提示的很清楚,并非简单的A+B,单纯的long  long型也不行(不要被样例所迷惑).因为lo ...

  5. AHOI2013 差异 【后缀数组】

    题目分析: 求出height以后很明显跨越最小height的一定贡献是最小height,所以对于区间找出最小height再将区间对半分. 代码: #include<bits/stdc++.h&g ...

  6. 【BZOJ3814】【清华集训2014】简单回路 状压DP

    题目描述 给你一个\(n\times m\)的网格图和\(k\)个障碍,有\(q\)个询问,每次问你有多少个不同的不经过任何一个障碍点且经过\((x,y)\)与\((x+1,y)\)之间的简单回路 \ ...

  7. css标准文档流

    css标准文档流 所谓的标准文档流指的是网页当中的一个渲染顺序,就如同人类读书一样,从上向下,从左向右.网页的渲染顺序也是如此.而我们使用的标签默认都是存在于标准文档流当中. 标准文档流当中的特性 空 ...

  8. PyCharmMarkdown插件的方法

    arkdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 从github下载的代码一般都会带有README.md文件,该文件是一个Markdo ...

  9. python中无法被转化为set的list[list组成的list]

    arr = [[a],[b]] set(arr) output: Traceback (most recent call last): File "<stdin>", ...

  10. 万物互联之~RPC专栏

    3.RPC引入 上篇回顾:万物互联之~深入篇 Code:https://github.com/lotapp/BaseCode/tree/master/python/6.net/6.rpc/ 其他专栏最 ...