feat(registry/nacos): add default kind option (#1650)

pull/1661/head
leyou240 3 years ago committed by GitHub
parent 98fd40bcb4
commit 67161b62eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      contrib/registry/nacos/registry.go
  2. 10
      contrib/registry/nacos/watcher.go

@ -23,6 +23,7 @@ type options struct {
weight float64 weight float64
cluster string cluster string
group string group string
kind string
} }
// Option is nacos option. // Option is nacos option.
@ -48,6 +49,11 @@ func WithGroup(group string) Option {
return func(o *options) { o.group = group } 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. // Registry is nacos registry.
type Registry struct { type Registry struct {
opts options opts options
@ -61,6 +67,7 @@ func New(cli naming_client.INamingClient, opts ...Option) (r *Registry) {
cluster: "DEFAULT", cluster: "DEFAULT",
group: "DEFAULT_GROUP", group: "DEFAULT_GROUP",
weight: 100, weight: 100,
kind: "grpc",
} }
for _, option := range opts { for _, option := range opts {
option(&op) 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. // Watch creates a watcher according to the service name.
func (r *Registry) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { 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. // 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)) items := make([]*registry.ServiceInstance, 0, len(res))
for _, in := range res { for _, in := range res {
kind := r.opts.kind
if k, ok := in.Metadata["kind"]; ok {
kind = k
}
items = append(items, &registry.ServiceInstance{ items = append(items, &registry.ServiceInstance{
ID: in.InstanceId, ID: in.InstanceId,
Name: in.ServiceName, Name: in.ServiceName,
Version: in.Metadata["version"], Version: in.Metadata["version"],
Metadata: in.Metadata, 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 return items, nil

@ -20,14 +20,16 @@ type watcher struct {
cancel context.CancelFunc cancel context.CancelFunc
watchChan chan struct{} watchChan chan struct{}
cli naming_client.INamingClient 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{ w := &watcher{
serviceName: serviceName, serviceName: serviceName,
clusters: clusters, clusters: clusters,
groupName: groupName, groupName: groupName,
cli: cli, cli: cli,
kind: kind,
watchChan: make(chan struct{}, 1), watchChan: make(chan struct{}, 1),
} }
w.ctx, w.cancel = context.WithCancel(ctx) 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)) items := make([]*registry.ServiceInstance, 0, len(res.Hosts))
for _, in := range res.Hosts { for _, in := range res.Hosts {
kind := w.kind
if k, ok := in.Metadata["kind"]; ok {
kind = k
}
items = append(items, &registry.ServiceInstance{ items = append(items, &registry.ServiceInstance{
ID: in.InstanceId, ID: in.InstanceId,
Name: res.Name, Name: res.Name,
Version: in.Metadata["version"], Version: in.Metadata["version"],
Metadata: in.Metadata, 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 return items, nil

Loading…
Cancel
Save