2019-07-28 22:00:09 +03:00
|
|
|
// Package registry resolves names using the go-micro registry
|
2019-07-28 14:14:40 +03:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2020-08-19 17:47:17 +03:00
|
|
|
"github.com/unistack-org/micro/v3/resolver"
|
|
|
|
"github.com/unistack-org/micro/v3/registry"
|
|
|
|
"github.com/unistack-org/micro/v3/registry/mdns"
|
2019-07-28 14:14:40 +03:00
|
|
|
)
|
|
|
|
|
2019-08-20 14:48:51 +03:00
|
|
|
// Resolver is a registry network resolver
|
2019-07-28 14:14:40 +03:00
|
|
|
type Resolver struct {
|
|
|
|
// Registry is the registry to use otherwise we use the defaul
|
|
|
|
Registry registry.Registry
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve assumes ID is a domain name e.g micro.mu
|
2019-07-28 22:00:09 +03:00
|
|
|
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
2019-07-28 14:14:40 +03:00
|
|
|
reg := r.Registry
|
|
|
|
if reg == nil {
|
2020-07-27 15:22:00 +03:00
|
|
|
reg = mdns.NewRegistry()
|
2019-07-28 14:14:40 +03:00
|
|
|
}
|
|
|
|
|
2019-07-28 22:00:09 +03:00
|
|
|
services, err := reg.GetService(name)
|
2019-07-28 14:14:40 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []*resolver.Record
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
for _, node := range service.Nodes {
|
|
|
|
records = append(records, &resolver.Record{
|
|
|
|
Address: node.Address,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|