首页 > 解决方案 > 查询Consul服务目录时如何检索有限数量的IP

问题描述

有没有办法从 Consul 服务目录中只检索 3 个 IP?

{{if service 'web' 'passing'}}
 "{{range $index, $service := service 'web'}}{{if ne $index 0}},{{end}}{{.Address}}{{end}}"

上面的代码获取所有通过 web 服务检查的逗号分隔的 IP(我有 100 个 IP)。我只需要前 3 个 IP。

-W

标签: consulconsul-template

解决方案


{{range $index, $service := service "web"}}{{if gt $index 3}}{{.Address}}:{{ .Port }},{{end}}{{end}}

应该这样做。


推荐阅读