c# - 在 .NET Core Worker 服务中执行健康检查
问题描述
如何在 .NET Core Worker 服务中实现运行状况检查?
该服务将在 Docker 内部运行,并且需要能够检查服务的运行状况。
解决方案
另一种方法是实现IHealthCheckPublisher
.
这种方法的好处是能够重用您现有IHealthCheck
的 s 或与依赖IHealthCheck
接口的 3rd 方库集成(例如这个)。
尽管您仍然Microsoft.NET.Sdk.Web
以 SDK 为目标,但您不需要添加任何 asp.net 细节。
这是一个例子:
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host
.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services
.AddHealthChecks()
.AddCheck<RedisHealthCheck>("redis_health_check")
.AddCheck<RfaHealthCheck>("rfa_health_check");
services.AddSingleton<IHealthCheckPublisher, HealthCheckPublisher>();
services.Configure<HealthCheckPublisherOptions>(options =>
{
options.Delay = TimeSpan.FromSeconds(5);
options.Period = TimeSpan.FromSeconds(5);
});
});
}
public class HealthCheckPublisher : IHealthCheckPublisher
{
private readonly string _fileName;
private HealthStatus _prevStatus = HealthStatus.Unhealthy;
public HealthCheckPublisher()
{
_fileName = Environment.GetEnvironmentVariable(EnvVariableNames.DOCKER_HEALTHCHECK_FILEPATH) ??
Path.GetTempFileName();
}
public Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
{
// AWS will check if the file exists inside of the container with the command
// test -f $DOCKER_HEALTH_CHECK_FILEPATH
var fileExists = _prevStatus == HealthStatus.Healthy;
if (report.Status == HealthStatus.Healthy)
{
if (!fileExists)
{
using var _ = File.Create(_fileName);
}
}
else if (fileExists)
{
File.Delete(_fileName);
}
_prevStatus = report.Status;
return Task.CompletedTask;
}
}
推荐阅读
- c - 有人可以告诉我为什么这不起作用吗?
- facebook - Facebook 和 Instagram,在自己的网站上获取和显示自己的帖子
- shell - 退出tmux后如何自动退出终端
- stm32f7 - 我可以将 c++ 与 stm32f7 一起使用吗?
- docker - 在 Dockerfile 中设置非 root 用户的环境,使其修改后的版本在构建时可用
- c# - 为什么在基于身份 Cookie 的身份验证中一段时间后 User.Identity.IsAuthenticated 设置为 false
- ios - 如何处理 JSONDecoder 中的空日期字符串
- android - 如何知道热重载是否足以应用 Flutter 应用程序中的更改?
- google-cloud-platform - 通过 generate_signed_url 函数访问图像时出现 403 错误
- python - 如何在 matplotlib 的每个条形顶部显示条形值?