我正在尝试利用Consul .NET API来注册并通过TTL触发运行状况检查。首先,我使用以下代码注册我的服务:
var address = node.Address;
var id = ServiceId(address);
var registration = new AgentServiceRegistration
{
ID = id,
Name = node.ClusterName,
Address = node.Address.Host,
Port = node.Address.Port.Value,
Check = new AgentServiceCheck
{
TTL = settings.AliveInterval, // 10sec
DeregisterCriticalServiceAfter = settings.AliveTimeout, // 60sec
}
};
// first, try to deregister service, if it has been registered previously
await consul.Agent.ServiceDeregister(registration.ID);
await consul.Agent.ServiceRegister(registration);
之后,我正尝试通过以下方式触发TTL:
await consul.Agent.PassTTL("service:" + ServiceId(addr), string.Empty);
但是,我最终遇到的是PassTTL期间引发的异常: Consul.ConsulRequestException:意外响应,状态码InternalServerError:CheckID“service:{service-id}”没有关联的TTL
以及来自领事代理本身的相关日志:
我想知道我在这里做错了什么。
我正在使用
consul agent -dev
(版本:1.0.1)和Nuget包Consul(版本:0.7.2.3)。 最佳答案
原来AgentServiceRegistration.Check
属性几乎没有用。我已经使用CheckRegister
方法达到了预期的结果。
这是代码
var registration = new AgentServiceRegistration
{
ID = "serviceId",
Name = node.ClusterName,
Address = node.Address.Host,
Port = node.Address.Port.Value
};
// first, try to deregister service, if it has been registered previously
await consul.Agent.ServiceDeregister(registration.ID);
await consul.Agent.ServiceRegister(registration);
await consul.Agent.CheckRegister(new AgentCheckRegistration()
{
ID = "checkId",
Name = "Check Name",
Status = HealthStatus.Passing,
TTL = settings.AliveInterval,
ServiceID = "serviceId",
DeregisterCriticalServiceAfter = settings.AliveTimeout, // 60sec
})
现在您可以通过
await consul.Agent.PassTTL("checkId", string.Empty);
请务必确保之后注销您的支票
关于c# - CheckID没有关联的TTL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47463060/