12.8. カスタムヘルスチェックの追加
追加のカスタムヘルスチェックを提供して、リクエストを処理する準備が整う前に Karaf サーバーがユーザートラフィックを受信しないようにすることができます。カスタムヘルスチェックを有効にするには、io.fabric8.karaf.checks.HealthChecker
または io.fabric8.karaf.checks.ReadinessChecker
インターフェイスを実装し、これらのオブジェクトを OSGi レジストリーに登録する必要があります。
手順
以下の mvn 依存関係をプロジェクトの
pom.xml
ファイルに追加します。pom.xml
<dependency> <groupId>io.fabric8</groupId> <artifactId>fabric8-karaf-checks</artifactId> </dependency>
<dependency> <groupId>io.fabric8</groupId> <artifactId>fabric8-karaf-checks</artifactId> </dependency>
Copy to Clipboard Copied! 注記SCR を使用すると、最も簡単に OSGi レジストリーでオブジェクトを作成および登録できます。
例
以下は、ヘルスチェックを実行して空きのディスク領域が確保されるようにする例になります。
import io.fabric8.karaf.checks.*; import org.apache.felix.scr.annotations.*; import org.apache.commons.io.FileSystemUtils; import java.util.Collections; import java.util.List; @Component( name = "example.DiskChecker", immediate = true, enabled = true, policy = ConfigurationPolicy.IGNORE, createPid = false ) @Service({HealthChecker.class, ReadinessChecker.class}) public class DiskChecker implements HealthChecker, ReadinessChecker { public List<Check> getFailingReadinessChecks() { // lets just use the same checks for both readiness and health return getFailingHeathChecks(); } public List<Check> getFailingHealthChecks() { long free = FileSystemUtils.freeSpaceKb("/"); if (free < 1024 * 500) { return Collections.singletonList(new Check("disk-space-low", "Only " + free + "kb of disk space left.")); } return Collections.emptyList(); } }
import io.fabric8.karaf.checks.*;
import org.apache.felix.scr.annotations.*;
import org.apache.commons.io.FileSystemUtils;
import java.util.Collections;
import java.util.List;
@Component(
name = "example.DiskChecker",
immediate = true,
enabled = true,
policy = ConfigurationPolicy.IGNORE,
createPid = false
)
@Service({HealthChecker.class, ReadinessChecker.class})
public class DiskChecker implements HealthChecker, ReadinessChecker {
public List<Check> getFailingReadinessChecks() {
// lets just use the same checks for both readiness and health
return getFailingHeathChecks();
}
public List<Check> getFailingHealthChecks() {
long free = FileSystemUtils.freeSpaceKb("/");
if (free < 1024 * 500) {
return Collections.singletonList(new Check("disk-space-low", "Only " + free + "kb of disk space left."));
}
return Collections.emptyList();
}
}