|
|
@ -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, ®istry.ServiceInstance{ |
|
|
|
items = append(items, ®istry.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 |
|
|
|