2

我有一些类似于以下内容的领事节点:

 [
    {
        "Address": "127.0.0.1",
        "Node": "foo",
        "ServiceAddress": "",
        "ServiceName": "api",
        "ServicePort": 8100,
        "ServiceTags": [
            "production",
            "blocking"
        ]
    },
    {
        "Address": "127.0.0.1",
        "Node": "foo",
        "ServiceAddress": "",
        "ServiceName": "api",
        "ServicePort": 8101,
        "ServiceTags": [
            "production",
            "nonblocking"
        ]
    }
]

按一个标签过滤很容易:

{{range service "production.api"}}
{{.Address}}
{{end}}

但是如何一次通过两个标签过滤我的领事模板中的服务?

4

2 回答 2

3

从 consul-template v0.11.1 开始,您可以使用contains运算符执行以下操作:

{{range service "production.api"}}
{{if .Tags | contains "nonblocking"}}
{{.Address}}
{{end}}
{{end}}

如果您使用的是旧版本,则可以利用 Go:

{{range service "api"}}
{{if and (.Tags.Contains "nonblocking") (.Tags.Contains "production")}}
{{end}}
{{end}}

另见:https ://github.com/hashicorp/consul-template/issues/260

于 2015-12-09T21:14:54.007 回答
0

这就是我在 haproxy 中使用服务标签的方式,因此可以在 nginx 中完成类似的操作

{{ range $tag, $services := service "some-service" | byTag }}
backend some-service-{{ $tag }}

   {{ if eq $tag "some_tag" }}
   ....
   {{ end }}
   ...

   {{ range $services }}
   server {{.Address}}-{{.Port}} {{.Address}}:{{.Port}} check downinter 3s inter 2000 fall 3 maxconn 100 check cookie {{.ID}} weight 1
   {{ end }}
{{ end }}
于 2017-08-14T11:10:59.073 回答