2015-12-09 37 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 }}