Merge pull request #730 from go-kratos/refactor/naming

split naming registry
pull/732/head
longxboy 4 years ago committed by GitHub
commit 68e857746d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app.go
  2. 5
      options.go
  3. 12
      registry/registry.go
  4. 12
      transport/grpc/client.go
  5. 8
      transport/grpc/resolver/discovery/builder.go

@ -58,7 +58,7 @@ func (a *App) Server() []transport.Server {
} }
// Registry returns registry. // Registry returns registry.
func (a *App) Registry() registry.Registry { func (a *App) Registry() registry.Registrar {
return a.opts.registry return a.opts.registry
} }

@ -24,7 +24,8 @@ type options struct {
sigs []os.Signal sigs []os.Signal
logger log.Logger logger log.Logger
registry registry.Registry registry registry.Registrar
servers []transport.Server servers []transport.Server
} }
@ -69,7 +70,7 @@ func Logger(logger log.Logger) Option {
} }
// Registry with service registry. // Registry with service registry.
func Registry(r registry.Registry) Option { func Registry(r registry.Registrar) Option {
return func(o *options) { o.registry = r } return func(o *options) { o.registry = r }
} }

@ -2,16 +2,20 @@ package registry
import "context" import "context"
// Registry is service registry. // Registrar is service registrar.
type Registry interface { type Registrar interface {
// Register the registration. // Register the registration.
Register(ctx context.Context, service *ServiceInstance) error Register(ctx context.Context, service *ServiceInstance) error
// Deregister the registration. // Deregister the registration.
Deregister(ctx context.Context, service *ServiceInstance) error Deregister(ctx context.Context, service *ServiceInstance) error
}
// Discoverer is service discovery.
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.
Service(ctx context.Context, name 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.
Watch(ctx context.Context, name string) (Watcher, error) Watch(ctx context.Context, serviceName string) (Watcher, error)
} }
// Watcher is service watcher. // Watcher is service watcher.

@ -38,10 +38,10 @@ func WithMiddleware(m middleware.Middleware) ClientOption {
} }
} }
// WithRegistry with client registry. // WithDiscovery with client registry.
func WithRegistry(r registry.Registry) ClientOption { func WithDiscovery(r registry.Discoverer) ClientOption {
return func(o *clientOptions) { return func(o *clientOptions) {
o.registry = r o.discovery = r
} }
} }
@ -57,7 +57,7 @@ type clientOptions struct {
endpoint string endpoint string
timeout time.Duration timeout time.Duration
middleware middleware.Middleware middleware middleware.Middleware
registry registry.Registry discovery registry.Discoverer
grpcOpts []grpc.DialOption grpcOpts []grpc.DialOption
} }
@ -86,8 +86,8 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
grpc.WithTimeout(options.timeout), grpc.WithTimeout(options.timeout),
grpc.WithUnaryInterceptor(UnaryClientInterceptor(options.middleware)), grpc.WithUnaryInterceptor(UnaryClientInterceptor(options.middleware)),
} }
if options.registry != nil { if options.discovery != nil {
grpc.WithResolvers(discovery.NewBuilder(options.registry)) grpc.WithResolvers(discovery.NewBuilder(options.discovery))
} }
if insecure { if insecure {
grpcOpts = append(grpcOpts, grpc.WithInsecure()) grpcOpts = append(grpcOpts, grpc.WithInsecure())

@ -21,14 +21,14 @@ func WithLogger(logger log.Logger) Option {
} }
type builder struct { type builder struct {
registry registry.Registry 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(r registry.Registry, opts ...Option) resolver.Builder { func NewBuilder(r registry.Discoverer, opts ...Option) resolver.Builder {
b := &builder{ b := &builder{
registry: r, discoverer: r,
logger: log.DefaultLogger, logger: log.DefaultLogger,
} }
for _, o := range opts { for _, o := range opts {
@ -38,7 +38,7 @@ func NewBuilder(r registry.Registry, 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.registry.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…
Cancel
Save