如何給asp.net core寫個簡單的健康檢查
Intro
健康檢查可以幫助我們知道應用的當前狀態(tài)是不是處于良好狀態(tài),現(xiàn)在無論是 docker 還是 k8s 還是現(xiàn)在大多數(shù)的服務注冊發(fā)現(xiàn)大多都提供了健康檢查機制來檢測應用的健康狀態(tài),如果應用本身就提供一個健康檢查的機制會更友好,更能真實的反映出應用的健康狀態(tài)。
我們的開發(fā)環(huán)境虛擬機配置有點低,所以有時候虛擬機會卡死。。導致接口無響應,有時可能有些服務啟動有問題會掛掉,所以需要一個簡單的健康檢查機制去檢查應用的健康狀態(tài)來第一時間知道應用出現(xiàn)異常。
健康檢查擴展實現(xiàn)
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 =>
{
// 檢查數(shù)據(jù)連接是否正常,這里只是一個示例,可以根據(jù)需要自定義自己的實現(xiàn)
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"
數(shù)據(jù)庫連接改為一個錯誤的連接,修改數(shù)據(jù)庫名稱為一個不存在的數(shù)據(jù)庫

End
這個實現(xiàn)比較簡單,只是實現(xiàn)一個比較簡單的檢查,最初的想法比較簡單只是看某個應用是否正常工作,具體的檢查邏輯可以自定義。官方的 HealthChecks 的實現(xiàn)稍為復雜,下次單獨寫一篇文章介紹。
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。
相關文章
使用vs2019加.net core 對WeiApi的創(chuàng)建過程詳解
這篇文章主要介紹了使用vs2019加.net core 對WeiApi的創(chuàng)建,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
.NET CORE中使用AutoMapper進行對象映射的方法
這篇文章主要給大家介紹了關于.NET CORE中使用AutoMapper進行對象映射的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用.NET CORE具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-04-04
IIS上部署你的ASP.NET?Core?Web?Api項目及Swagger(圖文)
本篇經(jīng)驗將和大家介紹如何在IIS上部署ASP.NET?Core項目,對大家的學習或者工作具有一定的參考學習價值,希望為初學.NET?CORE的童靴入門有所幫助2023-09-09


