12

玩转consul(1)–watch机制探究

 3 years ago
source link: http://vearne.cc/archives/13983
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
版权声明 本站原创文章 由 萌叔 发表
转载请注明 萌叔 | http://vearne.cc

consul 经常被用于服务的注册和发现,本文将带你对watch做更深入的探究

2. consul对外暴露了4种通讯接口

2.1 RPC

主要用于内部通讯Gossip/日志分发/选主等

2.2 HTTP API

服务发现/健康检查/KV存储等几乎所有功能
默认端口为8500

2.3 Consul Commands (CLI)

consul命令行工具可以与consul agent进行连接,提供一部分consul的功能。
实时上Consul CLI 默认就是调用的HTTP API来与consul集群进行通讯。
可以通过配置CONSUL_HTTP_ADDR 修改Consul CLI连接的目标地址

CONSUL_HTTP_ADDR=http://127.0.0.1:8500

详见参考资料3

2.4 DNS

仅用于服务查询

3. 服务注册&发现

3.1 服务注册

服务注册可以通过 服务注册接口 /agent/service/register 很容易做到

3.2 服务发现

3.2.1 DNS方式
$ dig @127.0.0.1 -p 8600 web.service.consul

;; QUESTION SECTION:
;web.service.consul.        IN  A

;; ANSWER SECTION:
web.service.consul. 0   IN  A   127.0.0.1

我们可以通过cosul提供的DNS接口来获取当前的service “web” 对应的可用节点(详细用法见参考资料4)
DNS方式要求使用方主动进行DNS解析,是主动请求的过程。它对线上服务节点的变化,反应是延迟的。

3.2.2 Watch方式

见参考资料1
watch采用HTTP长轮训(long polling)实现的。

不同的watch类型对应着不同HTTP API

key - Watch a specific KV pair
keyprefix - Watch a prefix in the KV store
services - Watch the list of available services
nodes - Watch the list of nodes
service- Watch the instances of a service
checks - Watch the value of health checks
event - Watch for custom user events

下面我们以watch service来举例。
假定我们有一个服务es,位于机房dc1

1) 获取node列表
详解 list-nodes-for-service

Method Path Produces GET /health/service/:service application/json
curl -v -XGET 'http://dev1:8500/v1/health/service/es?dc=dc1&passing=1&tag=search'

字段 类型 说明 passing true/false 节点通过了check(通常表示节点是活的)

我们会收到形如

< HTTP/1.1 200 OK
< Content-Type: application/json
< Vary: Accept-Encoding
< X-Consul-Effective-Consistency: leader
< X-Consul-Index: 923894    // X-Consul-Index 表示被请求资源的当前版本
< X-Consul-Knownleader: true
< X-Consul-Lastcontact: 0
< Date: Thu, 10 Jan 2019 08:38:15 GMT
< Transfer-Encoding: chunked

[{
    "Node": { ...},
    "Service": {
        "ID": "es1",
        "Service": "es",
        "Tags": [
            "es",
            "search"
        ],
        "Address": "192.168.120.103",
        "Meta": {
            "es_version": "6.2.4"
        },
        "Port": 9200,
        "Weights": {
            "Passing": 10,
            "Warning": 1
        },
        "EnableTagOverride": false,
        "ProxyDestination": "",
        "Proxy": {},
        "Connect": {},
        "CreateIndex": 393293,
        "ModifyIndex": 393293
    },
    "Checks": [ ... ]
}]

Endpoints that support blocking queries return an HTTP header named X-Consul-Index. This is a unique identifier representing the current state of the requested resource.

2) 第2 ~ N次请求

curl -v -XGET 'http://dev1:8500/v1/health/service/es?dc=dc1&passing=1&tag=search&wait=5s&index=923894'

字段 类型 说明 备注 wait string consul会尝试等待的时间 “5s”表示5秒,详见参考资料3 index int 上次拿到的版本号

consul会尝试等待被请求资源发生变化,如果在wait指定的时间内
1) 被请求资源发生变化, 请求直接返回新的X-Consul-Index和新的body体
2) 被请求资源未发生变化,则请求会一直阻塞,直到wait指定的时间耗尽,请求最终会返回。只是此时X-Consul-Index不发生变化,body体不变。

长轮训减少了频繁轮训的所造成的不必要的带宽和服务器资源开销,用在服务发现上,即时性也能有所保证,还是很合适的

watch操作重复步骤2,以完成对资源的监控。


如果我的文章对你有帮助,你可以给我打赏以促使我拿出更多的时间和精力来分享我的经验和思考总结。

微信支付码

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK