go-zero/rpcx/internal/resolver/resolver.go
2020-08-06 14:05:15 +08:00

54 lines
998 B
Go

package resolver
import (
"zero/core/discov"
"google.golang.org/grpc/resolver"
)
type discovResolver struct {
scheme string
etcd discov.EtcdConf
cc resolver.ClientConn
}
func (r *discovResolver) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (
resolver.Resolver, error) {
sub, err := discov.NewSubscriber(r.etcd.Hosts, r.etcd.Key)
if err != nil {
return nil, err
}
sub.AddListener(func() {
vals := sub.Values()
var addrs []resolver.Address
for _, val := range vals {
addrs = append(addrs, resolver.Address{
Addr: val,
})
}
r.cc.UpdateState(resolver.State{
Addresses: addrs,
})
})
return r, nil
}
func (r *discovResolver) Close() {
}
func (r *discovResolver) ResolveNow(options resolver.ResolveNowOptions) {
}
func (r *discovResolver) Scheme() string {
return r.scheme
}
func RegisterResolver(scheme string, etcd discov.EtcdConf) {
resolver.Register(&discovResolver{
scheme: scheme,
etcd: etcd,
})
}