close
參考文件:
1.新增套件(未來在dotent core 2.2會直接有)
dotnet add package Microsoft.AspNetCore.HealthChecks --version 1.0.0
2.為每個微服務呼叫一個 AddUrlCheck 方法
位置: Startup.cs > ConfigureServices
假設有一個相依的微服務網址為http://localhost:805
services.AddHealthChecks(checks =>{
checks.AddUrlCheck("http://localhost:805");
});
3.讓服務或 Web 應用程式公開健康狀態檢查端點
啟用 UseHealthChecks([url_for_health_checks]) 擴充方法。
位置:Program.cs
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseHealthChecks("/hc") //啟用 UseHealthChecks擴充方法。
.Build();
4.測試情況
http://localhost:56432/hc (假設啟用服務為localhost:56432)
健康
{"status":"Healthy"}
不健康
{"status":"Unhealthy"}
其它1.自訂檢查
自訂redis的連線檢查可以參考下面範例
services.AddHealthChecks(checks =>{
checks
.AddRedisCheck() //redis檢查
.AddMongoCheck() //mongo檢查
.AddUrlCheck("http://localhost:805") //apiproxy服務檢查
;
});
其它2.顯示詳細錯誤
因為預設對外端口只會顯示Healthy或Unhealthy兩種資訊,如果需要觀察詳細錯誤訊息可以參考下面範例
讀取checkResult的資料可顯示詳細訊息
{ "CheckStatus": 2, "Description": "RedisCheck: RedisCheck: Healthy\r\nMongoCheck: MongoCheck: Healthy\r\nUrlCheck(http://localhost:805): status code OK (200)", "Data": { "RedisCheck": {}, "MongoCheck": {}, "UrlCheck(http://localhost:805)": { "url": "http://localhost:805/", "status": 200, "reason": "OK", "body": "OK" } }, "Results": { "RedisCheck": { "CheckStatus": 2, "Data": {}, "Description": "RedisCheck: Healthy" }, "MongoCheck": { "CheckStatus": 2, "Data": {}, "Description": "MongoCheck: Healthy" }, "UrlCheck(http://localhost:805)": { "CheckStatus": 2, "Data": { "url": "http://localhost:805/", "status": 200, "reason": "OK", "body": "" }, "Description": "status code OK (200)" } } }
全站熱搜
留言列表