Edition 0 Updated to asp. Net core 0



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə269/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   265   266   267   268   269   270   271   272   ...   288
public
class
SqlConnectionHealthCheck : IHealthCheck 

private
const
string
DefaultTestQuery = 
"Select 1"

public
string
ConnectionString { 
get
; } 
public
string
TestQuery { 
get
; } 
public
SqlConnectionHealthCheck
(
string
connectionString) 

this
(connectionString, testQuery: DefaultTestQuery) 


public
SqlConnectionHealthCheck
(
string
connectionString, 
string
testQuery) 

ConnectionString = connectionString ?? 
throw
new
ArgumentNullException
(
nameof
(connectionString)); 
TestQuery = testQuery; 

public
async Task 
CheckHealthAsync
(HealthCheckContext context, 
CancellationToken cancellationToken = 
default
(CancellationToken)) 

using
(
var
connection = 
new
SqlConnection
(ConnectionString)) 

try

await connection.
OpenAsync
(cancellationToken); 
if
(TestQuery != 
null


var
command = connection.
CreateCommand
(); 
command.
CommandText
= TestQuery; 
await command.
ExecuteNonQueryAsync
(cancellationToken); 


catch
(DbException ex) 

return
new
HealthCheckResult
(status: context.
Registration
.
FailureStatus

exception: ex); 


return
HealthCheckResult.
Healthy
(); 


Note that in the previous code
Select 1
is the query used to check the Health of the database. To 
monitor the availability of your microservices, orchestrators like Kubernetes periodically perform 
health checks by sending requests to test the microservices. It’s important to keep your database 
queries efficient so that these operations are quick and don’t result in a higher utilization of resources.


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
//

Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   265   266   267   268   269   270   271   272   ...   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