2015-12-09 39 views
1

我公司拥有一批领事节点,看起来类似于:一个标签如何使用consul-template中的多个标签过滤consul节点?

[ 
    { 
     "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}} 

,但我怎么能由两个标签筛选我领事模板内的服务立刻?

回答

2

由于领事模板v0.11.1的你可以使用contains运营商要做到:

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

如果您使用的是旧版本,你可以拿围棋的优势:

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

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

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 }}