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

Loading…
Cancel
Save