.net core grpc 健康检查(二)
nuget 下载
下面是 HealthChecks.UI
AspNetCore.HealthChecks.UI
AspNetCore.HealthChecks.UI.Client
AspNetCore.HealthChecks.UI.InMemory.Storage
下面是 grpc 健康相关包
Grpc.AspNetCore.HealthChecks
appsettings.json HealthChecks.UI 配置
"HealthChecksUI": { "HealthChecks": [ { "Name": "HealthCheck", "Uri": "https://localhost:7252/healthz" } ], "EvaluationTimeinSeconds": 10, "MinimumSecondsBetweenFailureNotifications": 60 }
program 主要代码
using GrpcService1.data; using GrpcService1.Interceptors; using GrpcService1.Services; using HealthChecks.UI.Client; using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; using System.Reflection;
var builder = WebApplication.CreateBuilder(args); //基本 健康检查 builder.Services.AddHealthChecks();
builder.Services.AddGrpcHealthChecks().AddCheck("grpchealth", () => HealthCheckResult.Healthy("健康的")); builder.Services.AddHealthChecksUI().AddInMemoryStorage();
var app = builder.Build(); //健康检查 app.UseHealthChecks("/healthz", new Microsoft.AspNetCore.Diagnostics.HealthChecks.HealthCheckOptions { Predicate = _ => true, ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse }); app.MapGrpcHealthChecksService(); //健康检查ui app.UseHealthChecksUI(setup: options => { options.UIPath = "/health-ui"; }); app.Run();
测试 是否能 访问 grpchealth
根据 终结点配置 访问 https://localhost:7252/healthz

访问 HealthChecks.UI https://localhost:7252/health-ui

浙公网安备 33010602011771号