From bb2db8d7b6334c18eec3a5269f78450d9016e024 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 8 Jan 2026 09:23:22 +0800 Subject: [PATCH] feat: Avoid tracking health check endpoints --- .../AppHost.cs | 4 +- .../Program.cs | 5 ++- .../ServiceHealthChecksExtenssions.cs | 33 ----------------- .../HealthChecksExtenssions.cs | 37 +++++++++++++++++++ .../Program.cs | 7 ++-- .../ServiceHealthCheck.cs | 2 +- .../ServiceHealthChecksExtenssions.cs | 33 ----------------- 7 files changed, 47 insertions(+), 74 deletions(-) delete mode 100644 aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/ServiceHealthChecksExtenssions.cs create mode 100644 aspnet-core/aspire/LINGYUN.Abp.MicroService.ServiceDefaults/HealthChecksExtenssions.cs delete mode 100644 aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthChecksExtenssions.cs diff --git a/aspnet-core/aspire/LINGYUN.Abp.MicroService.AppHost/AppHost.cs b/aspnet-core/aspire/LINGYUN.Abp.MicroService.AppHost/AppHost.cs index e0f2be724..48a47b899 100644 --- a/aspnet-core/aspire/LINGYUN.Abp.MicroService.AppHost/AppHost.cs +++ b/aspnet-core/aspire/LINGYUN.Abp.MicroService.AppHost/AppHost.cs @@ -100,7 +100,7 @@ var localizationService = AddDotNetProject< migratorSuffix: "Migrator", port: 30030, portName: "localization") - .WithHttpHealthCheck("/service-health"); + .WithHttpHealthCheck("/health/service"); // AuthServer var authServer = AddDotNetProject< @@ -149,7 +149,7 @@ var taskService = AddDotNetProject< port: 30040, portName: "task", waitProject: adminService) - .WithHttpHealthCheck("/service-health"); + .WithHttpHealthCheck("/health/service"); // MessageService AddDotNetProject< diff --git a/aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/Program.cs b/aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/Program.cs index 846717202..bc1cdf330 100644 --- a/aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/Program.cs +++ b/aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/Program.cs @@ -1,5 +1,6 @@ using LINGYUN.Abp.Identity.Session.AspNetCore; using LINGYUN.Abp.MicroService.LocalizationService; +using LINGYUN.Abp.MicroService.ServiceDefaults; using LINGYUN.Abp.Serilog.Enrichers.Application; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -32,7 +33,7 @@ try }); builder.AddServiceDefaults(); - builder.AddServiceHealthChecks(); + builder.AddCustomHealthChecks("Service"); await builder.AddApplicationAsync(options => { @@ -50,7 +51,7 @@ try await app.InitializeApplicationAsync(); app.MapDefaultEndpoints(); - app.MapServiceHealthChecks(); + app.MapCustomHealthChecks("/health/service"); app.UseForwardedHeaders(); // 本地化 diff --git a/aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/ServiceHealthChecksExtenssions.cs b/aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/ServiceHealthChecksExtenssions.cs deleted file mode 100644 index ea6e6fe86..000000000 --- a/aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/ServiceHealthChecksExtenssions.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Diagnostics.HealthChecks; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace LINGYUN.Abp.MicroService.LocalizationService; - -internal static class ServiceHealthChecksExtenssions -{ - private const string HealthEndpointPath = "/service-health"; - private const string DefaultHealthTag = "ready"; - - public static TBuilder AddServiceHealthChecks(this TBuilder builder, string name = "Service", string tag = DefaultHealthTag) where TBuilder : IHostApplicationBuilder - { - builder.Services - .AddHealthChecks() - .AddCheck(name, tags: [tag]); - - return builder; - } - - public static WebApplication MapServiceHealthChecks(this WebApplication app, string tag = DefaultHealthTag) - { - if (app.Environment.IsDevelopment()) - { - app.MapHealthChecks(HealthEndpointPath, new HealthCheckOptions - { - Predicate = r => r.Tags.Contains(tag) - }); - } - return app; - } -} diff --git a/aspnet-core/aspire/LINGYUN.Abp.MicroService.ServiceDefaults/HealthChecksExtenssions.cs b/aspnet-core/aspire/LINGYUN.Abp.MicroService.ServiceDefaults/HealthChecksExtenssions.cs new file mode 100644 index 000000000..12b359a4f --- /dev/null +++ b/aspnet-core/aspire/LINGYUN.Abp.MicroService.ServiceDefaults/HealthChecksExtenssions.cs @@ -0,0 +1,37 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Hosting; + +namespace LINGYUN.Abp.MicroService.ServiceDefaults; +public static class HealthChecksExtenssions +{ + public static IHostApplicationBuilder AddCustomHealthChecks( + this IHostApplicationBuilder builder, + string name, + string tag = "ready") + where THealthCheck : class, IHealthCheck + { + builder.Services + .AddHealthChecks() + .AddCheck(name, tags: [tag]); + + return builder; + } + + public static WebApplication MapCustomHealthChecks( + this WebApplication app, + string pattern, + string tag = "ready") + { + if (app.Environment.IsDevelopment()) + { + app.MapHealthChecks(pattern, new HealthCheckOptions + { + Predicate = r => r.Tags.Contains(tag) + }); + } + return app; + } +} diff --git a/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/Program.cs b/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/Program.cs index fe86697ef..a499a0d72 100644 --- a/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/Program.cs +++ b/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/Program.cs @@ -1,4 +1,5 @@ -using LINGYUN.Abp.MicroService.TaskService; +using LINGYUN.Abp.MicroService.ServiceDefaults; +using LINGYUN.Abp.MicroService.TaskService; using LINGYUN.Abp.Serilog.Enrichers.Application; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; @@ -31,7 +32,7 @@ try }); builder.AddServiceDefaults(); - builder.AddServiceHealthChecks(); + builder.AddCustomHealthChecks("Service"); await builder.AddApplicationAsync(options => { @@ -49,7 +50,7 @@ try await app.InitializeApplicationAsync(); app.MapDefaultEndpoints(); - app.MapServiceHealthChecks(); + app.MapCustomHealthChecks("/health/service"); app.UseForwardedHeaders(); app.UseAbpRequestLocalization(); diff --git a/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthCheck.cs b/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthCheck.cs index 91c0745bb..c1c91dd3d 100644 --- a/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthCheck.cs +++ b/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthCheck.cs @@ -23,7 +23,7 @@ public class ServiceHealthCheck : IHealthCheck _factory = factory ?? throw new ArgumentNullException(nameof(factory)); } - public async Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) + public async virtual Task CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { try { diff --git a/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthChecksExtenssions.cs b/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthChecksExtenssions.cs deleted file mode 100644 index ca0badb28..000000000 --- a/aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthChecksExtenssions.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Diagnostics.HealthChecks; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace LINGYUN.Abp.MicroService.TaskService; - -internal static class ServiceHealthChecksExtenssions -{ - private const string HealthEndpointPath = "/service-health"; - private const string DefaultHealthTag = "ready"; - - public static TBuilder AddServiceHealthChecks(this TBuilder builder, string name = "Service", string tag = DefaultHealthTag) where TBuilder : IHostApplicationBuilder - { - builder.Services - .AddHealthChecks() - .AddCheck(name, tags: [tag]); - - return builder; - } - - public static WebApplication MapServiceHealthChecks(this WebApplication app, string tag = DefaultHealthTag) - { - if (app.Environment.IsDevelopment()) - { - app.MapHealthChecks(HealthEndpointPath, new HealthCheckOptions - { - Predicate = r => r.Tags.Contains(tag) - }); - } - return app; - } -}