Skip to content
bitzorcas
EN

Concept

健康检查

存活和就绪健康探针 — Aspire ServiceDefaults 提供带标签的检查,用于进程健康和依赖就绪。

Last updated

BitzOrcas 通过 Aspire ServiceDefaults 提供两级健康检查——存活用于进程健康,就绪用于依赖可用性。

两级检查

存活检查 (/health/live)

证明进程正在运行并响应。不检查外部依赖。

builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy("alive"),
tags: [LivenessTag]);

K8s 用法:livenessProbe → 失败时重启 Pod。

就绪检查 (/health/ready)

检查关键外部依赖。优雅失败——不会重启进程,但通知负载均衡器停止路由流量。

K8s 用法:readinessProbe → 从服务端点池中移除。

生产就绪检查由基础设施适配器注册:

依赖注册者检查
SQL ServerPersistenceAdapters连接 + 查询
RabbitMQPersistenceAdapters连接
(未来)Redis缓存适配器连接 + ping

标签

public const string LivenessTag = "live";
public const string ReadinessTag = "ready";

端点

GET /health/live → 200 OK(始终,如果进程存活)
GET /health/ready → 200 OK(所有依赖健康)或 503(依赖不可用)
GET /health → 所有已注册检查(未过滤)

配置

健康检查端点由 Aspire 的 AddServiceDefaults() 注册:

// 在 Program.cs 中 — 已包含
builder.AddServiceDefaults();
// 映射健康端点
app.MapHealthChecks("/health");
app.MapHealthChecks("/health/live", new HealthCheckOptions
{
Predicate = check => check.Tags.Contains("live")
});
app.MapHealthChecks("/health/ready", new HealthCheckOptions
{
Predicate = check => check.Tags.Contains("ready")
});

添加自定义健康检查

在你的适配器中注册额外的就绪检查:

services.AddHealthChecks()
.AddNpgSql(connectionString, name: "postgres", tags: ["ready"]);
services.AddHealthChecks()
.AddRabbitMQ(rabbitMqConnectionString, name: "rabbitmq", tags: ["ready"]);

另见