HealthChecks 有很多高级用法 —— 自定义检测健康状态、容器化、配合 Prometheus 等前端监控项目等等,这里只展示基本的使用方法

下文将展示 .NET Core 5 中 HealthChecks 的基本用法

环境

  • Visual Studio 2019
  • WIndows 10 2004
  • .NET Core 5

参考

步骤

ASP.NET Core 2.2 就已经新增了 HealthCheck,不用 Nuget 包安装

但是官方实现了一些基本的检测组件(检测网络、系统资源、数据库状态等等)

  1. 新建 ASP.NET Core 项目,安装 Nuget 组件
    Install-Package AspNetCore.HealthChecks.System
    Install-Package AspNetCore.HealthChecks.Network

    除了这两个组件外,还可以安装

    Install-Package AspNetCore.HealthChecks.SqlServer
    Install-Package AspNetCore.HealthChecks.MongoDb
    Install-Package AspNetCore.HealthChecks.Npgsql
    Install-Package AspNetCore.HealthChecks.Redis
    Install-Package AspNetCore.HealthChecks.AzureStorage
    Install-Package AspNetCore.HealthChecks.AzureServiceBus
    Install-Package AspNetCore.HealthChecks.MySql
    Install-Package AspNetCore.HealthChecks.DocumentDb
    Install-Package AspNetCore.HealthChecks.SqLite
    Install-Package AspNetCore.HealthChecks.Kafka
    Install-Package AspNetCore.HealthChecks.RabbitMQ
    Install-Package AspNetCore.HealthChecks.IdSvr
    Install-Package AspNetCore.HealthChecks.DynamoDB
    Install-Package AspNetCore.HealthChecks.Oracle
    Install-Package AspNetCore.HealthChecks.Uris

    以使用对应功能

  2. 在 Startup.cs 中注册 HealthChecks 服务
    public void ConfigureServices(IServiceCollection services) {
        //...
        services.AddHealthChecks()
            .AddDiskStorageHealthCheck(x => x.AddDrive("E", 1000_000_000L)) # 检测 E 盘是否有 1GB 空间
            .AddPrivateMemoryHealthCheck(1000_000_000L) # 最大私有内存不超过 1GB
            .AddVirtualMemorySizeHealthCheck(1000_000_000L) # 最大虚拟内存不超过 1GB
            .AddWorkingSetHealthCheck(1000_000_000L); # 最大工作内存不超过 1GB
    }

    在使用数据库检测时需要传入 ConnectionString,使用 Redis 检测则需要 Redis 服务器链接

  3. 启用 HealthChecks 并配置路由
    using Microsoft.AspNetCore.Diagnostics.HealthChecks;
    using Microsoft.AspNetCore.Http;
    using Newtonsoft.Json; # 第三方 Json 组件
    using Microsoft.Extensions.Diagnostics.HealthChecks;
    using System.Net.Mime;
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
        //...
    
        app.UseHealthChecks("/health", new HealthCheckOptions {
            ResponseWriter = async (context, report) => {
                var result = JsonConvert.SerializeObject(
                    new {
                        status = report.Status.ToString(),
                        errors = report.Entries.Select(e => new { key = e.Key, value = Enum.GetName(typeof(HealthStatus), e.Value.Status) })
                    });
                context.Response.ContentType = MediaTypeNames.Application.Json;
                await context.Response.WriteAsync(result);
            }
        });
    
        //...
    }

自定义检测需要实现 IHealthCheck 接口,并在服务注册方法中添加

services.AddHealthChecks()
  .AddPrivateMemoryHealthCheck(“自定义检测名称”)

Continue reading “ASP.NET Core HealthChecks”

下文并没有根本上解决端口绑定的问题,只是换了一种获取/更新证书的方式,绕过了端口绑定

环境

  • LNMP
  • certbot
  • Ubuntu 16.04

问题描述

certbot 更新证书,执行

certbot renew --dry-run

后,报错

Encountered exception during recovery: certbot.errors.MisconfigurationError: nginx restart failed:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

nginx: [emerg] still could not bind()

Attempting to renew cert (xxx.com) from /etc/letsencrypt/renewal/xxx.com.conf produced an unexpected error: nginx restart failed:

解决 certbot 无法更新证书问题

参考

参考太多了,不整理了

原因

Continue reading “解决 certbot 无法更新证书问题”