Edition 0 Updated to asp. Net core 0


Use the HealthChecks feature in your back-end ASP.NET microservices



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə268/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   264   265   266   267   268   269   270   271   ...   288
NET-Microservices-Architecture-for-Containerized-NET-Applications

Use the HealthChecks feature in your back-end ASP.NET microservices 
In this section, you’ll learn how to implement the HealthChecks feature in a sample ASP.NET Core 7.0 
Web API application when using the 
Microsoft.Extensions.Diagnostics.HealthChecks
 package. The 
Implementation of this feature in a large-scale microservices like the eShopOnContainers is explained 
in the next section. 
To begin, you need to define what constitutes a healthy status for each microservice. In the sample 
application, we define the microservice is healthy if its API is accessible via HTTP and its related SQL 
Server database is also available. 
In .NET 7, with the built-in APIs, you can configure the services, add a Health Check for the 
microservice and its dependent SQL Server database in this way: 
// Program.cs from .NET 7 Web API sample
//...
// Registers required services for health checks
builder.
Services
.
AddHealthChecks
() 
// Add a health check for a SQL Server database
.
AddCheck

"OrderingDB-check"

new
SqlConnectionHealthCheck
(builder.
Configuration
[
"ConnectionString"
]), 
HealthStatus.
Unhealthy

new
string
[] { 
"orderingdb"
}); 
In the previous code, the 
services.AddHealthChecks()
method configures a basic HTTP check that 
returns a status code 
200
with “Healthy”. Further, the 
AddCheck()
extension method configures a 
custom 
SqlConnectionHealthCheck
that checks the 
related SQL Database’s health.
The 
AddCheck()
method adds a new health check with a specified name and the implementation of 
type 
IHealthCheck
. You can add multiple Health Checks using AddCheck method, so a microservice 
won’t provide a “healthy” status unt
il all its checks are healthy. 


313 
CHAPTER 7 | Implement resilient applications 
SqlConnectionHealthCheck
is a custom class that implements 
IHealthCheck
, which takes a 
connection string as a constructor parameter and executes a simple query to check if the connection 
to the SQL database is successful. It returns 
HealthCheckResult.Healthy()
if the query was executed 
successfully and a 
FailureStatus
with the actual exception when it fails. 
// Sample SQL Connection Health Check

Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   264   265   266   267   268   269   270   271   ...   288




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin