获取健康状态并记录- YARP



我正在使用反向代理YARP与端点通信。我需要检查健康状态(如工作正常,停机时间,瞬态问题等)。我遵循了以下YARP文档中提到的配置:

https://microsoft.github.io/reverse-proxy/articles/dests-health-checks.html available-destination-collection

应用程序设置中用于健康检查的配置。json是:

"Clusters": {
"abc": {
"HealthCheck": {
"Active": {
"Enabled": "true",
"Interval": "00:00:10",
"Timeout": "00:00:10",
"Policy": "ConsecutiveFailures",
"Path": "/test"
}
},
"Metadata": {
"ConsecutiveFailuresHealthPolicy.Threshold": "3"
},
"Destinations": {
"abc": {
"Address": "http://localhost:5500/abc",
"Health": "http://localhost:5500/"
}
}
},
"api": {
"Destinations": {
"api": {
"Address": "http://localhost:5600/"
}
}
}
}

我能够在'abc'结束点上点击动作。但是,由于我想记录状态,所以我编写了下面的类,按照上面给出的链接中的示例编写。

using Yarp.ReverseProxy.Health;
using Yarp.ReverseProxy.Model;
namespace YarpReverseProxyServer
{
public class HealthResponsePolicy : IActiveHealthCheckPolicy
{
private readonly IDestinationHealthUpdater _healthUpdater;
public HealthResponsePolicy(IDestinationHealthUpdater healthUpdater)
{
_healthUpdater = healthUpdater;
}
public string Name => "HealthResponsePolicy";
public void ProbingCompleted(ClusterState cluster, IReadOnlyList<DestinationProbingResult> probingResults)
{
if (probingResults.Count == 0)
{
return;
}
var newHealthStates = new NewActiveDestinationHealth[probingResults.Count];
for (var i = 0; i < probingResults.Count; i++)
{
var response = probingResults[i].Response;
var newHealth = response is not null && response.IsSuccessStatusCode ? DestinationHealth.Healthy : DestinationHealth.Unhealthy;
newHealthStates[i] = new NewActiveDestinationHealth(probingResults[i].Destination, newHealth);
}
_healthUpdater.SetActive(cluster, newHealthStates);
}
}
}

,然后在Startup.cs中添加以下行,尽管上面的链接中没有提到:

services.AddSingleton<IActiveHealthCheckPolicy, HealthResponsePolicy>();

然而,这不起作用,因为代码永远不会命中HealthResponsePolicy类的ProbingCompleted方法。我不明白我错过了什么。

我已经找到问题所在了。我必须将以下策略设置为应用程序设置中的自定义类HealthResponsePolicy。

"Policy": "HealthResponsePolicy"

相关内容

  • 没有找到相关文章

最新更新