How can I read SRV Consul entries in my go application?

I am trying to implement consul to discover a service, and I am having problems with two things: connecting to a custom DNS server, and formatting my net.LookupSRV() request.

Here is what I am trying to find from my go application:

 $ dig @127.0.0.1 -p 8600 serviceb.service.consul SRV ; <<>> DiG 9.8.4-rpz2+rl005.12-P1 <<>> @127.0.0.1 -p 8600 serviceb.service.consul SRV ; (1 server found) ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4511 ;; flags: qr aa rd; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 2 ;; WARNING: recursion requested but not available ;; QUESTION SECTION: ;serviceb.service.consul. IN SRV ;; ANSWER SECTION: serviceb.service.consul. 0 IN SRV 1 1 80 az1-serviceb1.node.dc1.consul. serviceb.service.consul. 0 IN SRV 1 1 80 az2-serviceb2.node.dc1.consul. ;; ADDITIONAL SECTION: az1-serviceb1.node.dc1.consul. 0 IN A 10.6.41.22 az2-serviceb2.node.dc1.consul. 0 IN A 10.6.41.20 ;; Query time: 6 msec ;; SERVER: 127.0.0.1#8600(127.0.0.1) ;; WHEN: Fri May 16 15:09:28 2014 ;; MSG SIZE rcvd: 275 

and here is the corresponding code. (I know this is wrong, but just so you can see what I'm trying to do)

 cname, addrs, err := net.LookupSRV("serviceb", "service", "consul") log.Printf("%+v %+v %+v", cname, addrs, err) 

and conclusion:

 2014/05/16 15:24:31 [] lookup _serviceb._service.consul: no such host 

Any help would be appreciated! thanks

+6
source share
2 answers

Try using a sharper tool like github.com/miekg/dns . The last time I looked at it, it allowed me to control almost every bit of the client side to resolve DNS.

+2
source

Although this does not answer your exact question, I believe that this is an easier way to access service data for applications with new fields.

Simply call the HTTP API using net/http :

 package main import ( "fmt" "net/http" "io/ioutil" ) func main() { resp, _ := http.Get("http://localhost:8500/v1/catalog/service/serviceb") body, _ := ioutil.ReadAll(resp.Body) fmt.Print(string(body)) } 

The basics of the HTTP API are described in the Service Guide .

0
source

Source: https://habr.com/ru/post/969387/


All Articles