《深入浅出.NET框架设计与实现》阅读笔记(三)

健康检查

HealthCheckMiddleware类

需要引用Microsoft.Extensions.Diagnostics.HealthChecks Nuget包,使用IHealthCheck接口来操作健康监测的方法。

注册健康检查所依赖的核心服务

在Program类

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks();
var app = builder.Build();
app.UseHealthChecks("/health");
app.Run();
  • 通过IServiceCollection 接口的AddHealthChecks扩展方法去注册。
  • 通过WebApplication 对象调用UseHealthChecks扩展方法来注入中间件,并将 /health 作为健康检查终节点指定的路径。

通过AddCheck()来自定义逻辑处理

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks().AddCheck("default",Check);
var app = builder.Build();
app.UseHealthChecks("/health");
app.Run();
HealthCheckResult Check()
{
    var aaa = new Random().Next(1,4);

    return aaa switch
    {
        1 => HealthCheckResult.Healthy(),
        2 => HealthCheckResult.Unhealthy(),
        3 => HealthCheckResult.Degraded(),
    };
}

通过多次刷新网址,可以看到返回不同的健康状态。

改变响应状态码

var healthCheckOpt = new HealthCheckOptions()
{
    ResultStatusCodes =
    {
        [HealthStatus.Healthy] = 298,
        [HealthStatus.Unhealthy] = 420,
        [HealthStatus.Degraded] = 299
    }
};
app.UseHealthChecks("/health",healthCheckOpt);

自定义响应内容

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
    .AddCheck("Foo", Check)
    .AddCheck("Bar",Check)
    .AddCheck("Baz",Check);
var app = builder.Build();
var healthCheckOpt = new HealthCheckOptions()
{
    ResponseWriter = ReportAsync
};
app.UseHealthChecks("/health",healthCheckOpt);
app.Run();

HealthCheckResult Check()
{
    var aaa = new Random().Next(1, 4);

    return aaa switch
    {
        1 => HealthCheckResult.Healthy(),
        2 => HealthCheckResult.Unhealthy(),
        3 => HealthCheckResult.Degraded(),
    };
}
static Task ReportAsync(HttpContext context,HealthReport report)
{
    var result = JsonConvert.SerializeObject(
        new
        {
            status = report.Status.ToString(),
            responseTimeStamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
            errors = report.Entries.Select(x=>new { key = x.Key,value = Enum.GetName(typeof(HealthStatus),x.Value.Status) })
        });
    context.Response.ContentType = MediaTypeNames.Application.Json;
    return context.Response.WriteAsync(result);
}

在这里插入图片描述

发布健康报告

通过IHealthCheckPublisher 接口实现自定义发送逻辑

 public class SimplePublisher : IHealthCheckPublisher
 {
     public Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
     {
         if(report.Status == HealthStatus.Healthy) Console.ForegroundColor = ConsoleColor.Green;
         else Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine($"{DateTime.UtcNow} Prob Status: {report.Status}");

         var sb = new StringBuilder();

         foreach (var name in report.Entries.Keys)
         {
             sb.AppendLine($"{name} : {report.Entries[name].Status}");
         }
         cancellationToken.ThrowIfCancellationRequested();
         Console.WriteLine(sb);
         Console.ResetColor();
         return Task.CompletedTask;
     }
 }

在Program中注册

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHealthChecks()
    .AddCheck("Foo", Check)
    .AddCheck("Bar",Check)
    .AddCheck("Baz",Check);
builder.Services.Configure<HealthCheckPublisherOptions>(options =>
{
    options.Delay = TimeSpan.FromSeconds(2);
    options.Period = TimeSpan.FromSeconds(5);
});
builder.Services.AddSingleton<IHealthCheckPublisher, SimplePublisher>();
var app = builder.Build();
var healthCheckOpt = new HealthCheckOptions()
{
    ResponseWriter = ReportAsync
};
app.UseHealthChecks("/health",healthCheckOpt);
app.Run();

HealthCheckResult Check()
{
    var aaa = new Random().Next(1, 4);

    return aaa switch
    {
        1 => HealthCheckResult.Healthy(),
        2 => HealthCheckResult.Unhealthy(),
        3 => HealthCheckResult.Degraded(),
    };
}

HealthCheckPublisherOptions对象

  • Delay : 服务启动后收集工作的延后时间
  • Period : 健康报告发布时间间隔
  • Predicate : 对IHealthCheck对象进行过滤
  • Timeout :对象超时时间
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baobao熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值