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" "strings"
"time" "time"
"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
"github.com/hashicorp/consul/api" "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{ asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
TCP: address, TCP: address,
Interval: fmt.Sprintf("%ds", c.healthcheckInterval), 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) err := c.cli.Agent().ServiceRegister(asr)
if err != nil { if err != nil {
return err return err
} }
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
if c.heartbeat { if c.heartbeat {
go func() { 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)) ticker := time.NewTicker(time.Second * time.Duration(c.healthcheckInterval))
defer ticker.Stop() defer ticker.Stop()
for { for {
select { select {
case <-ticker.C: 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(): case <-c.ctx.Done():
return return
} }

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

Loading…
Cancel
Save