fix(registry/consul): fix can not find service in 20s (#1728)

* fix(registry/consul): fix can not find service in 20s
pull/1738/head
letian 3 years ago committed by GitHub
parent ed86f36476
commit 17201cd284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      contrib/registry/consul/client.go
  2. 9
      contrib/registry/consul/registry.go

@ -21,11 +21,17 @@ type Client struct {
// resolve service entry endpoints
resolver ServiceResolver
// healthcheck time interval in seconds
healthcheckInterval int
}
// NewClient creates consul client
func NewClient(cli *api.Client) *Client {
c := &Client{cli: cli, resolver: defaultResolver}
c := &Client{
cli: cli,
resolver: defaultResolver,
healthcheckInterval: 10,
}
c.ctx, c.cancel = context.WithCancel(context.Background())
return c
}
@ -107,7 +113,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
for _, address := range checkAddresses {
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
TCP: address,
Interval: "20s",
Interval: fmt.Sprintf("%ds", c.healthcheckInterval),
DeregisterCriticalServiceAfter: "70s",
})
}
@ -116,8 +122,9 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
if err != nil {
return err
}
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
go func() {
ticker := time.NewTicker(time.Second * 20)
ticker := time.NewTicker(time.Second * time.Duration(c.healthcheckInterval))
defer ticker.Stop()
for {
select {

@ -35,6 +35,15 @@ func WithServiceResolver(fn ServiceResolver) Option {
}
}
// WithHealthCheckInterval with healthcheck interval in seconds.
func WithHealthCheckInterval(interval int) Option {
return func(o *Registry) {
if o.cli != nil {
o.cli.healthcheckInterval = interval
}
}
}
// Config is consul registry config
type Config struct {
*api.Config

Loading…
Cancel
Save