Search code examples
asp.net-coremicroserviceshealth-monitoring

How can I make my .NET Core microservice do a recursive health check?


As described I can do:

checks.AddUrlCheck(Configuration["OrderingUrl"])

to make my health check dependant on the health of other Microservices. However,I do not just want to do an url check. I want to do a full health check on the other Microservice (so it will also check the database dependencies etc of the other Microservice). This could be something like,

checks.AddFullMicroserviceIncludingDatabaseAndUrlCheck(Configuration["OrderingUrl"]) (hypothically).

How can I do such a recursive health check in my .NET Core microservice?


Solution

  • In your microservice you can do whatever you want. Let's say that we have two microservice A and B and we want to monitor their health check:

    Microservice A

    This microservice uses SQL Server, so we're going to check SQL connection.

    services.AddHealthChecks(checks =>
    {
        checks.AddSqlCheck("ServiceA_DB", Configuration["ConnectionString"]);
    });
    

    Microservice B

    This microservice uses SQL server too, but it also uses some other service (for example REST API), so we're going to check SQL connection and the REST API

    services.AddHealthChecks(checks =>
    {
        checks.AddUrlCheck(Configuration["RequiredServiceUrl"]);
        checks.AddSqlCheck("ServiceB_DB", Configuration["ConnectionString"]);
    });
    

    Web Status

    Finally we have some web application that monitors these two microservices

    services.AddHealthChecks(checks =>
    {
        checks.AddUrlCheck(Configuration["ServiceAUrl"]);
        checks.AddUrlCheck(Configuration["ServiceBUrl"]);
    });
    

    It means that if I navigate to http://webstatus/hc (health check page), the system checks http://serviceA/hc (it checks db), and http://serviceB/hc (it checks db and rest api).

    Or you can visualize health check of each microservice as shown there (Figure 10-8)