314
CHAPTER 7 | Implement resilient applications
Finally, add a middleware that responds to the url path
/hc
:
// Program.cs from .NET 7 Web Api sample
app.
MapHealthChecks
(
"/hc"
);
When the endpoint
/hc
is invoked, it runs all the health checks that are
configured in the
AddHealthChecks()
method in the Startup class and shows the result.
HealthChecks implementation in eShopOnContainers
Microservices in eShopOnContainers rely on multiple services to perform its task. For example, the
Catalog.API
microservice from eShopOnContainers depends on many services, such as Azure Blob
Storage, SQL Server, and RabbitMQ. Therefore, it has several health checks added using the
AddCheck()
method. For every dependent service, a custom
IHealthCheck
implementation that
defines its respective health status would need to be added.
The open-source project
AspNetCore.Diagnostics.HealthChecks
solves this problem by providing
custom health check implementations for each of these enterprise services, that are built on top of
.NET 7. Each health check is available as an individual NuGet package that can be easily added to the
project. eShopOnContainers uses them extensively in all its microservices.
For instance, in the
Catalog.API
microservice, the following NuGet packages were added:
Figure 8-7. Custom Health Checks implemented in Catalog.API using AspNetCore.Diagnostics.HealthChecks
In the following code, the health check implementations are added for each dependent service and
then the middleware is configured:
// Extension method from Catalog.api microservice
//
Dostları ilə paylaş: