mirror of
https://github.com/go-kratos/kratos.git
synced 2025-02-21 19:19:32 +02:00
fix(registry/consul): fix can not find service in 20s (#1728)
* fix(registry/consul): fix can not find service in 20s
This commit is contained in:
parent
ed86f36476
commit
17201cd284
@ -21,11 +21,17 @@ type Client struct {
|
||||
|
||||
// resolve service entry endpoints
|
||||
resolver ServiceResolver
|
||||
// healthcheck time interval in seconds
|
||||
healthcheckInterval int
|
||||
}
|
||||
|
||||
// NewClient creates consul 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())
|
||||
return c
|
||||
}
|
||||
@ -107,7 +113,7 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
|
||||
for _, address := range checkAddresses {
|
||||
asr.Checks = append(asr.Checks, &api.AgentServiceCheck{
|
||||
TCP: address,
|
||||
Interval: "20s",
|
||||
Interval: fmt.Sprintf("%ds", c.healthcheckInterval),
|
||||
DeregisterCriticalServiceAfter: "70s",
|
||||
})
|
||||
}
|
||||
@ -116,8 +122,9 @@ func (c *Client) Register(_ context.Context, svc *registry.ServiceInstance, enab
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = c.cli.Agent().UpdateTTL("service:"+svc.ID, "pass", "pass")
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Second * 20)
|
||||
ticker := time.NewTicker(time.Second * time.Duration(c.healthcheckInterval))
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
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
|
||||
type Config struct {
|
||||
*api.Config
|
||||
|
Loading…
x
Reference in New Issue
Block a user