ASP.NET CORE 健康檢查

首先創(chuàng)建一個空的web api項(xiàng)目


添加引用
https://www.nuget.org/packages/Microsoft.AspNetCore.Diagnostics.HealthChecks

Startup文件中添加相應(yīng)代碼

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddHealthChecks();//添加服務(wù)
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
            app.UseHealthChecks("/health");//配置訪問URL
        }

官方支持如下多種檢查
Sql Server
MySql
Oracle
Sqlite
RavenDB
Postgres
EventStore
RabbitMQ
Elasticsearch
Redis
System: Disk Storage, Private Memory, Virtual Memory
Azure Service Bus: EventHub, Queue and Topics
Azure Storage: Blob, Queue and Table
Azure Key Vault
Azure DocumentDb
Amazon DynamoDb
Amazon S3
Network: Ftp, SFtp, Dns, Tcp port, Smtp, Imap
MongoDB
Kafka
Identity Server
Uri: single uri and uri groups
Consul
Hangfire
SignalR

首先我們添加mysql檢查
https://github.com/Xabaril/AspNetCore.Diagnostics.HealthChecks
官方文檔說明 使用了myget來管理HealthChecks相關(guān)的包,所以我們首先要添加源到NUGET管理里來
We use MyGet feed for preview versions of HealthChecks pacakges.

VS-工具-選項(xiàng)


添加以后點(diǎn)擊更新 確定就可以進(jìn)行安裝了


在當(dāng)前管理界面右上角選擇自己添加的源進(jìn)行搜索

我們選擇第三個進(jìn)行安裝

進(jìn)行數(shù)據(jù)庫連接的配置

首先運(yùn)行mysql服務(wù)


image.png

連接正常 配置相關(guān)數(shù)據(jù)庫連接和代碼設(shè)置


 services
.AddHealthChecks()
.AddMySql(Configuration["ConnectionStrings:DefaultConnection"];

運(yùn)行訪問



如果健康則顯示Healthy不健康為Unhealthy



不過這樣并不是很明顯,因此還需要添加一款更直觀的UI進(jìn)行展示。
搜索剛才添加的源進(jìn)行安裝



添加配置代碼

services.AddHealthChecksUI();

app.UseHealthChecksUI();

This automatically registers a new interface on /healthchecks-ui where the spa will be served.
已經(jīng)自動注冊了地址了

源碼相關(guān)默認(rèn)配置

namespace HealthChecks.UI.Configuration
{
    public class Options
    {
        public string UIPath { get; set; } = "/healthchecks-ui";
        public string ApiPath { get; set; } = "/healthchecks-api";
        public bool UseRelativeApiPath = true;
        public string WebhookPath { get; set; } = "/healthchecks-webhooks";
        public bool UseRelativeWebhookPath = true;
        public string ResourcesPath { get; set; } = "/ui/resources";
        public bool UseRelativeResourcesPath = true;
    }
}

這樣配置好以后其實(shí)UI里還是不能顯示東西的,還需要添加HealthReport數(shù)據(jù)進(jìn)來 如下配置
When we target applications to be tested and shown on the UI interface, those endpoints have to register the UIResponseWriter that is present on the AspNetCore.HealthChecks.UI.Client as their ResponseWriter in the HealthChecksOptions when configuring UseHealthChecks method.

安裝配置相關(guān)的包

配置代碼(WriteHealthCheckUIResponse is defined on HealthChecks.UI.Client nuget package.)

            app.UseHealthChecks("/health",new HealthCheckOptions() {
                Predicate=_=>true,
                ResponseWriter= UIResponseWriter.WriteHealthCheckUIResponse
            });

下來還需要添加一些JSON配置到appsettings.json,這玩意也是麻煩的很!.NET CORE的整體環(huán)境還是有待改進(jìn)。

{
  "HealthChecks-UI": {
    "HealthChecks": [
      {
        "Name": "HealthChecks",
        "Uri": "http://localhost:5000/health"
      }
    ],
    "Webhooks": [
      {
        "Name": "",
        "Uri": "",
        "Payload": "",
        "RestoredPayload":""
      }
    ],
    "EvaluationTimeOnSeconds": 10,
    "MinimumSecondsBetweenFailureNotifications":60,
    "HealthCheckDatabaseConnectionString": "Data Source=[PUT-MY-PATH-HERE]\\healthchecksdb"
  }
}

運(yùn)行查看效果


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容