Consul Watch:如何在更改节点地址时运行脚本

时间:2015-12-09 10:39:35

标签: curl consul

我已注册以下领事服务。

curl -s -X GET http://<CONSUL_URL>/v1/catalog/service/myservice | jq .
[
  {
    "ServicePort": 0000,
    "ServiceAddress": "",
    "ServiceTags": null,
    "ServiceName": "myservice",
    "ServiceID": "myservice",
    "Address": "10.3.6.28",
    "Node": "myservice-one"
  },
  {
    "ServicePort": 0000,
    "ServiceAddress": "",
    "ServiceTags": null,
    "ServiceName": "myservice",
    "ServiceID": "myservice",
    "Address": "10.3.6.28",
    "Node": "myservice-main"
  },
  {
    "ServicePort": 0000,
    "ServiceAddress": "",
    "ServiceTags": null,
    "ServiceName": "myservice",
    "ServiceID": "myservice",
    "Address": "10.3.6.26",
    "Node": "myservice-two"
  },
  {
    "ServicePort": 0000,
    "ServiceAddress": "",
    "ServiceTags": null,
    "ServiceName": "myservice",
    "ServiceID": "myservice",
    "Address": "10.3.6.27",
    "Node": "myservice-three"
  }
]


 curl -s -X GET http://10.3.6.21:8500/v1/catalog/node/myservice-main | jq .
{
  "Services": {
    "myservice": {
      "Port": 0000,
      "Address": "",
      "Tags": null,
      "Service": "myservice",
      "ID": "myservice"
    }
  },
  "Node": {
    "Address": "10.3.6.28",
    "Node": "myservice-main"
  }
}

如图所示,我在myservice服务中有一个Node myservice-main。当myservice-main的地址发生变化时,我想执行一些特定的东西。我读到consul watch可以帮助实现它,但我无法编写相同的curl命令。有人能指出我可以用来实现我的要求的http api吗?我只能使用HTTP apis(curl)和shell脚本,因为我想从远程系统中执行它

1 个答案:

答案 0 :(得分:0)

要执行阻止查询,您需要查看响应中的X-Consul-Index标头,并将其用作下一个查询的URL中的查询参数。 Here is the documentation

但文档还指出,即使服务没有变化,您的呼叫也可能会返回。你必须处理索引号。

我建议您查看consul template:您可以编写一个模板文件,其中包含您要监控的所有信息,包括IP地址。一旦这些信息发生变化,consul模板将立即调用您的脚本。