diff --git a/contrib/registry/consul/client.go b/contrib/registry/consul/client.go index b5e19a17a..ed505974a 100644 --- a/contrib/registry/consul/client.go +++ b/contrib/registry/consul/client.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 } diff --git a/contrib/registry/consul/registry.go b/contrib/registry/consul/registry.go index 36b648598..c3bc25466 100644 --- a/contrib/registry/consul/registry.go +++ b/contrib/registry/consul/registry.go @@ -8,6 +8,7 @@ import ( "time" "github.com/go-kratos/kratos/v2/registry" + "github.com/hashicorp/consul/api" )