- Add redis health check
- Add health checks on startup
This commit is contained in:
parent
56d47db91e
commit
c59e66861f
@ -7,6 +7,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Grpc.AspNetCore" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.Redis" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -4,6 +4,13 @@ public static class CustomExtensionMethods
|
||||
{
|
||||
public static IServiceCollection AddRedis(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddHealthChecks()
|
||||
.AddRedis(_ =>
|
||||
{
|
||||
return configuration.GetConnectionString("redis");
|
||||
},
|
||||
"redis", tags: new[] { "ready", "liveness" });
|
||||
|
||||
return services.AddSingleton(sp =>
|
||||
{
|
||||
var redisConfig = ConfigurationOptions.Parse(configuration.GetConnectionString("redis"), true);
|
||||
|
@ -1,4 +1,6 @@
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.AddServiceDefaults();
|
||||
|
||||
@ -23,20 +25,38 @@ builder.Services.AddTransient<IIdentityService, IdentityService>();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseServiceDefaults();
|
||||
|
||||
app.MapGet("/", () => Results.Redirect("/swagger"));
|
||||
|
||||
app.MapGrpcService<BasketService>();
|
||||
app.MapControllers();
|
||||
|
||||
var eventBus = app.Services.GetRequiredService<IEventBus>();
|
||||
|
||||
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();
|
||||
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>();
|
||||
|
||||
try
|
||||
{
|
||||
app.Logger.LogInformation("Running health checks...");
|
||||
|
||||
// Do a health check on startup, this will throw an exception if any of the checks fail
|
||||
var report = await app.Services.GetRequiredService<HealthCheckService>().CheckHealthAsync();
|
||||
|
||||
if (report.Status == HealthStatus.Unhealthy)
|
||||
{
|
||||
app.Logger.LogCritical("Health checks failed!");
|
||||
foreach (var entry in report.Entries)
|
||||
{
|
||||
if (entry.Value.Status == HealthStatus.Unhealthy)
|
||||
{
|
||||
app.Logger.LogCritical("{Check}: {Status}", entry.Key, entry.Value.Status);
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
app.UseServiceDefaults();
|
||||
|
||||
app.MapGet("/", () => Results.Redirect("/swagger"));
|
||||
|
||||
app.MapGrpcService<BasketService>();
|
||||
app.MapControllers();
|
||||
|
||||
var eventBus = app.Services.GetRequiredService<IEventBus>();
|
||||
|
||||
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();
|
||||
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>();
|
||||
|
||||
await app.RunAsync();
|
||||
|
||||
return 0;
|
||||
|
@ -7,6 +7,11 @@
|
||||
"type": "rabbitmq",
|
||||
"connectionId": "eventbus",
|
||||
"dynamicId": null
|
||||
},
|
||||
"redis1": {
|
||||
"type": "redis",
|
||||
"connectionId": "ConnectionStrings:Redis",
|
||||
"dynamicId": null
|
||||
}
|
||||
}
|
||||
}
|
@ -11,6 +11,16 @@
|
||||
"type": "rabbitmq.container",
|
||||
"connectionId": "eventbus",
|
||||
"dynamicId": null
|
||||
},
|
||||
"redis1": {
|
||||
"serviceConnectorResourceId": "",
|
||||
"containerPorts": "6379:6379",
|
||||
"secretStore": "LocalSecretsFile",
|
||||
"containerName": "basket-redis",
|
||||
"containerImage": "redis:alpine",
|
||||
"type": "redis.container",
|
||||
"connectionId": "ConnectionStrings:Redis",
|
||||
"dynamicId": null
|
||||
}
|
||||
}
|
||||
}
|
@ -283,13 +283,13 @@ public static class CommonExtensions
|
||||
"servicebus" => hcBuilder.AddAzureServiceBusTopic(
|
||||
eventBusConnectionString,
|
||||
topicName: "eshop_event_bus",
|
||||
name: "servicebus-check",
|
||||
tags: new string[] { "servicebus" }),
|
||||
name: "servicebus",
|
||||
tags: new string[] { "ready" }),
|
||||
|
||||
_ => hcBuilder.AddRabbitMQ(
|
||||
$"amqp://{eventBusConnectionString}",
|
||||
name: "rabbitmqbus-check",
|
||||
tags: new string[] { "rabbitmqbus" })
|
||||
name: "rabbitmq",
|
||||
tags: new string[] { "ready" })
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user