mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-10 00:29:01 +02:00
Merge pull request #730 from go-kratos/refactor/naming
split naming registry
This commit is contained in:
commit
68e857746d
2
app.go
2
app.go
@ -58,7 +58,7 @@ func (a *App) Server() []transport.Server {
|
||||
}
|
||||
|
||||
// Registry returns registry.
|
||||
func (a *App) Registry() registry.Registry {
|
||||
func (a *App) Registry() registry.Registrar {
|
||||
return a.opts.registry
|
||||
}
|
||||
|
||||
|
@ -24,8 +24,9 @@ type options struct {
|
||||
sigs []os.Signal
|
||||
|
||||
logger log.Logger
|
||||
registry registry.Registry
|
||||
servers []transport.Server
|
||||
registry registry.Registrar
|
||||
|
||||
servers []transport.Server
|
||||
}
|
||||
|
||||
// ID with service id.
|
||||
@ -69,7 +70,7 @@ func Logger(logger log.Logger) Option {
|
||||
}
|
||||
|
||||
// Registry with service registry.
|
||||
func Registry(r registry.Registry) Option {
|
||||
func Registry(r registry.Registrar) Option {
|
||||
return func(o *options) { o.registry = r }
|
||||
}
|
||||
|
||||
|
@ -2,16 +2,20 @@ package registry
|
||||
|
||||
import "context"
|
||||
|
||||
// Registry is service registry.
|
||||
type Registry interface {
|
||||
// Registrar is service registrar.
|
||||
type Registrar interface {
|
||||
// Register the registration.
|
||||
Register(ctx context.Context, service *ServiceInstance) error
|
||||
// Deregister the registration.
|
||||
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(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(ctx context.Context, name string) (Watcher, error)
|
||||
Watch(ctx context.Context, serviceName string) (Watcher, error)
|
||||
}
|
||||
|
||||
// Watcher is service watcher.
|
||||
|
@ -38,10 +38,10 @@ func WithMiddleware(m middleware.Middleware) ClientOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithRegistry with client registry.
|
||||
func WithRegistry(r registry.Registry) ClientOption {
|
||||
// WithDiscovery with client registry.
|
||||
func WithDiscovery(r registry.Discoverer) ClientOption {
|
||||
return func(o *clientOptions) {
|
||||
o.registry = r
|
||||
o.discovery = r
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,7 +57,7 @@ type clientOptions struct {
|
||||
endpoint string
|
||||
timeout time.Duration
|
||||
middleware middleware.Middleware
|
||||
registry registry.Registry
|
||||
discovery registry.Discoverer
|
||||
grpcOpts []grpc.DialOption
|
||||
}
|
||||
|
||||
@ -86,8 +86,8 @@ func dial(ctx context.Context, insecure bool, opts ...ClientOption) (*grpc.Clien
|
||||
grpc.WithTimeout(options.timeout),
|
||||
grpc.WithUnaryInterceptor(UnaryClientInterceptor(options.middleware)),
|
||||
}
|
||||
if options.registry != nil {
|
||||
grpc.WithResolvers(discovery.NewBuilder(options.registry))
|
||||
if options.discovery != nil {
|
||||
grpc.WithResolvers(discovery.NewBuilder(options.discovery))
|
||||
}
|
||||
if insecure {
|
||||
grpcOpts = append(grpcOpts, grpc.WithInsecure())
|
||||
|
@ -21,15 +21,15 @@ func WithLogger(logger log.Logger) Option {
|
||||
}
|
||||
|
||||
type builder struct {
|
||||
registry registry.Registry
|
||||
logger log.Logger
|
||||
discoverer registry.Discoverer
|
||||
logger log.Logger
|
||||
}
|
||||
|
||||
// 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{
|
||||
registry: r,
|
||||
logger: log.DefaultLogger,
|
||||
discoverer: r,
|
||||
logger: log.DefaultLogger,
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(b)
|
||||
@ -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) {
|
||||
w, err := d.registry.Watch(context.Background(), target.Endpoint)
|
||||
w, err := d.discoverer.Watch(context.Background(), target.Endpoint)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user