fix: consul heartbeat ttl not registered (#1781)

* fix: consul heartbeat ttl check not registered
pull/1783/head
longxboy 3 years ago committed by GitHub
parent c250958af6
commit a87abe223c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      contrib/registry/consul/client.go
  2. 1
      contrib/registry/consul/registry.go

@ -9,7 +9,9 @@ import (
"strings"
"time"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
"github.com/hashicorp/consul/api"
)
@ -117,23 +119,39 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
TCP: address,
Interval: fmt.Sprintf("%ds", c.healthcheckInterval),
DeregisterCriticalServiceAfter: "70s",
DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.healthcheckInterval*60),
Timeout: "5s",
})
}
}
if c.heartbeat {
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
CheckID: "service:" + svc.ID,
TTL: fmt.Sprintf("%ds", c.healthcheckInterval*2),
DeregisterCriticalServiceAfter: fmt.Sprintf("%ds", c.healthcheckInterval*60),
})
}
err := c.cli.Agent().ServiceRegister(asr)
if err != nil {
return err
}
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
if c.heartbeat {
go func() {
time.Sleep(time.Second)
err = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
if err != nil {
log.Errorf("[Consul]update ttl heartbeat to consul failed!err:=%v", err)
}
ticker := time.NewTicker(time.Second * time.Duration(c.healthcheckInterval))
defer ticker.Stop()
for {
select {
case <-ticker.C:
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
err = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
if err != nil {
log.Errorf("[Consul]update ttl heartbeat to consul failed!err:=%v", err)
}
case <-c.ctx.Done():
return
}

@ -8,6 +8,7 @@ import (
"time"
"github.com/go-kratos/kratos/v2/registry"
"github.com/hashicorp/consul/api"
)

Loading…
Cancel
Save