|
|
@ -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 |
|
|
|
} |
|
|
|
} |
|
|
|