add net.LookupHost for dns

This commit is contained in:
Asim Aslam 2019-01-07 09:34:07 +00:00
parent f9da55e8a9
commit d6a5ff432c

View File

@ -3,6 +3,7 @@ package dns
import ( import (
"net" "net"
"strconv"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
"github.com/micro/go-micro/selector" "github.com/micro/go-micro/selector"
@ -29,9 +30,36 @@ func (d *dnsSelector) Options() selector.Options {
} }
func (d *dnsSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) { func (d *dnsSelector) Select(service string, opts ...selector.SelectOption) (selector.Next, error) {
_, srv, err := net.LookupSRV(service, "tcp", d.domain) var srv []*net.SRV
// check if its host:port
host, port, err := net.SplitHostPort(service)
// not host:port
if err != nil { if err != nil {
return nil, err // lookup the SRV record
_, srvs, err := net.LookupSRV(service, "tcp", d.domain)
if err != nil {
return nil, err
}
// set SRV records
srv = srvs
// got host:port
} else {
p, _ := strconv.Atoi(port)
// lookup the A record
ips, err := net.LookupHost(host)
if err != nil {
return nil, err
}
// create SRV records
for _, ip := range ips {
srv = append(srv, &net.SRV{
Target: ip,
Port: uint16(p),
})
}
} }
var nodes []*registry.Node var nodes []*registry.Node