小編給大家分享一下怎么給asp.net core寫個(gè)簡單的健康檢查,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
網(wǎng)站的建設(shè)創(chuàng)新互聯(lián)公司專注網(wǎng)站定制,經(jīng)驗(yàn)豐富,不做模板,主營網(wǎng)站定制開發(fā).小程序定制開發(fā),H5頁面制作!給你煥然一新的設(shè)計(jì)體驗(yàn)!已為成都小攪拌車等企業(yè)提供專業(yè)服務(wù)。Intro
健康檢查可以幫助我們知道應(yīng)用的當(dāng)前狀態(tài)是不是處于良好狀態(tài),現(xiàn)在無論是 docker 還是 k8s 還是現(xiàn)在大多數(shù)的服務(wù)注冊發(fā)現(xiàn)大多都提供了健康檢查機(jī)制來檢測應(yīng)用的健康狀態(tài),如果應(yīng)用本身就提供一個(gè)健康檢查的機(jī)制會更友好,更能真實(shí)的反映出應(yīng)用的健康狀態(tài)。
我們的開發(fā)環(huán)境虛擬機(jī)配置有點(diǎn)低,所以有時(shí)候虛擬機(jī)會卡死。。導(dǎo)致接口無響應(yīng),有時(shí)可能有些服務(wù)啟動有問題會掛掉,所以需要一個(gè)簡單的健康檢查機(jī)制去檢查應(yīng)用的健康狀態(tài)來第一時(shí)間知道應(yīng)用出現(xiàn)異常。
健康檢查擴(kuò)展實(shí)現(xiàn)
實(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, FunccheckFunc) { return UseHealthCheck(applicationBuilder, new PathString(path), serviceProvider => Task.FromResult(checkFunc(serviceProvider))); } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, string path, Func > checkFunc) { return UseHealthCheck(applicationBuilder, new PathString(path), checkFunc); } public static IApplicationBuilder UseHealthCheck(this IApplicationBuilder applicationBuilder, PathString path, Func 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 > 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 ().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(); var connString = configuration.GetConnectionString("DefaultConnection"); try { using (var conn = new SqlConnection(connString)) { conn.EnsureOpen(); } return true; } catch (Exception) { return false; } });
實(shí)際效果
直接啟動訪問 "/health"
數(shù)據(jù)庫連接改為一個(gè)錯(cuò)誤的連接,修改數(shù)據(jù)庫名稱為一個(gè)不存在的數(shù)據(jù)庫
以上是“怎么給asp.net core寫個(gè)簡單的健康檢查”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!