最新要闻

广告

手机

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

iphone11大小尺寸是多少?苹果iPhone11和iPhone13的区别是什么?

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

警方通报辅警执法直播中被撞飞:犯罪嫌疑人已投案

家电

ASP.NET Core优雅的添加HealthCheck_快看点

来源:博客园


(相关资料图)

ASP.NET Core 2.2 之后提供了内置的 Health Checks 来帮助您监视和报告应用程序的健康状况。默认情况是不需要在额外的引入其它nuget包的,因为AspNet Core自带了这些功能。

public void ConfigureServices(IServiceCollection services){    //使用该扩展方法    services.AddHealthChecks();}public void Configure(IApplicationBuilder app){    app.UseRouting();    app.UseEndpoints(endpoints =>    {        //使用该扩展方法        endpoints.MapHealthChecks("/health");    });}

进行请求"health"路径,就会得到下面的结果:

内置的AddHealthChecks功能比较简单,而现实场景我们是需要对各种指标进行检查的,redis,数据库,RabbitMQ,上游api等等。

而Nugget早已经有人造好轮子,大部分场景我们下载使用即可。

我们可以先添加AspNetCore.Diagnostics.HealthChecks基础包,然后需要添加那种检查,就添加对应的包即可。

services.AddHealthChecks().AddNpgSql("ConnectionString").AddRedis("ConnectionString").AddRabbitMQ("ConnectionString", null, "rabbitMQ");
app.UseEndpoints(endpoints =>{endpoints.MapControllers();//设置健康检查终结点endpoints.MapHealthChecks("/health", new HealthCheckOptions(){//设置返回格式ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse});});

接下来我们再次进行请求"health"路径,就会得到下面的结果:

如果还想给健康检查配置访问控制可以一般情况nuget包是够用的

Install-Package AspNetCore.HealthChecks.ApplicationStatusInstall-Package AspNetCore.HealthChecks.ArangoDbInstall-Package AspNetCore.HealthChecks.Aws.S3Install-Package AspNetCore.HealthChecks.Aws.SecretsManagerInstall-Package AspNetCore.HealthChecks.Aws.SnsInstall-Package AspNetCore.HealthChecks.Aws.SqsInstall-Package AspNetCore.HealthChecks.Aws.SystemsManagerInstall-Package AspNetCore.HealthChecks.Azure.IoTHubInstall-Package AspNetCore.HealthChecks.AzureDigitalTwinInstall-Package AspNetCore.HealthChecks.AzureKeyVaultInstall-Package AspNetCore.HealthChecks.AzureServiceBusInstall-Package AspNetCore.HealthChecks.AzureStorageInstall-Package AspNetCore.HealthChecks.ConsulInstall-Package AspNetCore.HealthChecks.CosmosDbInstall-Package AspNetCore.HealthChecks.DocumentDbInstall-Package AspNetCore.HealthChecks.DynamoDBInstall-Package AspNetCore.HealthChecks.ElasticsearchInstall-Package AspNetCore.HealthChecks.EventStoreInstall-Package AspNetCore.HealthChecks.EventStore.gRPCInstall-Package AspNetCore.HealthChecks.Gcp.CloudFirestoreInstall-Package AspNetCore.HealthChecks.GremlinInstall-Package AspNetCore.HealthChecks.HangfireInstall-Package AspNetCore.HealthChecks.IbmMQInstall-Package AspNetCore.HealthChecks.InfluxDBInstall-Package AspNetCore.HealthChecks.KafkaInstall-Package AspNetCore.HealthChecks.KubernetesInstall-Package AspNetCore.HealthChecks.MongoDbInstall-Package AspNetCore.HealthChecks.MySqlInstall-Package AspNetCore.HealthChecks.NatsInstall-Package AspNetCore.HealthChecks.NetworkInstall-Package AspNetCore.HealthChecks.NpgsqlInstall-Package AspNetCore.HealthChecks.OpenIdConnectServerInstall-Package AspNetCore.HealthChecks.OracleInstall-Package AspNetCore.HealthChecks.RabbitMQInstall-Package AspNetCore.HealthChecks.RavenDBInstall-Package AspNetCore.HealthChecks.RedisInstall-Package AspNetCore.HealthChecks.SendGridInstall-Package AspNetCore.HealthChecks.SignalRInstall-Package AspNetCore.HealthChecks.SolrInstall-Package AspNetCore.HealthChecks.SqLiteInstall-Package AspNetCore.HealthChecks.SqlServerInstall-Package AspNetCore.HealthChecks.SystemInstall-Package AspNetCore.HealthChecks.Uris

如果nuget没有符合你的包,可以自己实现

public class DatabaseHeaCheakCheck : IHealthCheck{public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default){try{// 你要检查的业务throw new ArgumentNullException();return HealthCheckResult.Healthy();}catch(Exception ex){return HealthCheckResult.Unhealthy(exception:ex );}}}
services.AddHealthChecks().AddCheck("Database").AddNpgSql("ConnectionString").AddRedis("ConnectionString").AddRabbitMQ("ConnectionString", null, "rabbitMQ");

接下来我们再次进行请求"health"路径,就会得到下面的结果:微软官方文档-ASP.NET Core 中的运行状况检查https://learn.microsoft.com/zh-cn/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-3.1#health-check-publisher

关键词: