add resolver
parent
e16fa958f9
commit
a1c9db0f80
@ -1,13 +1,53 @@
|
||||
package resolver
|
||||
|
||||
import "google.golang.org/grpc/resolver"
|
||||
import (
|
||||
"zero/core/discov"
|
||||
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
type discovResolver struct {
|
||||
scheme string
|
||||
etcd discov.EtcdConf
|
||||
cc resolver.ClientConn
|
||||
}
|
||||
|
||||
func (r discovResolver) ResolveNow(options resolver.ResolveNowOptions) {
|
||||
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 (r discovResolver) Close() {
|
||||
func RegisterResolver(scheme string, etcd discov.EtcdConf) {
|
||||
resolver.Register(&discovResolver{
|
||||
scheme: scheme,
|
||||
etcd: etcd,
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue