- Add redis health check
- Add health checks on startup
This commit is contained in:
parent
56d47db91e
commit
c59e66861f
@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Grpc.AspNetCore" />
|
<PackageReference Include="Grpc.AspNetCore" />
|
||||||
|
<PackageReference Include="AspNetCore.HealthChecks.Redis" />
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -4,6 +4,13 @@ public static class CustomExtensionMethods
|
|||||||
{
|
{
|
||||||
public static IServiceCollection AddRedis(this IServiceCollection services, IConfiguration configuration)
|
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 =>
|
return services.AddSingleton(sp =>
|
||||||
{
|
{
|
||||||
var redisConfig = ConfigurationOptions.Parse(configuration.GetConnectionString("redis"), true);
|
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();
|
builder.AddServiceDefaults();
|
||||||
|
|
||||||
@ -23,20 +25,38 @@ builder.Services.AddTransient<IIdentityService, IdentityService>();
|
|||||||
|
|
||||||
var app = builder.Build();
|
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
|
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();
|
await app.RunAsync();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -7,6 +7,11 @@
|
|||||||
"type": "rabbitmq",
|
"type": "rabbitmq",
|
||||||
"connectionId": "eventbus",
|
"connectionId": "eventbus",
|
||||||
"dynamicId": null
|
"dynamicId": null
|
||||||
|
},
|
||||||
|
"redis1": {
|
||||||
|
"type": "redis",
|
||||||
|
"connectionId": "ConnectionStrings:Redis",
|
||||||
|
"dynamicId": null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,6 +11,16 @@
|
|||||||
"type": "rabbitmq.container",
|
"type": "rabbitmq.container",
|
||||||
"connectionId": "eventbus",
|
"connectionId": "eventbus",
|
||||||
"dynamicId": null
|
"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(
|
"servicebus" => hcBuilder.AddAzureServiceBusTopic(
|
||||||
eventBusConnectionString,
|
eventBusConnectionString,
|
||||||
topicName: "eshop_event_bus",
|
topicName: "eshop_event_bus",
|
||||||
name: "servicebus-check",
|
name: "servicebus",
|
||||||
tags: new string[] { "servicebus" }),
|
tags: new string[] { "ready" }),
|
||||||
|
|
||||||
_ => hcBuilder.AddRabbitMQ(
|
_ => hcBuilder.AddRabbitMQ(
|
||||||
$"amqp://{eventBusConnectionString}",
|
$"amqp://{eventBusConnectionString}",
|
||||||
name: "rabbitmqbus-check",
|
name: "rabbitmq",
|
||||||
tags: new string[] { "rabbitmqbus" })
|
tags: new string[] { "ready" })
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user