如何在 Azure 应用服务器上使用运行状况检查

问题描述 投票:0回答:1

我已在

Program.cs
中为我的 Blazor 服务器端应用程序设置运行状况检查。我在 Azure 应用服务上运行它。

var healthCheckBuilder = builder.Services.AddHealthChecks()
    .AddDbContextCheck<TrackingDbContext>("App Database")
    .AddDbContextCheck<UserDbContext>("Identity Database");
if (!string.IsNullOrEmpty(sendGridKey))
    healthCheckBuilder.AddSendGrid(sendGridKey, name: "SendGrid", failureStatus: HealthStatus.Degraded);
if (!string.IsNullOrEmpty(blobConnStr))
    healthCheckBuilder.AddAzureBlobStorage(blobConnStr,
        name: "Azure Blob Storage", failureStatus: HealthStatus.Degraded);

那么我现在该怎么办?我想进行设置,以便 Azure 将重新启动服务和/或启动一个新实例,然后在需要时终止该实例。

如果 Azure 有的话,我还希望仪表板中有一个页面,我可以在其中查看每个服务的运行状况历史记录。

azure-web-app-service blazor-server-side health-monitoring health-check
1个回答
0
投票

感谢@zori提供MSDOC来配置运行状况检查。

我已通过门户配置了运行状况检查,并使用代码执行以下步骤。

我创建了一个 Blazor 应用程序并通过在

Program.cs
中添加以下代码来配置运行状况检查:

  • 请参阅文章以编程方式在 Blazor 应用程序中配置运行状况检查:
builder.Services.AddHealthChecks();
app
    .MapHealthChecks("/health", new HealthCheckOptions()
    {
        ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
    });

程序.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddHealthChecks();
var app = builder.Build();
app
    .MapHealthChecks("/health", new HealthCheckOptions()
    {
        ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
    });
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();

当地健康检查响应:

enter image description here

部署了应用程序Azure App Service

  • 门户响应:

enter image description here

在门户中配置健康检查:

  • 导航到 Azure 应用服务中的
    Monitoring=>Health check
    ,然后启用运行状况检查并配置路径。

enter image description here

  • 在您的站点上指定路径后,应用服务将定期对其进行 ping 操作。
  • 如果其结果的 Http 状态代码在 200 到 299 范围内,则该实例被视为 healthy else 它被视为 unhealthy
  • 如果运行状况检查状态不健康,该实例将从负载均衡器轮换中删除,以避免负载均衡器将请求路由到不健康的实例。

导航到实例以检查服务器实例运行状况:

  • 启用运行状况检查后,重新启动并通过实例选项卡监控应用程序实例的状态。

enter image description here

  • 转到 Metrics 监控应用程序的健康状态。显示

应用程序服务计划中应用程序实例的平均运行状况。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.