0

我注册了以下领事服务。

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 服务中有一个节点 myservice-main。当 myservice-main 的地址发生变化时,我想执行一些特定的东西。我读到 consul watch 可以帮助实现它,但我无法为此编写 curl 命令。谁能指出我可以用来实现我的要求的http api?我只能使用 HTTP apis(curl) 和 shell 脚本,因为我想从远程系统执行它

4

1 回答 1

0

要进行阻塞查询,您需要查看X-Consul-Index响应中的标头并将其用作 URL 中的查询参数以进行下一个查询。这是文档

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

我建议您改为查看consul 模板:您将编写一个模板文件,其中包含您想要监控的所有信息,包括 IP 地址。然后,只要这些信息发生任何变化,领事模板就会调用您的脚本。

于 2015-12-12T15:12:38.563 回答