美文网首页.Net & Core
ASP.NET CORE 健康检查

ASP.NET CORE 健康检查

作者: 三只仓鼠 | 来源:发表于2019-06-26 17:33 被阅读82次

    首先创建一个空的web api项目


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

    Startup文件中添加相应代码
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
                services.AddHealthChecks();//添加服务
            }
    
            // 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相关的包,所以我们首先要添加源到NUGET管理里来
    We use MyGet feed for preview versions of HealthChecks pacakges.

    VS-工具-选项


    添加以后点击更新 确定就可以进行安装了


    在当前管理界面右上角选择自己添加的源进行搜索

    我们选择第三个进行安装

    进行数据库连接的配置

    首先运行mysql服务


    image.png

    连接正常 配置相关数据库连接和代码设置


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

    运行访问



    如果健康则显示Healthy不健康为Unhealthy



    不过这样并不是很明显,因此还需要添加一款更直观的UI进行展示。
    搜索刚才添加的源进行安装



    添加配置代码

    services.AddHealthChecksUI();
    
    app.UseHealthChecksUI();
    

    This automatically registers a new interface on /healthchecks-ui where the spa will be served.
    已经自动注册了地址了

    源码相关默认配置

    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;
        }
    }
    

    这样配置好以后其实UI里还是不能显示东西的,还需要添加HealthReport数据进来 如下配置
    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.

    安装配置相关的包

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

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

    下来还需要添加一些JSON配置到appsettings.json,这玩意也是麻烦的很!.NET CORE的整体环境还是有待改进。

    {
      "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"
      }
    }
    

    运行查看效果


    相关文章

      网友评论

        本文标题:ASP.NET CORE 健康检查

        本文链接:https://www.haomeiwen.com/subject/jobccctx.html