Lädt...


🔧 Health Checks in ASP.NET Core for .NET Microservices with Actuator


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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");
        });
    }
}

Order Service

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        // Add health checks
        services.AddHealthChecks()
                .AddCheck("self", () => HealthCheckResult.Healthy())
                .AddSqlServer(Configuration.GetConnectionString("OrderDatabase"), name: "OrderDB-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");
        });
    }
}

4. Run and Test Health Checks

Run the services and test the health check endpoints.

Port Number (80) should be replaced with your port numbers.
You should see a JSON response indicating the health status of the service on the /health endpoint and a detailed UI on the /health-ui endpoint.

5. Aggregating Health Checks with Kubernetes or Docker

When deploying to Kubernetes or Docker, you can use readiness and liveness probes to ensure that your services are healthy.

Kubernetes Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: product-service
  template:
    metadata:
      labels:
        app: product-service
    spec:
      containers:
        - name: product-service
          image: your-docker-image
          ports:
            - containerPort: 80
          readinessProbe:
            httpGet:
              path: /health
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 10

Conclusion

Implementing health checks in ASP.NET Core for microservices like Product and Order services is straightforward and enhances the reliability of your system. By configuring health checks and a health check UI, you can monitor the status of your services effectively. Integrating this setup with orchestration tools like Kubernetes further ensures robust deployment strategies and the smooth operation of your microservices architecture.

🔥 Don't Miss Out! Explore More Exciting Bytes Here! 🔍https://linktr.ee/dotnetfullstackdev and dive into the content!

...

🔧 Health Checks in ASP.NET Core for .NET Microservices with Actuator


📈 79.21 Punkte
🔧 Programmierung

🔧 How to set up ASP.NET Core 2.2 Health Checks with BeatPulse’s AspNetCore.Diagnostics.HealthChecks


📈 39.93 Punkte
🔧 Programmierung

🔧 Asp.Net Core and Keycloak testcontainer. Testing a secure Asp.Net Core Api using Keycloak Testcontainer


📈 36.86 Punkte
🔧 Programmierung

🔧 Visual Studio for Mac + ASP.NET Core – Getting started with ASP.NET Core using eShopOnWeb


📈 36.86 Punkte
🔧 Programmierung

📰 .NET Core, ASP.NET Core und Entity Framework Core 2.1 sind fertig


📈 33.72 Punkte
📰 IT Nachrichten

🔧 [ASP.NET Core][EntityFramework Core] Update from .NET 6 to .NET 8


📈 32.27 Punkte
🔧 Programmierung

🔧 Differences between Asp.net and Asp.net Core


📈 31.28 Punkte
🔧 Programmierung

🔧 Upcoming SameSite Cookie Changes in ASP.NET and ASP.NET Core


📈 31.28 Punkte
🔧 Programmierung

🔧 Using SkipToken for Paging in Asp.Net OData and Asp.Net Core OData


📈 31.28 Punkte
🔧 Programmierung

🔧 Microsoft .NET Maze: Understand .NET Core Vs .NET Framework Vs ASP.NET


📈 30.83 Punkte
🔧 Programmierung

🔧 Microservices using ASP.NET Core, Ocelot, MongoDB and JWT


📈 29.66 Punkte
🔧 Programmierung

📰 Adaptiva adds new capabilities and health checks to its Endpoint Health product


📈 29.66 Punkte
📰 IT Security Nachrichten

🔧 ASP.NET Core and Blazor updates in .NET Core 3.0 Preview 7


📈 28.14 Punkte
🔧 Programmierung

📰 Announcing a Microsoft .NET Core and ASP.NET Core Bug Bounty


📈 28.14 Punkte
📰 IT Security Nachrichten

🔧 ASP.NET Core and Blazor updates in .NET Core 3.0 Preview 6


📈 28.14 Punkte
🔧 Programmierung

🔧 .NET Core und ASP.NET Core 2.2.0 Preview 3 verfügbar


📈 28.14 Punkte
🔧 Programmierung

🕵️ Microsoft ASP.NET Core/.NET Core System.IO.Pipelines denial of service


📈 28.14 Punkte
🕵️ Sicherheitslücken

📰 ASP.NET Core 3.0 läuft nur noch auf .NET Core


📈 28.14 Punkte
📰 IT Nachrichten

🔧 Updating an ASP.NET Core 2.2 Web Site to .NET Core 3.1 LTS


📈 28.14 Punkte
🔧 Programmierung

🕵️ Microsoft ASP.NET Core/.NET Core Web Request Denial of Service


📈 28.14 Punkte
🕵️ Sicherheitslücken

🔧 ASP.NET Core updates in .NET Core 3.1


📈 28.14 Punkte
🔧 Programmierung

🕵️ Microsoft ASP.NET Core/.NET Core Web Request erweiterte Rechte


📈 28.14 Punkte
🕵️ Sicherheitslücken

🔧 ASP.NET Core updates in .NET Core 3.1 Preview 3


📈 28.14 Punkte
🔧 Programmierung

🕵️ Microsoft ASP.NET Core/.NET Core Web Request Spoofing [CVE-2017-0256]


📈 28.14 Punkte
🕵️ Sicherheitslücken

🔧 ASP.NET Core updates in .NET Core 3.1 Preview 2


📈 28.14 Punkte
🔧 Programmierung

📰 Microsoft legt Bug-Bounty-Programm für .NET Core und ASP.NET Core neu auf


📈 28.14 Punkte
📰 IT Nachrichten

🔧 ASP.NET Core updates in .NET Core 3.1 Preview 1


📈 28.14 Punkte
🔧 Programmierung

📰 Microsoft legt Bug-Bounty-Programm für .NET Core und ASP.NET Core neu auf


📈 28.14 Punkte
📰 IT Nachrichten

🕵️ Microsoft ASP.NET Core/.NET Core Web Request denial of service


📈 28.14 Punkte
🕵️ Sicherheitslücken

🕵️ Microsoft ASP.NET Core/.NET Core Web Request privilege escalation


📈 28.14 Punkte
🕵️ Sicherheitslücken

🕵️ Microsoft ASP.NET Core/.NET Core Web Request spoofing


📈 28.14 Punkte
🕵️ Sicherheitslücken

🔧 ASP.NET Core and Blazor updates in .NET Core 3.0


📈 28.14 Punkte
🔧 Programmierung

matomo