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

End
這個(gè)實(shí)現(xiàn)比較簡(jiǎn)單,只是實(shí)現(xiàn)一個(gè)比較簡(jiǎn)單的檢查,最初的想法比較簡(jiǎn)單只是看某個(gè)應(yīng)用是否正常工作,具體的檢查邏輯可以自定義。官方的 HealthChecks 的實(shí)現(xiàn)稍為復(fù)雜,下次單獨(dú)寫一篇文章介紹。
好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
asp.net Parameters.AddWithValue方法在SQL語句的 Where 字句中的用法
今天晚上看論壇,有人提問說,Parameters.AddWithValue方法在有些情況下不好使2009-01-01
Asp.NEt郵箱驗(yàn)證修改密碼通過郵箱找回密碼功能
這篇文章主要介紹了Asp.NEt郵箱驗(yàn)證修改密碼通過郵箱找回密碼功能,需要的朋友可以參考下2017-10-10
ASP.NET實(shí)現(xiàn)電影票信息的增刪查改功能
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)電影票信息的增刪查改功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
Ext.Net學(xué)習(xí)筆記之button小結(jié)
Ext.Net學(xué)習(xí)筆記之button小結(jié),需要的朋友可以參考一下2013-02-02


