diff --git a/network/resolver/dns/dns.go b/network/resolver/dns/dns.go index 3cfa2746..fe022d93 100644 --- a/network/resolver/dns/dns.go +++ b/network/resolver/dns/dns.go @@ -1,8 +1,7 @@ -// Package dns resolves names to dns srv records +// Package dns resolves names to dns records package dns import ( - "fmt" "net" "github.com/micro/go-micro/network/resolver" @@ -13,19 +12,27 @@ type Resolver struct{} // Resolve assumes ID is a domain name e.g micro.mu func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) { - _, addrs, err := net.LookupSRV("network", "udp", name) + host, port, err := net.SplitHostPort(name) + if err != nil { + host = name + port = "8085" + } + + addrs, err := net.LookupHost(host) if err != nil { return nil, err } + var records []*resolver.Record + for _, addr := range addrs { - address := addr.Target - if addr.Port > 0 { - address = fmt.Sprintf("%s:%d", addr.Target, addr.Port) - } + // join resolved record with port + address := net.JoinHostPort(addr, port) + // append to record set records = append(records, &resolver.Record{ Address: address, }) } + return records, nil } diff --git a/network/resolver/dnssrv/dnssrv.go b/network/resolver/dnssrv/dnssrv.go new file mode 100644 index 00000000..75a11795 --- /dev/null +++ b/network/resolver/dnssrv/dnssrv.go @@ -0,0 +1,31 @@ +// Package dns srv resolves names to dns srv records +package dnssrv + +import ( + "fmt" + "net" + + "github.com/micro/go-micro/network/resolver" +) + +// Resolver is a DNS network resolve +type Resolver struct{} + +// Resolve assumes ID is a domain name e.g micro.mu +func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) { + _, addrs, err := net.LookupSRV("network", "udp", name) + if err != nil { + return nil, err + } + var records []*resolver.Record + for _, addr := range addrs { + address := addr.Target + if addr.Port > 0 { + address = fmt.Sprintf("%s:%d", addr.Target, addr.Port) + } + records = append(records, &resolver.Record{ + Address: address, + }) + } + return records, nil +}