小编给大家分享一下怎么给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, 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"
数据库连接改为一个错误的连接,修改数据库名称为一个不存在的数据库
以上是“怎么给asp.net core写个简单的健康检查”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。