4.4. MicroProfile Health の開発
4.4.1. カスタムヘルスチェックの例 リンクのコピーリンクがクリップボードにコピーされました!
microprofile-health-smallrye サブシステムによって提供されるデフォルトの実装は基本的なヘルスチェックを実行します。サーバーやアプリケーションの状態の詳細情報はカスタムヘルスチェックに含まれる可能性があります。クラスレベルで org.eclipse.microprofile.health.Liveness、org.eclipse.microprofile.health.Readiness、または org.eclipse.microprofile.health.Startup アノテーションを含む Jakarta コンテキスト and Dependency Injection は、実行時に自動的に検出されて呼び出されます。
以下の例は、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"
}
]
}
}
次のコマンドを使用して、liveness、readiness、および startup のチェックを行うことができます。
-
/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 チェックでエラーが報告されます。
@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");
}
}