From 67161b62ebb64e4af1cbf46e68b2b968a2594007 Mon Sep 17 00:00:00 2001 From: leyou240 Date: Thu, 2 Dec 2021 11:38:01 +0800 Subject: [PATCH] feat(registry/nacos): add default kind option (#1650) --- contrib/registry/nacos/registry.go | 15 +++++++++++++-- contrib/registry/nacos/watcher.go | 10 ++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/contrib/registry/nacos/registry.go b/contrib/registry/nacos/registry.go index 56414ef70..327b5109b 100644 --- a/contrib/registry/nacos/registry.go +++ b/contrib/registry/nacos/registry.go @@ -23,6 +23,7 @@ type options struct { weight float64 cluster string group string + kind string } // Option is nacos option. @@ -48,6 +49,11 @@ func WithGroup(group string) Option { return func(o *options) { o.group = group } } +// WithDefaultKind with default kind option. +func WithDefaultKind(kind string) Option { + return func(o *options) { o.kind = kind } +} + // Registry is nacos registry. type Registry struct { opts options @@ -61,6 +67,7 @@ func New(cli naming_client.INamingClient, opts ...Option) (r *Registry) { cluster: "DEFAULT", group: "DEFAULT_GROUP", weight: 100, + kind: "grpc", } for _, option := range opts { option(&op) @@ -144,7 +151,7 @@ func (r *Registry) Deregister(ctx context.Context, service *registry.ServiceInst // Watch creates a watcher according to the service name. func (r *Registry) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { - return newWatcher(ctx, r.cli, serviceName, r.opts.group, []string{r.opts.cluster}) + return newWatcher(ctx, r.cli, serviceName, r.opts.group, r.opts.kind, []string{r.opts.cluster}) } // GetService return the service instances in memory according to the service name. @@ -158,12 +165,16 @@ func (r *Registry) GetService(ctx context.Context, serviceName string) ([]*regis } items := make([]*registry.ServiceInstance, 0, len(res)) for _, in := range res { + kind := r.opts.kind + if k, ok := in.Metadata["kind"]; ok { + kind = k + } items = append(items, ®istry.ServiceInstance{ ID: in.InstanceId, Name: in.ServiceName, Version: in.Metadata["version"], Metadata: in.Metadata, - Endpoints: []string{fmt.Sprintf("%s://%s:%d", in.Metadata["kind"], in.Ip, in.Port)}, + Endpoints: []string{fmt.Sprintf("%s://%s:%d", kind, in.Ip, in.Port)}, }) } return items, nil diff --git a/contrib/registry/nacos/watcher.go b/contrib/registry/nacos/watcher.go index 22961b69f..eac90b12f 100644 --- a/contrib/registry/nacos/watcher.go +++ b/contrib/registry/nacos/watcher.go @@ -20,14 +20,16 @@ type watcher struct { cancel context.CancelFunc watchChan chan struct{} cli naming_client.INamingClient + kind string } -func newWatcher(ctx context.Context, cli naming_client.INamingClient, serviceName string, groupName string, clusters []string) (*watcher, error) { +func newWatcher(ctx context.Context, cli naming_client.INamingClient, serviceName, groupName, kind string, clusters []string) (*watcher, error) { w := &watcher{ serviceName: serviceName, clusters: clusters, groupName: groupName, cli: cli, + kind: kind, watchChan: make(chan struct{}, 1), } w.ctx, w.cancel = context.WithCancel(ctx) @@ -59,12 +61,16 @@ func (w *watcher) Next() ([]*registry.ServiceInstance, error) { } items := make([]*registry.ServiceInstance, 0, len(res.Hosts)) for _, in := range res.Hosts { + kind := w.kind + if k, ok := in.Metadata["kind"]; ok { + kind = k + } items = append(items, ®istry.ServiceInstance{ ID: in.InstanceId, Name: res.Name, Version: in.Metadata["version"], Metadata: in.Metadata, - Endpoints: []string{fmt.Sprintf("%s://%s:%d", in.Metadata["kind"], in.Ip, in.Port)}, + Endpoints: []string{fmt.Sprintf("%s://%s:%d", kind, in.Ip, in.Port)}, }) } return items, nil