网站建设促销活动,城市形象设计vi手册,域名和空间的定义,千峰培训多少钱给 asp.net core 写一个简单的健康检查Intro健康检查可以帮助我们知道应用的当前状态是不是处于良好状态#xff0c;现在无论是 docker 还是 k8s 还是现在大多数的服务注册发现大多都提供了健康检查机制来检测应用的健康状态#xff0c;如果应用本身就提供一个健康检查的机制… 给 asp.net core 写一个简单的健康检查Intro健康检查可以帮助我们知道应用的当前状态是不是处于良好状态现在无论是 docker 还是 k8s 还是现在大多数的服务注册发现大多都提供了健康检查机制来检测应用的健康状态如果应用本身就提供一个健康检查的机制会更友好更能真实的反映出应用的健康状态。我们的开发环境虚拟机配置有点低所以有时候虚拟机会卡死。。导致接口无响应有时可能有些服务启动有问题会挂掉所以需要一个简单的健康检查机制去检查应用的健康状态来第一时间知道应用出现异常。健康检查扩展实现实现源码 public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder) { return UseHealthCheck(applicationBuilder, new PathString(/api/health)); } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, string path) { return UseHealthCheck(applicationBuilder, new PathString(path)); } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, PathString path) { applicationBuilder.Map(path, builder builder.Use( (context, next) { context.Response.StatusCode 200; return context.Response.WriteAsync(healthy); })); return applicationBuilder; } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, string path, FuncIServiceProvider, bool checkFunc) { return UseHealthCheck(applicationBuilder, new PathString(path), serviceProvider Task.FromResult(checkFunc(serviceProvider))); } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, string path, FuncIServiceProvider, Taskbool checkFunc) { return UseHealthCheck(applicationBuilder, new PathString(path), checkFunc); } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, PathString path, FuncIServiceProvider, bool checkFunc) { if (checkFunc null) { checkFunc serviceProvider true; } return UseHealthCheck(applicationBuilder, path, serviceProvider Task.FromResult(checkFunc(serviceProvider))); } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, PathString path, FuncIServiceProvider, Taskbool checkFunc) { if (checkFunc null) { checkFunc serviceProvider Task.FromResult(true); } applicationBuilder.Map(path, builder builder.Use( async (context, next) { try { var healthy await checkFunc.Invoke(context.RequestServices); if (healthy) { context.Response.StatusCode StatusCodes.Status200OK; await context.Response.WriteAsync(healthy); } else { context.Response.StatusCode StatusCodes.Status503ServiceUnavailable; await context.Response.WriteAsync(unhealthy); } } catch (Exception ex) { context.RequestServices.GetServiceILoggerFactory().CreateLogger(HealthCheck).Error(ex); context.Response.StatusCode StatusCodes.Status503ServiceUnavailable; await context.Response.WriteAsync(unhealthy); } })); return applicationBuilder; }配置健康检查在 Startup 里配置健康检查示例代码app.UseHealthCheck(); // 最基本的健康检查 默认检查路径为 /api/health直接返回 healthyapp.UseHealthCheck(/heath); // 配置健康检查的路径为 /health直接返回 healthyapp.UseHealthCheck(/health, serviceProvider { // 检查数据连接是否正常这里只是一个示例可以根据需要自定义自己的实现 var configuration serviceProvider.GetServiceIConfiguration(); var connString configuration.GetConnectionString(DefaultConnection); try { using (var conn new SqlConnection(connString)) { conn.EnsureOpen(); } return true; } catch (Exception) { return false; } });实际效果直接启动访问 /health数据库连接改为一个错误的连接修改数据库名称为一个不存在的数据库End这个实现比较简单只是实现一个比较简单的检查最初的想法比较简单只是看某个应用是否正常工作具体的检查逻辑可以自定义。官方的 HealthChecks 的实现稍为复杂下次单独写一篇文章介绍。