feat(registry): consul get service support remote (#2275)

* feat(registry): consul get service support remote acces

* fix lint
pull/2096/merge
包子 2 years ago committed by GitHub
parent b9b7888d51
commit 84235462b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      contrib/registry/consul/registry.go

@ -100,19 +100,33 @@ func (r *Registry) Deregister(ctx context.Context, svc *registry.ServiceInstance
}
// GetService return service by name
func (r *Registry) GetService(ctx context.Context, name string) (services []*registry.ServiceInstance, err error) {
func (r *Registry) GetService(ctx context.Context, name string) ([]*registry.ServiceInstance, error) {
r.lock.RLock()
defer r.lock.RUnlock()
set := r.registry[name]
getRemote := func() []*registry.ServiceInstance {
services, _, err := r.cli.Service(ctx, name, 0, true)
if err == nil && len(services) > 0 {
return services
}
return nil
}
if set == nil {
if s := getRemote(); len(s) > 0 {
return s, nil
}
return nil, fmt.Errorf("service %s not resolved in registry", name)
}
ss, _ := set.services.Load().([]*registry.ServiceInstance)
if ss == nil {
if s := getRemote(); len(s) > 0 {
return s, nil
}
return nil, fmt.Errorf("service %s not found in registry", name)
}
services = append(services, ss...)
return
return ss, nil
}
// ListServices return service list.

Loading…
Cancel
Save