.net core 健康检查 使用 HealthChecksUI 查看
基本健康检查 很简单
HealthStatus 状态 三种 为 HealthStatus.Healthy、HealthStatus.Degraded 或 HealthStatus.Unhealth 分别为健康,降级,不健康
//添加 健康检查
builder.Services.AddHealthChecks();
//添加 健康检查终结点
app.UseHealthChecks("/healthz");
using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using WebApplication1; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddHealthChecks(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHealthChecks("/healthz"); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
因为 终结点 注册为 /healthz 所以请求 localhost:7185/healthz 查看健康状态

这个有点简陋 弄个ui查看
nuget 下载
AspNetCore.HealthChecks.UI
AspNetCore.HealthChecks.UI.Client
AspNetCore.HealthChecks.UI.InMemory.Storage
再弄个 健康检查 mysql
Microsoft.EntityFrameworkCore
Pomelo.EntityFrameworkCore.MySql
实现 健康检查mysql 代码
using Microsoft.Extensions.Diagnostics.HealthChecks; using WebApplication1.Data; namespace WebApplication1 { public class CheckHealthMysql : IHealthCheck { private readonly MyDBContext myDBContext; public CheckHealthMysql(MyDBContext myDBContext) { this.myDBContext = myDBContext; } public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { if (!await myDBContext.Database.CanConnectAsync()) { return new HealthCheckResult(HealthStatus.Unhealthy,description:"mysql数据库连接断开了,不正常"); } return new HealthCheckResult(HealthStatus.Healthy,description:"mysql数据库正常"); } } }
添加 健康检查mysql 扩展
using HealthChecks.UI.Client; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.EntityFrameworkCore; using WebApplication1; using WebApplication1.Data; using Microsoft.Extensions.Diagnostics.HealthChecks; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); //添加 HealthChecks builder.Services.AddHealthChecks() .AddCheck("self", _ => new HealthCheckResult(HealthStatus.Healthy,"健康的")) .AddCheck<CheckHealthMysql>("testmysql"); //添加 HealthChecksUI builder.Services.AddHealthChecksUI().AddInMemoryStorage(); //添加mysql builder.Services.AddDbContext<MyDBContext>(options => { string con = builder.Configuration.GetConnectionString("con"); options.UseMySql(con, new MySqlServerVersion(new Version(8, 2, 26))); }); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } //增加 HealthChecks 中间件 app.UseHealthChecks("/healthz", new HealthCheckOptions { //谓词过滤 Predicate = _ => true, // 这里只展示名字为simple的健康检查 //Predicate = c => c.Name== "simple", //这里返回json格式 ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); //增加 HealthChecksUI 中间件 app.UseHealthChecksUI(setup: options => { //UI 访问路径 options.UIPath = "/health-ui"; }); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
以上是代码的组装
下面是 appsettings.json
HealthChecksUI json配置
"HealthChecksUI": {
"HealthChecks": [
{
"Name": "Test Health",
"Uri": "/healthz"
}
],
"EvaluationTimeinSeconds": 10,
"MinimumSecondsBetweenFailureNotifications": 30
}
以下配置全内容 注意这里为了测试健康检查 我并没有添加 mysql 数据库连接 字符串
也就是说 mysql 是不健康的 因为没有连接 到数据库
{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*", "HealthChecksUI": { "HealthChecks": [ { "Name": "Test Health", "Uri": "/healthz" } ], "EvaluationTimeinSeconds": 10, "MinimumSecondsBetweenFailureNotifications": 30 } }
开始测试
按照配置 url 地址为 https://localhost:7185/health-ui

结果正确
浙公网安备 33010602011771号