refactor: 健康检查移动到通用类库中

Bootstrap.Security.Mvc 升级到 2.2.12 增加健康检查
This commit is contained in:
Argo Zhang 2019-08-24 01:58:38 +08:00
parent 0a1d25c194
commit aadb99957e
8 changed files with 8 additions and 168 deletions

View File

@ -10,7 +10,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.11" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.12" />
<PackageReference Include="Longbow.Configuration" Version="2.2.7" />
<PackageReference Include="Longbow.Tasks" Version="1.8.0" />
<PackageReference Include="Microsoft.AspNetCore.App" />

View File

@ -1,46 +0,0 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Bootstrap.Admin.HealthChecks
{
/// <summary>
/// 文件健康检查类
/// </summary>
public class FileHealCheck : IHealthCheck
{
private readonly IHostingEnvironment _env;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="env"></param>
public FileHealCheck(IHostingEnvironment env)
{
_env = env;
}
/// <summary>
/// 异步检查方法
/// </summary>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var file = _env.IsDevelopment() ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Longbow.lic") : Path.Combine(_env.ContentRootPath, "Longbow.lic");
var data = new Dictionary<string, object>
{
{ "ApplicationName", _env.ApplicationName },
{ "EnvironmentName", _env.EnvironmentName },
{ "ContentRootPath", _env.ContentRootPath },
{ "WebRootPath", _env.WebRootPath },
{ "CheckFile", file }
};
return Task.FromResult(File.Exists(file) ? HealthCheckResult.Healthy("Ok", data) : HealthCheckResult.Unhealthy($"Missing file {file}", null, data));
}
}
}

View File

@ -1,34 +0,0 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Bootstrap.Admin.HealthChecks
{
/// <summary>
/// 内存状态检查其
/// </summary>
public class GCHealthCheck : IHealthCheck
{
/// <summary>
/// 异步检查方法
/// </summary>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
// Include GC information in the reported diagnostics.
var allocated = GC.GetTotalMemory(forceFullCollection: false);
var data = new Dictionary<string, object>()
{
{ "AllocatedMBytes", allocated / 1024 / 1024 },
{ "Gen0Collections", GC.CollectionCount(0) },
{ "Gen1Collections", GC.CollectionCount(1) },
{ "Gen2Collections", GC.CollectionCount(2) },
};
return Task.FromResult(HealthCheckResult.Healthy("OK", data));
}
}
}

View File

@ -1,45 +0,0 @@
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Builder
{
/// <summary>
/// BootstrapAdmin 健康检查扩展类
/// </summary>
public static class HealthChecksAppBuilderExtensions
{
/// <summary>
/// 启用健康检查
/// </summary>
/// <param name="app"></param>
/// <param name="path"></param>
/// <returns></returns>
public static IApplicationBuilder UseBootstrapHealthChecks(this IApplicationBuilder app, PathString path = default)
{
if (path == default) path = "/Healths";
app.UseHealthChecks(path, new HealthCheckOptions()
{
ResponseWriter = (context, report) =>
{
context.Response.ContentType = "application/json";
return context.Response.WriteAsync(JsonConvert.SerializeObject(new { report.Entries.Keys, Report = report }));
},
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status200OK,
[HealthStatus.Unhealthy] = StatusCodes.Status200OK
}
});
app.UseWhen(context => context.Request.Path == "/healths-ui", builder => builder.Run(request =>
{
request.Response.Redirect("/html/Healths-UI.html");
return Task.CompletedTask;
}));
return app;
}
}
}

View File

@ -10,16 +10,15 @@ namespace Microsoft.Extensions.DependencyInjection
/// <summary>
/// 添加 BootstrapAdmin 健康检查
/// </summary>
/// <param name="builder"></param>
/// <param name="services"></param>
/// <returns></returns>
public static IHealthChecksBuilder AddBootstrapAdminHealthChecks(this IHealthChecksBuilder builder)
public static IServiceCollection AddAdminHealthChecks(this IServiceCollection services)
{
var builder = services.AddHealthChecks();
builder.AddCheck<DBHealthCheck>("db");
builder.AddCheck<FileHealCheck>("file");
builder.AddCheck<GCHealthCheck>("gc");
builder.AddCheck<MemoryHealthCheck>("mem");
builder.AddBootstrapAdminHealthChecks();
builder.AddCheck<GiteeHttpHealthCheck>("Gitee");
return builder;
return services;
}
}
}

View File

@ -1,34 +0,0 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace Bootstrap.Admin.HealthChecks
{
/// <summary>
/// 内存检查器
/// </summary>
public class MemoryHealthCheck : IHealthCheck
{
/// <summary>
/// 异步检查方法
/// </summary>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
var process = Process.GetCurrentProcess();
var data = new Dictionary<string, object>()
{
{ "Id", process.Id },
{ "WorkingSet", process.WorkingSet64 },
{ "PrivateMemory", process.PrivateMemorySize64 },
{ "VirtualMemory", process.VirtualMemorySize64 },
};
return Task.FromResult(HealthCheckResult.Healthy("Ok", data));
}
}
}

View File

@ -65,7 +65,7 @@ namespace Bootstrap.Admin
services.AddButtonAuthorization();
services.AddDemoTask();
services.AddHttpClient<GiteeHttpClient>();
services.AddHealthChecks().AddBootstrapAdminHealthChecks();
services.AddAdminHealthChecks();
services.AddMvc(options =>
{
options.Filters.Add<BootstrapAdminAuthorizeFilter>();

View File

@ -6,7 +6,7 @@
<ItemGroup>
<PackageReference Include="Bootstrap.Security.DataAccess" Version="2.2.11" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.11" />
<PackageReference Include="Bootstrap.Security.Mvc" Version="2.2.12" />
<PackageReference Include="Longbow.Configuration" Version="2.2.7" />
<PackageReference Include="Longbow.Logging" Version="2.2.12" />
<PackageReference Include="Longbow.Web" Version="2.2.15" />