您好,登錄后才能下訂單哦!
小編給大家分享一下怎么給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)
實(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ù)庫(kù)連接改為一個(gè)錯(cuò)誤的連接,修改數(shù)據(jù)庫(kù)名稱為一個(gè)不存在的數(shù)據(jù)庫(kù)
以上是“怎么給asp.net core寫個(gè)簡(jiǎn)單的健康檢查”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。