环球ug充值:.Net Core基础的健康检查

admin 4个月前 (06-18) 科技 49 0

前言

康健检查能查看我们的应用程序当前是否是一个康健的运行状态。微软已经给我们提供了康健检查轮子,只需要简朴的设置就能完成服务的状态检查。一起来实现一个最简朴的康健检查吧。

最先

  • 新建一个空的webApi项目。 并引用Microsoft.Extensions.Diagnostics.HealthChecks 包。并在ConfigureServicesConfigure中加入相关设置
public void ConfigureServices(IServiceCollection services)
{
    //康健检查服务
    services.AddHealthChecks();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //加入中间件
    app.UseHealthChecks("/healthChecks");
}

最简朴的检查就完成了,我们测试一下。

返Healthy,示意服务正常。

自定义拓展

HealthChecks提供了一个IHealthCheck接口,这个接口只有一个CheckHealthAsync方式,我们只需要实现这个接口就可以实现我们需要的种种自定义的检查项目。CheckHealthAsync返回一个HealthCheckResult的枚举代表康健检查的几种状态,分别是异常,降级,康健。

public enum HealthStatus
{
    Unhealthy = 0,
    Degraded = 1,
    Healthy = 2,
}

实现接口,返回不康健状态。

public class SqlHealthChecks : IHealthCheck
{
    public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = new CancellationToken())
    {
        if (1 == 1)
        {
            return Task.FromResult(HealthCheckResult.Unhealthy());
        }
    }
}

ConfigureServices中添加自定义的检查,AddCheck可以添加你自定的康健检查服务,

public void ConfigureServices(IServiceCollection services)
{
    //康健检查服务
    services.AddHealthChecks().AddCheck<SqlHealthChecks>("key");
}

测试可以发现返回的为不康健的应用

自定义返回值

我们可以行使HealthCheckOptions来实现康健检查的自定义返回内容.

private static Task WriteResponse(HttpContext context, HealthReport healthReport)
{
    context.Response.ContentType = "application/json";
    var result = JsonHelper.SerializeObject(new
    {
        code = context.Response.StatusCode,
        errors = healthReport.Entries.Select(e => new
        {
            key = e.Key,
            value = e.Value.Status.ToString()
        })
    });

    return context.Response.WriteAsync(result);
}


public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime appLifetime)
{

    app.UseHealthChecks("/healthChecks", new HealthCheckOptions{ResponseWriter = WriteResponse});

}

测试返回效果

引入Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore 可以为DbContext举行检查


public void ConfigureServices(IServiceCollection services)
{
    services.AddHealthChecks().AddCheck<SqlHealthChecks>("key").AddDbContextCheck<DbContext>("DbContext");
}

康健检查UI

引入AspNetCore.HealthChecks.UI并在ConfigureServicesConfigure中加入响应的设置

public void ConfigureServices(IServiceCollection services)  
{  
    services.AddHealthChecksUI();  
}  

public void Configure(IApplicationBuilder app, IHostingEnvironment env)  
{  
    app.UseHealthChecksUI();  
}  

appsetting,json文件中加入设置

{
  "HealthChecksUI": {
    "HealthChecks": [
      {
        "Name": "HealthCheck",
        "Uri": "https://localhost:5000/healthCheck"
      }
    ],
    "EvaluationTimeinSeconds": 10,
    "MinimumSecondsBetweenFailureNotifications": 60
  }
}

启动项目并指向/healthchecks-ui。

扩展包

开源社区已经有许多现有的优异的扩展包我们可以直接引用

AspNetCore.HealthChecks.Npgsql
AspNetCore.HealthChecks.Redis
AspNetCore.HealthChecks.AzureStorage
AspNetCore.HealthChecks.AzureServiceBus
AspNetCore.HealthChecks.MySql
AspNetCore.HealthChecks.DocumentDb
AspNetCore.HealthChecks.SqLite
AspNetCore.HealthChecks.Kafka
AspNetCore.HealthChecks.RabbitMQ
AspNetCore.HealthChecks.IdSvr
AspNetCore.HealthChecks.DynamoDB
AspNetCore.HealthChecks.Oracle
AspNetCore.HealthChecks.Uris
AspNetCore.HealthChecks.System
AspNetCore.HealthChecks.Network
AspNetCore.HealthChecks.SqlServer
AspNetCore.HealthChecks.MongoDb

参考文章

  • 微软官方文档
  • 社区
  • 源码明白HealthCheck

总结

实现了一个最简朴的康健检查功效,可以在这个基础上举行自定义的扩展和开发。

,

欧博Allbet

欢迎进入欧博Allbet官网(Allbet Game):www.aLLbetgame.us,欧博官网是欧博集团的官方网站。欧博官网开放Allbet注册、Allbe代理、Allbet电脑客户端、Allbet手机版下载等业务。

Sunbet声明:该文看法仅代表作者自己,与本平台无关。转载请注明:环球ug充值:.Net Core基础的健康检查

网友评论

  • (*)

最新评论

标签列表

    文章归档

      站点信息

      • 文章总数:671
      • 页面总数:0
      • 分类总数:8
      • 标签总数:1160
      • 评论总数:283
      • 浏览总数:12748