MicroProfile Health Check

このエントリは以下のエントリをベースにしています。
This entry is based on the entry by Hayri Cicek
https://kodnito.com/posts/microprofile-health-check/

ヘルスチェックを使ってサービスが実行中、停止中、ディスク領域の不足、もしくはデータベース接続での問題発生などを判断してみましょう。このチュートリアルではEclipse MicroProfile Starterを使って新規プロジェクトを生成します。

Eclipse MicroProfile Starter

Eclipse MicroProfile Starterに移動して以下の手順に従い新規プロジェクトを生成します。

MicroProfile Starter
https://start.microprofile.io/

その他の詳細は以下の通りです。

groupId: お好みで(上図ではデフォルトのcom.example)
artifactId: お好みで(上図ではhealth_example)
MicroProfile Version
: MP 2.1(実際には1.2以上で利用可能)
MicroProfile Server: Open Liberty もしくは Thorntail v2(お好みで)
Examples for specifications: Health Checksだけチェック

これでおしまいです。あとはDownloadボタンをクリックして、新たなプロジェクトをダウンロードしましょう。Zipフィルを展開してお好みのIDEでプロジェクトを開いてください。Eclipse MicroProfile Starterが{groupId}.{artifactId}.healthパッケージ内にhealth checkのサンプルを生成していることがわかります。

ServiceHealthCheck.javaを開いてGeneratorが生成した内容を確認しましょう。
@Health@ApplicationScoped で注釈がついたBeanを自動的に発見します。ServiceHealthCheckはHealthCheckインターフェースを実装し、 call()メソッドをオーバーライドしています。

package com.example.health_example.health;

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

import javax.enterprise.context.ApplicationScoped;

@Health
@ApplicationScoped
public class ServiceHealthCheck implements HealthCheck {

    @Override
    public HealthCheckResponse call() {

        return HealthCheckResponse.named(ServiceHealthCheck.class.getSimpleName()).up().build();

    }
}

ターミナルを開いてプロジェクトのディレクトリへ移動し、以下のコマンドを実行してアプリケーションを開始してみましょう。

$ mvn clean package && java -jar target/{artifactId}.jar

http://localhost:8181/health を開くと、シンプルなヘルスチェック機能が実装されていることがわかります。

それでは、以下のようにServiceHealthCheck.javaに手をいれてみましょう。

package com.example.health_example.health;

import org.eclipse.microprofile.health.Health;
import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;

import javax.enterprise.context.ApplicationScoped;

@Health
@ApplicationScoped
public class ServiceHealthCheck implements HealthCheck {
    @Override
    public HealthCheckResponse call() {
        HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named(ServiceHealthCheck.class.getSimpleName());

        responseBuilder.withData("memory", Runtime.getRuntime().freeMemory());
        responseBuilder.withData("availableProcessors", Runtime.getRuntime().availableProcessors());

        return responseBuilder.state(true).build();
    }
}

先ほど動かしたアプリケーションは停止して、再起動します。

$ mvn clean package && java -jar target/{artifactId}.jar

もう一度 http://localhost:8181/health を確認すると、新たなヘルスチェック情報が出ていることがわかります。

非常に間単にMicroProfile Healthを使ってヘルスチェック機能をアプリケーションに追加できます。この短時間のチュートリアルで、MicroProfile Healthをすぐに使い始められること、そして空きメモリや利用可能なプロセッサのシンプルなチェックを追加できることがわかりました。しかしながら、こうしたチェックだけにとどまらず、無数のヘルスチェックをサービスに追加できます。

Health CheckはMicroProfile 1.2で導入済みと書きましたが、その例としてHelidonで確認してみます(HelidonはMicroProfile 1.2に準拠しています)。quickstartで十分ですが、SEではなく、MPを使うことに注意してください。

Mavenでプロジェクトを生成します。

mvn archetype:generate -DinteractiveMode=false \ 
    -DarchetypeGroupId=io.helidon.archetypes \
    -DarchetypeArtifactId=helidon-quickstart-mp \ 
    -DarchetypeVersion=1.0.0 \ 
    -DgroupId=io.helidon.examples \ 
    -DartifactId=quickstart-mp \ 
    -Dpackage=io.helidon.examples.quickstart.mp

プロジェクトディレクトリに移動して、以下のコマンドでビルド、実行します。

$ mvn clean package && java -jar target/quickstart-mp.jar

QuickStartの動作を確認する場合には、 http://localhost:8080/greet を呼び出しますが、Health Checkの場合は、 http://localhost:8080/health を呼び出します。

すでにヒープの空きなどを確認できるようになっています。必要であれば、出力したい情報を追加できます。

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中