Health Check of CAP Library— CAP.HealthCheck
Microservice architectures frequently use asynchronous communication. One service can send an asynchronous message to another service using the message broker, such as RabbitMQ or Kafka. The outbox pattern is one of the standard solutions in this architecture. CAP is a library based on .NET, which implements the outbox pattern described in the eShop ebook. In this pattern, published and received messages are stored on storage, such as MSSQL or PostgreSQL. In addition, each message has a state which is Succeeded or Failed. It is essential to monitor storage and message broker connectivity and failures. Otherwise, data inconsistencies may occur in the system. In this article, CAP.HealthCheck, a health check library developed for the CAP, will be introduced.
CAP.HealthCheck library
It is available at NuGet. CAP HealthCheck can be installed in the project with the following command:
PM> Install-Package DotNetCore.CAP.HealthCheck
Storage health checks
Currently, it supports PostgreSQL and MongoDB as storage health checks. To install:
PM> Install-Package DotNetCore.CAP.HealthCheck.PostgreSql
PM> Install-Package DotNetCore.CAP.HealthCheck.MongoDB
They have several health check services that check database connectivity and failed data count in published
and receive
tables. If failed counts are zero and the connection is successful, it returns healthy, otherwise unhealthy.
Transport health checks
Currently, it supports only RabbitMQ as transport health checks. To install:
PM> Install-Package DotNetCore.CAP.HealthCheck.RabbitMQ
It has only broker connectivity health check for now.
Configuration
Usage and configuration are easy as a .NET health check service. They are added in ConfigureServices
method without specifying parameters like below:
Health check services are easily accessible at route /health-cap
by default. The code below is used to enable and configure the route.
Example Output
{
"status": "Unhealthy",
"results": {
"cap.postgres.connection": {
"status": "Healthy",
"description": null,
"duration": "00:00:00.0669333",
"tags": [
"cap"
],
"data": {}
},
"cap.postgres.publishedtable": {
"status": "Healthy",
"description": null,
"duration": "00:00:00.0282225",
"tags": [
"cap"
],
"data": {
"failedCount": 0,
"succeededCount": 1
}
},
"cap.postgres.receivedtable": {
"status": "Unhealthy",
"description": "Failed Count: 1",
"duration": "00:00:00.0407061",
"tags": [
"cap"
],
"data": {
"failedCount": 1,
"succeededCount": 0
}
},
"cap.rabbitmq.connection": {
"status": "Healthy",
"description": null,
"duration": "00:00:00.0042972",
"tags": [
"cap"
],
"data": {}
}
}
}
Sample Applications
The github repository contains sample applications, and they have their docker compose file to run required infrastructures. Sample applications have a controller named Test with route api/test
.
There are two endpoints for publishing an event with success and failure scenarios.
http://localhost:5000/api/test/publish
publishes an event with successful subscription.http://localhost:5000/api/test/publish/error
publishes an event with failed subscription.
The healthcheck service is available at http://localhost:5000/health-cap
.
Repository
Source code is available at github:
Future Works
This project will continue with the addition of health check services for all the features of the CAP library.
Our articles about CAP
Thanks for reading.