领事观察:如何在更改节点地址时运行脚本



我注册了以下领事服务。

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服务中有一个Nodemyservice main。当myservice main的地址发生变化时,我想执行一些特定的东西。我读到领事手表可以帮助实现它,但我无法为此编写curl命令。有人能给我指一下我可以用来实现我的需求的http api吗?我只能使用HTTPAPI(curl)和shell脚本,因为我想从远程系统

中这样做

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

但文档也指出,即使服务没有变化,您的呼叫也可能返回。你必须注意索引数字。

我建议你改为看看consul模板:你会写一个模板文件,里面有你想要监控的所有信息,包括IP地址。一旦这些信息发生变化,执政官模板就会调用您的脚本。

最新更新