fix registry interface (#747)

pull/749/head
Tony Chen 4 years ago committed by GitHub
parent f86c91849b
commit 8722894939
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      registry/registry.go
  2. 10
      transport/grpc/client.go
  3. 12
      transport/grpc/resolver/discovery/builder.go

@ -10,8 +10,8 @@ type Registrar interface {
Deregister(ctx context.Context, service *ServiceInstance) error
}
// Instancer is service instancer.
type Instancer interface {
// Discoverer is service discovery.
type Discoverer interface {
// Service return the service instances in memory according to the service name.
Fetch(ctx context.Context, serviceName string) ([]*ServiceInstance, error)
// Watch creates a watcher according to the service name.

@ -39,9 +39,9 @@ func WithMiddleware(m middleware.Middleware) ClientOption {
}
// WithRegistry with client registry.
func WithRegistry(in registry.Instancer) ClientOption {
func WithRegistry(d registry.Discoverer) ClientOption {
return func(o *clientOptions) {
o.instancer = in
o.discoverer = d
}
}
@ -57,7 +57,7 @@ type clientOptions struct {
endpoint string
timeout time.Duration
middleware middleware.Middleware
instancer registry.Instancer
discoverer registry.Discoverer
grpcOpts []grpc.DialOption
}
@ -85,8 +85,8 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
var grpcOpts = []grpc.DialOption{
grpc.WithUnaryInterceptor(unaryClientInterceptor(options.middleware, options.timeout)),
}
if options.instancer != nil {
grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.instancer)))
if options.discoverer != nil {
grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.discoverer)))
}
if insecure {
grpcOpts = append(grpcOpts, grpc.WithInsecure())

@ -21,15 +21,15 @@ func WithLogger(logger log.Logger) Option {
}
type builder struct {
instancer registry.Instancer
logger log.Logger
discoverer registry.Discoverer
logger log.Logger
}
// NewBuilder creates a builder which is used to factory registry resolvers.
func NewBuilder(in registry.Instancer, opts ...Option) resolver.Builder {
func NewBuilder(d registry.Discoverer, opts ...Option) resolver.Builder {
b := &builder{
instancer: in,
logger: log.DefaultLogger,
discoverer: d,
logger: log.DefaultLogger,
}
for _, o := range opts {
o(b)
@ -38,7 +38,7 @@ func NewBuilder(in registry.Instancer, opts ...Option) resolver.Builder {
}
func (d *builder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
w, err := d.instancer.Watch(context.Background(), target.Endpoint)
w, err := d.discoverer.Watch(context.Background(), target.Endpoint)
if err != nil {
return nil, err
}

Loading…
Cancel
Save