如何使用逗号连接领事模板的服务元数据

有人知道如何将consul中的字符串连接到consul模板吗? </ p>

如果我在领事馆中注册了服务'foo'</ p>

  {
“ Node”:“ node1”,
“ 地址”:“ 192.168.0.1”,
“端口”:3333
},
{
“节点”:“ node2”,
“地址”:“ 192.168.0.2”,
“端口” :4444
}
</ code> </ pre>

我希望领事模板生成以下行:</ p>

  servers =  192.168.0.1:3333,192.168.0.2:4444/伪造的
</ code> </ pre>

以下尝试不起作用,因为它留下了结尾的逗号,</ code> </ p>

  servers = {{range service“ foo”}} {{。Address}} {{。Port}},{{end}} /伪造
#渲染\ nservers = 192.168.0.1:3333,192.168.0.2:4444,/伪造

#我实际上想要的
servers = 192.168.0.1:3333,192.168.0.2:4444 / bogus
</ code> </ pre> >

我知道consul-template使用golang模板语法,但是我根本无法弄清楚该语法可以使它正常工作。 我可能应该使用领事模板的 join </ code>,但是如何将 .Address </ code>和 .Port </ code>都传递给 join </ code >? 这只是一个简单的例子,由于服务数量可能超过两个,因此我没有故意使用索引。 有什么想法吗?</ p>
</ div>

展开原文

原文

Does anyone know how to concatenate strings from consul for consul-template?

If I have a service 'foo' registered in Consul

{
  "Node": "node1",
  "Address": "192.168.0.1",
  "Port": 3333
},
{
  "Node": "node2",
  "Address": "192.168.0.2",
  "Port": 4444
}

I would like consul-template to generate the following line:

servers=192.168.0.1:3333,192.168.0.2:4444/bogus

The following attempt does not work since it leaves a trailing comma ,

servers={{range service "foo"}}{{.Address}}{{.Port}},{{end}}/bogus
# renders
servers=192.168.0.1:3333,192.168.0.2:4444,/bogus

# What I actually want
servers=192.168.0.1:3333,192.168.0.2:4444/bogus

I know consul-template uses golang template syntax, but I simply cannot figure out the syntax to get this working. Its likely that I should use consul-template's join but how do I pass both .Address and .Port to join? This is just a trivial example, and I'm not using indexes intentionally since the number of services could be more than two. Any ideas?

2个回答



这应该起作用。</ p>

  {{$$ foo_srv:= service“ foo”  }} 
{{if $ foo_srv}}
{{$ last:= len $ foo_srv | 减去1}}
servers =
{{-range $ i:= loop $ last}}
{{-with index $ foo_srv $ i}} {{。Address}} {{。Port}},{{ end}}
{{-end}}
{{-,索引为$ foo_srv last}} {{。Address}} {{。Port}} {{end}} /伪造的
{{end}} \ n </ code> </ pre>

我在想是否可以使用“ join”。</ p>

注意“ {{-”表示删除前导空格( 这样的'',\ t,
)。</ p>
</ div>

展开原文

原文

This should work.

{{$foo_srv := service "foo"}}
{{if $foo_srv}}
  {{$last := len $foo_srv | subtract 1}}
servers=
  {{- range $i := loop $last}}
    {{- with index $foo_srv $i}}{{.Address}}{{.Port}},{{end}}
  {{- end}}
  {{- with index $foo_srv last}}{{.Address}}{{.Port}}{{end}}/bogus
{{end}}

I was thinking if "join" can be used.

Note "{{-" means removing leading white spaces (such ' ', \t, ).

You can use a custom plugin.

servers={{service "foo" | toJSON | plugin "path/to/plugin"}}

The plugin code:

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type InputEntry struct {
    Node    string
    Address string
    Port    int
}

func main() {
    arg := []byte(os.Args[1])
    var input []InputEntry
    if err := json.Unmarshal(arg, &input); err != nil {
        fmt.Fprintln(os.Stderr, fmt.Sprintf("err: %s", err))
        os.Exit(1)
    }

    var output string
    for i, entry := range input {
        output += fmt.Sprintf("%v:%v", entry.Address, entry.Port)
        if i != len(input)-1 {
            output += ","
        }
    }

    fmt.Fprintln(os.Stdout, string(output))
    os.Exit(0)
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问