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, Func<IServiceProvider, bool> checkFunc) { return UseHealthCheck(applicationBuilder, new PathString(path), serviceProvider => Task.FromResult(checkFunc(serviceProvider))); } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, string path, Func<IServiceProvider, Task<bool>> checkFunc) { return UseHealthCheck(applicationBuilder, new PathString(path), checkFunc); } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, PathString path, Func<IServiceProvider, 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, Func<IServiceProvider, Task<bool>> 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.GetService<ILoggerFactory>().CreateLogger("HealthCheck").Error(ex); context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable; await context.Response.WriteAsync("unhealthy"); } })); return applicationBuilder; }
配置健康检查
在 Startup 里配置健康检查,示例代码
app.UseHealthCheck(); // 最基本的健康检查, 默认检查路径为 ""/api/health",直接返回 healthy app.UseHealthCheck("/heath"); // 配置健康检查的路径为 "/health",直接返回 healthy app.UseHealthCheck("/health", serviceProvider => { // 检查数据连接是否正常,这里只是一个示例,可以根据需要自定义自己的实现 var configuration = serviceProvider.GetService<IConfiguration>(); var connString = configuration.GetConnectionString("DefaultConnection"); try { using (var conn = new SqlConnection(connString)) { conn.EnsureOpen(); } return true; } catch (Exception) { return false; } });
实际效果
直接启动访问 "/health"
数据库连接改为一个错误的连接,修改数据库名称为一个不存在的数据库
End
这个实现比较简单,只是实现一个比较简单的检查,最初的想法比较简单只是看某个应用是否正常工作,具体的检查逻辑可以自定义。官方的 HealthChecks 的实现稍为复杂,下次单独写一篇文章介绍。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对站长技术的支持。
Java HashMap removeNode 方法 分析 源码分析仓库 https://github.com/HANXU2018...
步骤: 1、新建一个空文件,文件名为hhhh 2、初始化 git init 3、自己要与origin...
图片来自 Pexels 突然电话响了起来,一看是我们的一个开发同学,顿时紧张了起来...
vscode怎么浏览器打开html预览?这里大家可以通过安装open in browser插件解决。...
PHP+Mysql简单实现了图书购物车 本文主要讲述如何通过PHP+HTML简单实现图书购物...
mysql提供的模式匹配的其他类型是使用扩展正则表达式。 当你对这类模式进行匹配...
官网连接 https://docs.rt-thread.org/#/rt-thread-version/rt-thread-standard/...
1.小程序端代码示例 my.getPhoneNumber({ success: (res) = { let encryptedData...
1. 始终在 `v-for` 中使用 `:key` 在需要操纵数据时,将key属性与v-for指令一起...
有许多命令行选手在linux下开发的时候会经常遇到一个问题,无论是svn还是git,提...