mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
feat(registry/nacos): add default kind option (#1650)
This commit is contained in:
parent
98fd40bcb4
commit
67161b62eb
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user