【Consul】Consul实践指导-Watch机制

x33g5p2x  于2021-12-20 转载在 其他  
字(5.3k)|赞(0)|评价(0)|浏览(409)

      Watches是查看指定数据信息的一种方法,比如查看nodes列表、键值对、健康检查。当监控到更新时,可以调用外部处理程序——可以自定义。比如,发现健康状态发生变化可以通知外部系统健康异常。

      Watches在调用http api接口使用阻塞队列。Agent会自动调用合适的API接口俩监控数据的变化。

**        Watches可以作为Agent配置的一部分。在Agent初始化时就运行,并且支持重新载入配置——运行时新添加或删除配置。**

      Alternatively, the watch command enables a watch to be started outsideof the agent. This can be used by an operator to inspect data in Consul or toeasily pipe data into processes without being tied to the agent lifecycle.

      在任意情况下,watches的type都必须指定。Watch支持的每一个type需要的不同的参数,一些是必须的一些事非必须的。这些都是通过JSON来设置的。

1.1.1  Handle

      Watch配置可以指定监控的数据。一旦数据发生变化,可以运行指定的处理程序——可以是任意可执行的程序。

      处理程序可以从标准输入中读取输入,也可以读取json数据。数据格式依赖于watch类型。Watch类型与Json格式是想对象的。因为watch是直接调用HTTP API,因此输入数据要格式化。

Additionally, the CONSUL_INDEX environment variable will beset. This maps to theX-Consul-Index value in responses from the HTTP API.

1.1.2  GlobalParameters

      除了每一种类型支持的参数外,还有一些全局参数:
datacenter – 数据中心名字

token – ALC token

handler – 监控到数据变化后的调用程序。

1.1.3  Watches类型

      支持如下类型:

  1. Key – 监视指定K/V键值对
  2. Keyprefix – Watch a prefix in the KV store
  3. Services – 监视服务列表
  4. nodes – 监控节点列表
  5. service – 监视服务实例
  6. checks- 监视健康检查的值
  7. event – 监视用户事件
1.1.3.1     类型key

      Key watch类型通常用来监视指定键值对的变化。要求提供key参数

实例

{
  "type": "key",
  "key": "foo/bar/baz",
  "handler": "/usr/bin/my-key-handler.sh"
}

      使用命令行:

$ consul watch -type key -key foo/bar/baz/usr/bin/my-key-handler.sh

输出信息:

{
  "Key": "foo/bar/baz",
  "CreateIndex": 1793,
  "ModifyIndex": 1793,
  "LockIndex": 0,
  "Flags": 0,
  "Value": "aGV5",
  "Session": ""
}
1.1.3.2     类型keyprefix

      Keyprefix类型是用来监视KV存储中keys的前缀。必须提供prefix参数。

      监视会返回匹配prefix的所有键值对。

示例:

{
  "type": "keyprefix",
  "prefix": "foo/",
  "handler": "/usr/bin/my-prefix-handler.sh"
}

命令行:

``

$ consul watch -typekeyprefix -prefix foo/ /usr/bin/my-prefix-handler.sh

输出结果:

[
  {
    "Key": "foo/bar",
    "CreateIndex": 1796,
    "ModifyIndex": 1796,
    "LockIndex": 0,
    "Flags": 0,
    "Value": "TU9BUg==",
    "Session": ""
  },
  {
    "Key": "foo/baz",
    "CreateIndex": 1795,
    "ModifyIndex": 1795,
    "LockIndex": 0,
    "Flags": 0,
    "Value": "YXNkZg==",
    "Session": ""
  },
  {
    "Key": "foo/test",
    "CreateIndex": 1793,
    "ModifyIndex": 1793,
    "LockIndex": 0,
    "Flags": 0,
    "Value": "aGV5",
    "Session": ""
  }
]
1.1.3.3     类型services

      监视一系列有效的service,无参数

      内部接口/v1/catalog/services 

命令行输出信息如下:

{
  "consul": [],
  "redis": [],
  "web": []
}
1.1.3.4     类型nodes

      监视一系列有效的节点,无参数。

      内部API:/v1/catalog/nodes

输出信息:

[
  {
    "Node": "nyc1-consul-1",
    "Address": "192.241.159.115"
  },
  {
    "Node": "nyc1-consul-2",
    "Address": "192.241.158.205"
  },
  {
    "Node": "nyc1-consul-3",
    "Address": "198.199.77.133"
  },
  {
    "Node": "nyc1-worker-1",
    "Address": "162.243.162.228"
  },
  {
    "Node": "nyc1-worker-2",
    "Address": "162.243.162.226"
  },
  {
    "Node": "nyc1-worker-3",
    "Address": "162.243.162.229"
  }
]
1.1.3.5     类型service

      监控指定的单个service。必须提供参数service。tag和passingonly参数可选。

      内部接口:/v1/health/service 

配置示例:

{
  "type": "service",
  "service": "redis",
  "handler": "/usr/bin/my-service-handler.sh"
}

命令行:

$ consul watch -typeservice -service redis /usr/bin/my-service-handler.sh

输出信息:

[
  {
    "Node": {
      "Node": "foobar",
      "Address": "10.1.10.12"
    },
    "Service": {
      "ID": "redis",
      "Service": "redis",
      "Tags": null,
      "Port": 8000
    },
    "Checks": [
      {
        "Node": "foobar",
        "CheckID": "service:redis",
        "Name": "Service'redis' check",
        "Status": "passing",
        "Notes": "",
        "Output": "",
        "ServiceID": "redis",
        "ServiceName": "redis"
      },
      {
        "Node": "foobar",
        "CheckID": "serfHealth",
        "Name": "SerfHealth Status",
        "Status": "passing",
        "Notes": "",
        "Output": "",
        "ServiceID": "",
        "ServiceName": ""
      }
    ]
  }
]
1.1.3.6     类型checks

      监视指定service的checks或指定状态的checks。

      内部接口/v1/health/state//v1/health/checks/

[
  {
    "Node": "foobar",
    "CheckID": "service:redis",
    "Name": "Service'redis' check",
    "Status": "passing",
    "Notes": "",
    "Output": "",
    "ServiceID": "redis",
    "ServiceName": "redis"
  }
]
1.1.3.7     类型event

      监视用户事件。

      内部接口:v1/event/list

配置文件示例:

{
  "type": "event",
  "name": "web-deploy",
  "handler": "/usr/bin/my-deploy-handler.sh"
}

命令行参数:

$ consul watch -typeevent -name web-deploy /usr/bin/my-deploy-handler.sh

输出

[
  {
    "ID": "f07f3fcc-4b7d-3a7c-6d1e-cf414039fcee",
    "Name": "web-deploy",
    "Payload": "MTYwOTAzMA==",
    "NodeFilter": "",
    "ServiceFilter": "",
    "TagFilter": "",
    "Version": 1,
    "LTime": 18
  },
  ...
]

 

相关文章