2021-01-29 13:17:32 +03:00
|
|
|
// Package register resolves names using the micro register
|
2021-10-02 19:55:07 +03:00
|
|
|
package register // import "go.unistack.org/micro/v3/resolver/registry"
|
2019-07-28 12:14:40 +01:00
|
|
|
|
|
|
|
import (
|
2020-11-03 02:02:32 +03:00
|
|
|
"context"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/register"
|
|
|
|
"go.unistack.org/micro/v3/resolver"
|
2019-07-28 12:14:40 +01:00
|
|
|
)
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
// Resolver is a register network resolver
|
2019-07-28 12:14:40 +01:00
|
|
|
type Resolver struct {
|
2021-01-29 13:17:32 +03:00
|
|
|
// Register is the register to use otherwise we use the defaul
|
|
|
|
Register register.Register
|
2019-07-28 12:14:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve assumes ID is a domain name e.g micro.mu
|
2019-07-28 20:00:09 +01:00
|
|
|
func (r *Resolver) Resolve(name string) ([]*resolver.Record, error) {
|
2021-01-29 13:17:32 +03:00
|
|
|
services, err := r.Register.LookupService(context.TODO(), name)
|
2019-07-28 12:14:40 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-29 17:43:36 +03:00
|
|
|
records := make([]*resolver.Record, 0, len(services))
|
2019-07-28 12:14:40 +01:00
|
|
|
for _, service := range services {
|
|
|
|
for _, node := range service.Nodes {
|
|
|
|
records = append(records, &resolver.Record{
|
|
|
|
Address: node.Address,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|