4.4. MicroProfile 健康开发

4.4.1. 自定义健康检查示例

microprofile-health-smallrye 子系统提供的默认实施将执行基本的健康检查。如需更多详细信息,可以包括服务器或应用程序状态上的自定义健康检查。在运行时会自动发现和调用任何包含 org.eclipse.microprofile.health.Liveness 注解 或类级别的 org.eclipse.microprofile.health.Readiness 注解的任何 Jakarta Contexts 和 Dependency Injection bean。

以下示例演示了如何创建返回 UP 状态的健康检查的新实施。

import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Liveness;

import javax.enterprise.context.ApplicationScoped;

@Liveness
@ApplicationScoped
public class HealthTest implements HealthCheck {

    @Override
    public HealthCheckResponse call() {
        return HealthCheckResponse.named("health-test").up().build();
    }
}

部署后,任何后续健康检查查询都会包括自定义检查,如下例中所示。

/subsystem=microprofile-health-smallrye:check
{
    "outcome" => "success",
    "result" => {
        "outcome" => "UP",
        "checks" => [{
            "name" => "health-test",
            "state" => "UP"
        }]
    }
}
注意

您可以使用以下命令进行存活度和就绪度检查:

  • /subsystem=microprofile-health-smallrye:check-live
  • /subsystem=microprofile-health-smallrye:check-ready