add net.LookupHost for dns
This commit is contained in:
parent
f9da55e8a9
commit
d6a5ff432c
@ -3,6 +3,7 @@ package dns
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/micro/go-micro/registry"
|
||||
"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) {
|
||||
_, 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 {
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user