4.4. MicroProfile 健康开发
4.4.1. 自定义健康检查示例 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
microprofile-health-smallrye 子系统提供的默认实施执行基本的健康检查。如需更多详细信息,可能会包含在服务器或应用程序状态上。任何 Jakarta 上下文和依赖注入 Bean,其中包括 org.eclipse.microprofile.health.Liveness、org.eclipse.microprofile.health.Readiness、或 org.eclipse.microprofile.health.Startup 注释,它们在运行时会自动发现并调用。
以下示例演示了如何创建返回 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();
}
}
部署健康检查后,后续健康检查查询都包含自定义检查,如下例所示。
[standalone@localhost:9990 /] /subsystem=microprofile-health-smallrye:check
{
"outcome" => "success",
"result" => {
"status" => "UP",
"checks" => [
{
"name" => "deployments-status",
"status" => "UP",
"data" => {"<deployment_name>.war" => "OK"}
},
{
"name" => "server-state",
"status" => "UP",
"data" => {"value" => "running"}
},
{
"name" => "boot-errors",
"status" => "UP"
},
{
"name" => "health-test",
"status" => "UP"
},
{
"name" => "ready-deployment.<deployment_name>.war,
"status" => "UP"
},
{
"name" => "started-deployment.<deployment_name>.war",
"status" => "UP"
}
]
}
}
注意
您可以使用以下命令进行存活度、就绪度和启动检查:
-
/subsystem=microprofile-health-smallrye:check-live -
/subsystem=microprofile-health-smallrye:check-ready -
/subsystem=microprofile-health-smallrye:check-started
4.4.2. @Liveness 注释示例 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
以下示例演示了如何在应用中使用 @Liveness 注释。
@Liveness
@ApplicationScoped
public class DataHealthCheck implements HealthCheck {
@Override
public HealthCheckResponse call() {
return HealthCheckResponse.named("Health check with data")
.up()
.withData("foo", "fooValue")
.withData("bar", "barValue")
.build();
}
}
4.4.3. @Readiness 注释示例 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
以下示例演示了如何检查与数据库的连接。如果数据库停机,就绪度检查会报告错误。
@Readiness
@ApplicationScoped
public class DatabaseConnectionHealthCheck implements HealthCheck {
@Inject
@ConfigProperty(name = "database.up", defaultValue = "false")
private boolean databaseUp;
@Override
public HealthCheckResponse call() {
HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("Database connection health check");
try {
simulateDatabaseConnectionVerification();
responseBuilder.up();
} catch (IllegalStateException e) {
// cannot access the database
responseBuilder.down()
.withData("error", e.getMessage()); // pass the exception message
}
return responseBuilder.build();
}
private void simulateDatabaseConnectionVerification() {
if (!databaseUp) {
throw new IllegalStateException("Cannot contact database");
}
}
}
4.4.4. @Startup 注释示例 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
以下是在应用中使用 @Startup 注释的示例:
@Startup
@ApplicationScoped
public class StartupHealthCheck implements HealthCheck {
@Override
public HealthCheckResponse call() {
return HealthCheckResponse.up("Application started");
}
}