玩轉CONSUL(1)–WATCH機制探究

1. 前言

consul 常常被用於服務的註冊和發現,本文將帶你對watch作更深刻的探究html

2. consul對外暴露了4種通信接口

2.1 RPC

主要用於內部通信Gossip/日誌分發/選主等node

2.2 HTTP API

服務發現/健康檢查/KV存儲等幾乎全部功能
默認端口爲8500web

2.3 Consul Commands (CLI)

consul命令行工具能夠與consul agent進行鏈接,提供一部分consul的功能。
實時上Consul CLI 默認就是調用的HTTP API來與consul集羣進行通信。
能夠經過配置CONSUL_HTTP_ADDR 修改Consul CLI鏈接的目標地址json

CONSUL_HTTP_ADDR=http://127.0.0.1:8500

詳見參考資料3api

2.4 DNS

僅用於服務查詢服務器

3. 服務註冊&發現

3.1 服務註冊

服務註冊能夠經過 服務註冊接口 /agent/service/register 很容易作到app

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解析,是主動請求的過程。它對線上服務節點的變化,反應是延遲的。curl

3.2.2 Watch方式

見參考資料1
watch採用HTTP長輪訓(long polling)實現的。ide

不一樣的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,以完成對資源的監控。

參考資料

    1. consul-Watches
    2. Blocking Queries
    3. consul_http_addr
    4. DNS-API
相關文章
相關標籤/搜索