Compare commits

...

1 Commits

Author SHA1 Message Date
baozhecheng 496f663424 fix: watch failure caused instance list not to be updated 1 year ago
  1. 4
      contrib/registry/consul/client.go
  2. 11
      contrib/registry/consul/registry.go

@ -41,6 +41,8 @@ type Client struct {
deregisterCriticalServiceAfter int deregisterCriticalServiceAfter int
// serviceChecks user custom checks // serviceChecks user custom checks
serviceChecks api.AgentServiceChecks serviceChecks api.AgentServiceChecks
// reRegistry re-registry when service is deregistered
reRegistry bool
} }
func defaultResolver(_ context.Context, entries []*api.ServiceEntry) []*registry.ServiceInstance { func defaultResolver(_ context.Context, entries []*api.ServiceEntry) []*registry.ServiceInstance {
@ -223,6 +225,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
err = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass") err = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
if err != nil { if err != nil {
log.Errorf("[Consul] update ttl heartbeat to consul failed! err=%v", err) log.Errorf("[Consul] update ttl heartbeat to consul failed! err=%v", err)
if c.reRegistry {
// when the previous report fails, try to re register the service // when the previous report fails, try to re register the service
time.Sleep(time.Duration(rand.Intn(5)) * time.Second) time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
if err := c.cli.Agent().ServiceRegister(asr); err != nil { if err := c.cli.Agent().ServiceRegister(asr); err != nil {
@ -233,6 +236,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
} }
} }
} }
}
}() }()
} }
return nil return nil

@ -86,6 +86,13 @@ func WithServiceCheck(checks ...*api.AgentServiceCheck) Option {
} }
} }
// WithReRegistry re-registry when service is deregistered
func WithReRegistry() Option {
return func(r *Registry) {
r.cli.reRegistry = true
}
}
// Config is consul registry config // Config is consul registry config
type Config struct { type Config struct {
*api.Config *api.Config
@ -190,7 +197,6 @@ func (r *Registry) Watch(ctx context.Context, name string) (registry.Watcher, er
services: &atomic.Value{}, services: &atomic.Value{},
serviceName: name, serviceName: name,
} }
r.registry[name] = set
} }
// init watcher // init watcher
@ -215,6 +221,9 @@ func (r *Registry) Watch(ctx context.Context, name string) (registry.Watcher, er
return nil, err return nil, err
} }
} }
r.registry[name] = set
return w, nil return w, nil
} }

Loading…
Cancel
Save