Browse Source

feat: Avoid tracking health check endpoints

pull/1414/head
colin 4 weeks ago
parent
commit
bb2db8d7b6
  1. 4
      aspnet-core/aspire/LINGYUN.Abp.MicroService.AppHost/AppHost.cs
  2. 5
      aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/Program.cs
  3. 33
      aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/ServiceHealthChecksExtenssions.cs
  4. 37
      aspnet-core/aspire/LINGYUN.Abp.MicroService.ServiceDefaults/HealthChecksExtenssions.cs
  5. 7
      aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/Program.cs
  6. 2
      aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthCheck.cs
  7. 33
      aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthChecksExtenssions.cs

4
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<

5
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<ServiceHealthCheck>("Service");
await builder.AddApplicationAsync<LocalizationServiceModule>(options =>
{
@ -50,7 +51,7 @@ try
await app.InitializeApplicationAsync();
app.MapDefaultEndpoints();
app.MapServiceHealthChecks();
app.MapCustomHealthChecks("/health/service");
app.UseForwardedHeaders();
// 本地化

33
aspnet-core/aspire/LINGYUN.Abp.MicroService.LocalizationService/ServiceHealthChecksExtenssions.cs

@ -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<TBuilder>(this TBuilder builder, string name = "Service", string tag = DefaultHealthTag) where TBuilder : IHostApplicationBuilder
{
builder.Services
.AddHealthChecks()
.AddCheck<ServiceHealthCheck>(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;
}
}

37
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<THealthCheck>(
this IHostApplicationBuilder builder,
string name,
string tag = "ready")
where THealthCheck : class, IHealthCheck
{
builder.Services
.AddHealthChecks()
.AddCheck<THealthCheck>(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;
}
}

7
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<ServiceHealthCheck>("Service");
await builder.AddApplicationAsync<TaskServiceModule>(options =>
{
@ -49,7 +50,7 @@ try
await app.InitializeApplicationAsync();
app.MapDefaultEndpoints();
app.MapServiceHealthChecks();
app.MapCustomHealthChecks("/health/service");
app.UseForwardedHeaders();
app.UseAbpRequestLocalization();

2
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<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
public async virtual Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
{
try
{

33
aspnet-core/aspire/LINGYUN.Abp.MicroService.TaskService/ServiceHealthChecksExtenssions.cs

@ -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<TBuilder>(this TBuilder builder, string name = "Service", string tag = DefaultHealthTag) where TBuilder : IHostApplicationBuilder
{
builder.Services
.AddHealthChecks()
.AddCheck<ServiceHealthCheck>(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;
}
}
Loading…
Cancel
Save