如何给asp.net core写个简单的健康检查
程序员文章站
2023-12-04 23:22:46
intro
健康检查可以帮助我们知道应用的当前状态是不是处于良好状态,现在无论是 docker 还是 k8s 还是现在大多数的服务注册发现大多都提供了健康检查机制来检...
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 的实现稍为复杂,下次单独写一篇文章介绍。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。
上一篇: window.open()弹出居中的窗口