In a microservices architecture, ensuring that each service is running and healthy is crucial for maintaining the overall system's reliability. Actuator, a library inspired by Spring Boot Actuator, can be used to expose operational information about an application running in production. This guide will walk through the implementation of health checks for two .NET microservices, Product and Order services, using Actuator.
Setting Up Actuator in ASP.NET Core
Actuator in the context of .NET is typically managed through libraries like AspNetCore.Diagnostics.HealthChecks offers various health check implementations and features.
1. Create Product and Order Services
Assume you already have two microservices: Product and Order. We'll add health checks using Actuator-like features to these services.
2. Install Required Packages
First, install the necessary NuGet packages for health checks.
dotnet add package AspNetCore.HealthChecks.UI
dotnet add package AspNetCore.HealthChecks.UI.Client
dotnet add package AspNetCore.HealthChecks.UI.InMemory.Storage
dotnet add package AspNetCore.HealthChecks.SqlServer
3. Configure Health Checks in Startup.cs
In each service, configure health checks in the Startup.cs file.
Product Service
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Add health checks
services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy())
.AddSqlServer(Configuration.GetConnectionString("ProductDatabase"), name: "ProductDB-check");
// Add HealthChecks UI
services.AddHealthChecksUI()
.AddInMemoryStorage();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
// Map health checks
endpoints.MapHealthChecks("/health", new HealthCheckOptions()
{
Predicate = _ => true,
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
// Map HealthChecks UI
endpoints.MapHealthChecksUI(setup => setup.UIPath = "/health-ui");
});
}
}

Join the conversation! Your thoughts help the community grow.