2019-06-24 17:30:17 +03:00
|
|
|
// Package registry resolves ids using the go-micro registry
|
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2019-07-02 22:54:21 +03:00
|
|
|
"fmt"
|
|
|
|
|
2019-06-24 17:30:17 +03:00
|
|
|
"github.com/micro/go-micro/network/resolver"
|
|
|
|
"github.com/micro/go-micro/registry"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
func (r *Resolver) Resolve(id string) ([]*resolver.Record, error) {
|
|
|
|
reg := r.Registry
|
|
|
|
if reg == nil {
|
|
|
|
reg = registry.DefaultRegistry
|
|
|
|
}
|
|
|
|
|
|
|
|
services, err := reg.GetService(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var records []*resolver.Record
|
|
|
|
|
|
|
|
for _, service := range services {
|
|
|
|
for _, node := range service.Nodes {
|
2019-07-02 22:54:21 +03:00
|
|
|
addr := node.Address
|
|
|
|
// such a hack
|
|
|
|
if node.Port > 0 {
|
|
|
|
addr = fmt.Sprintf("%s:%d", node.Address, node.Port)
|
|
|
|
}
|
2019-06-24 17:30:17 +03:00
|
|
|
records = append(records, &resolver.Record{
|
2019-07-02 22:54:21 +03:00
|
|
|
Address: addr,
|
2019-06-24 17:30:17 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|