mirror of
https://github.com/go-kratos/kratos.git
synced 2024-12-26 20:54:38 +02:00
fix registry interface (#747)
This commit is contained in:
parent
f86c91849b
commit
8722894939
@ -10,8 +10,8 @@ type Registrar interface {
|
|||||||
Deregister(ctx context.Context, service *ServiceInstance) error
|
Deregister(ctx context.Context, service *ServiceInstance) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Instancer is service instancer.
|
// Discoverer is service discovery.
|
||||||
type Instancer interface {
|
type Discoverer interface {
|
||||||
// Service return the service instances in memory according to the service name.
|
// Service return the service instances in memory according to the service name.
|
||||||
Fetch(ctx context.Context, serviceName string) ([]*ServiceInstance, error)
|
Fetch(ctx context.Context, serviceName string) ([]*ServiceInstance, error)
|
||||||
// Watch creates a watcher according to the service name.
|
// Watch creates a watcher according to the service name.
|
||||||
|
@ -39,9 +39,9 @@ func WithMiddleware(m middleware.Middleware) ClientOption {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WithRegistry with client registry.
|
// WithRegistry with client registry.
|
||||||
func WithRegistry(in registry.Instancer) ClientOption {
|
func WithRegistry(d registry.Discoverer) ClientOption {
|
||||||
return func(o *clientOptions) {
|
return func(o *clientOptions) {
|
||||||
o.instancer = in
|
o.discoverer = d
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ type clientOptions struct {
|
|||||||
endpoint string
|
endpoint string
|
||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
middleware middleware.Middleware
|
middleware middleware.Middleware
|
||||||
instancer registry.Instancer
|
discoverer registry.Discoverer
|
||||||
grpcOpts []grpc.DialOption
|
grpcOpts []grpc.DialOption
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,8 +85,8 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
|
|||||||
var grpcOpts = []grpc.DialOption{
|
var grpcOpts = []grpc.DialOption{
|
||||||
grpc.WithUnaryInterceptor(unaryClientInterceptor(options.middleware, options.timeout)),
|
grpc.WithUnaryInterceptor(unaryClientInterceptor(options.middleware, options.timeout)),
|
||||||
}
|
}
|
||||||
if options.instancer != nil {
|
if options.discoverer != nil {
|
||||||
grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.instancer)))
|
grpcOpts = append(grpcOpts, grpc.WithResolvers(discovery.NewBuilder(options.discoverer)))
|
||||||
}
|
}
|
||||||
if insecure {
|
if insecure {
|
||||||
grpcOpts = append(grpcOpts, grpc.WithInsecure())
|
grpcOpts = append(grpcOpts, grpc.WithInsecure())
|
||||||
|
@ -21,15 +21,15 @@ func WithLogger(logger log.Logger) Option {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type builder struct {
|
type builder struct {
|
||||||
instancer registry.Instancer
|
discoverer registry.Discoverer
|
||||||
logger log.Logger
|
logger log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBuilder creates a builder which is used to factory registry resolvers.
|
// 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{
|
b := &builder{
|
||||||
instancer: in,
|
discoverer: d,
|
||||||
logger: log.DefaultLogger,
|
logger: log.DefaultLogger,
|
||||||
}
|
}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(b)
|
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) {
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user