Consul使用【watcher handle使用】

  • 创建watcher.json文件,放在consul配置目录下,启动consul时指定-config-dir
{
  "watches": [
    {
      "type": "key",
      "key": "foo/data",
      "handler_type": "http",
      "http_handler_config": {
         "path":"http://127.0.0.1:8763/watch",
         "method": "POST",
         "header": {"x-foo":["bar", "baz"]},
         "timeout": "10s",
         "tls_skip_verify": false
      }
    }
  ]
}
    @RequestMapping("/watch")
    public void watcher(HttpServletRequest request) throws IOException {
        System.out.println(request.getMethod());
        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String str = "";
        String wholeStr = "";
        while ((str = reader.readLine()) != null) {
            wholeStr += str;
        }
        System.out.println("body:" + wholeStr);
    }
  • 在consul ui中新增 或者修改foo/data这个key值,watcher就会收到这个更新后的值
POST
body:{"Key":"foo/data","CreateIndex":12,"ModifyIndex":12,"LockIndex":0,"Flags":0,"Value":"YWFhYWFhYQ==","Session":""}
POST
body:{"Key":"foo/data","CreateIndex":12,"ModifyIndex":17,"LockIndex":0,"Flags":0,"Value":"YWFhYWFhYXNzc3NkZHNkZHNk","Session":""}
POST
body:{"Key":"foo/data","CreateIndex":12,"ModifyIndex":25,"LockIndex":0,"Flags":0,"Value":"ewogICJ0ZXN0IjoidGVzdCIsCiAgInRlc3QyIjoidDMzM2VzdCIKfQ==","Session":""}

value为base64编码,解码后得到需要的值。

  • handle除了是http,还可以是sh脚本,如
    {
      "type": "key",
      "key": "foo/bar/baz",
      "handler_type": "script",
      "args": ["/usr/bin/my-service-handler.sh", "-redis"]
    }

    以下摘自官网:https://www.consul.io/docs/agent/watches.html#key

  • Watch Types/支持的类型

    The following types are supported. Detailed documentation on each is below:

  • 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
{
  "type": "key",
  "key": "foo/bar/baz",
  "args": ["/usr/bin/my-service-handler.sh", "-redis"]
}
{
  "type": "keyprefix",
  "prefix": "foo/",
  "args": ["/usr/bin/my-service-handler.sh", "-redis"]
}
{
  "type": "service",
  "service": "redis",
  "args": ["/usr/bin/my-service-handler.sh", "-redis"],
  "tag": "bar"
}

等等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值