在Golang中获取Cosul SRV记录 [英] Fetching Cosul SRV records in golang

查看:195
本文介绍了在Golang中获取Cosul SRV记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Main应用程序和多个工作程序节点,这些节点在领事中注册.我想通过负载平衡将数据发送到工作节点.使用consul API for golang,我可以在主应用程序上获得可用的服务.

I have a Main application and multiple worker nodes, which are registered in consul. I want to send data to the worker nodes, by load balancing. Using the consul API for golang, I'm able to get the available Services on the main application.

但是,我无法在golang应用程序中获取DNS SRV记录.

But, I'm unable to get DNS SRV records in my golang application.

如该主题所述,如何我可以在go应用程序中读取领事SRV记录吗?,我尝试了github.com/miekg/dns,但是没有用. 另外,我尝试使用github.com/benschw/consul-clb-go,如下:

As mention in this thread, How can I read consul SRV records in my go application?, I tried github.com/miekg/dns, but it didn't work. Also, I tried using github.com/benschw/consul-clb-go, as:

c := clb.NewClb("127.0.0.1", "8600", clb.Random)

srvRecord := "Processor" + ".service.consul"
address, err := c.GetAddress(srvRecord)
if err != nil {
    fmt.Println(err)
}
fmt.Println(address)

它给了我这个错误:

panic: runtime error: index out of range [0] with length 0

此外,我尝试按以下方式使用网络软件包:

Also,I tried using the net package as follows:

resolver := &net.Resolver{
    Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
        return (&net.Dialer{}).DialContext(ctx, network, "127.0.0.1:8600")
    },
}
_, addrs, err := resolver.LookupSRV(
    context.Background(), "Processor", "tcp", "consul",
)
if err != nil {
    fmt.Printf("Error : %v", err)
}
fmt.Println(addrs)

它返回:

Error : lookup _Processor._tcp.consul: dnsquery: DNS name does not exist.[]

我还尝试添加服务"到查询字符串,但它也返回了相同的错误.

I also tried adding "service" to the query string, but it also returned the same error.

但是,dig会正确返回:

But, dig returns correctly:

C:\Users\Sude>dig @127.0.0.1 -p 8600 Processor.service.consul SRV

; <<>> DiG 9.8.8 <<>> @127.0.0.1 -p 8600 Processor.service.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62807
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 0
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;Processor.service.consul.      IN      SRV

;; ANSWER SECTION:
Processor.service.consul. 0     IN      SRV     1 1 8001 localhost.
Processor.service.consul. 0     IN      SRV     1 1 8005 localhost.
Processor.service.consul. 0     IN      SRV     1 1 8004 localhost.

;; Query time: 0 msec
;; SERVER: 127.0.0.1#8600(127.0.0.1)
;; WHEN: Sat Jun 27 09:37:05 India Standard Time 2020
;; MSG SIZE  rcvd: 129

我如何在go应用程序中读取这些记录?

How could I Read these records inside my go application?

此外,转到Consul API 中是否有任何功能获得负载平衡的端点?这也足够了.

Also, is there any function in Go Consul API to get load-balanced endpoints? It would also be sufficient.

推荐答案

经过领事Golang API文档. 最好的方法是使用PreparedQueries:

I found the solution after going through the Consul Golang API docs. The best way is to use PreparedQueries:

preparedQuery := consul.PreparedQuery()
queryID, _, err := pq.Create(&consulapi.PreparedQueryDefinition{
    Name: "DnsQuery",
    Service: consulapi.ServiceQuery{
        Service:     "Processor",
        OnlyPassing: true,
    },
}, &consulapi.WriteOptions{})
if err != nil {
    fmt.Println(err)
}

res, _, _ := preparedQuery.Execute(queryID, &consulapi.QueryOptions{})
for _, node := range res.Nodes {
    fmt.Println(node.Service.Address, node.Service.Port)
}

res.Nodes是服务端点的一部分.

res.Nodes is a slice of the service endpoints.

这篇关于在Golang中获取Cosul SRV记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆