日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

给 asp.net core 写一个简单的健康检查

發布時間:2023/12/4 asp.net 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 给 asp.net core 写一个简单的健康检查 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

給 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"

    數據庫連接改為一個錯誤的連接,修改數據庫名稱為一個不存在的數據庫

    End

    這個實現比較簡單,只是實現一個比較簡單的檢查,最初的想法比較簡單只是看某個應用是否正常工作,具體的檢查邏輯可以自定義。官方的 HealthChecks 的實現稍為復雜,下次單獨寫一篇文章介紹。


    總結

    以上是生活随笔為你收集整理的给 asp.net core 写一个简单的健康检查的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。