diff --git a/rpcx/internal/resolver/resolver.go b/rpcx/internal/resolver/resolver.go index 4f2e4a7e..9639e26e 100644 --- a/rpcx/internal/resolver/resolver.go +++ b/rpcx/internal/resolver/resolver.go @@ -1,13 +1,53 @@ package resolver -import "google.golang.org/grpc/resolver" +import ( + "zero/core/discov" + + "google.golang.org/grpc/resolver" +) type discovResolver struct { - cc resolver.ClientConn + 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) ResolveNow(options resolver.ResolveNowOptions) { +func (r *discovResolver) Scheme() string { + return r.scheme } -func (r discovResolver) Close() { +func RegisterResolver(scheme string, etcd discov.EtcdConf) { + resolver.Register(&discovResolver{ + scheme: scheme, + etcd: etcd, + }) }