Micronaut 活性始终未知

Micronaut liveness always unknown

我目前正在尝试向我的服务添加 livenessreadiness 端点以将其部署在 k8s 上。

我使用的是最新的 micronaut 版本 (2.5.4),并具有以下设置:

application.yml:

micronaut:
  application:
    name: sandbox

endpoints:
  health:
    enabled: true
    sensitive: false
    details-visible: ANONYMOUS

build.gradle:

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.4.32"
    id("org.jetbrains.kotlin.kapt") version "1.4.32"
    id("com.github.johnrengelman.shadow") version "7.0.0"
    id("io.micronaut.application") version "1.5.0"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.4.32"
}

version = "0.1"
group = "com.example"

repositories {
    mavenCentral()
}

micronaut {
    runtime("netty")
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.example.*")
    }
}

dependencies {
    implementation("io.micronaut:micronaut-http-client")
    implementation("io.micronaut:micronaut-management")
    implementation("io.micronaut:micronaut-runtime")
    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    implementation("javax.annotation:javax.annotation-api")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    runtimeOnly("ch.qos.logback:logback-classic")
    implementation("io.micronaut:micronaut-validation")

    runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")

}


application {
    mainClass.set("com.example.ApplicationKt")
}
java {
    sourceCompatibility = JavaVersion.toVersion("11")
}

tasks {
    compileKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }
    compileTestKotlin {
        kotlinOptions {
            jvmTarget = "11"
        }
    }


}

每当我到达终点 localhost:8080/health/liveness 时,我得到的响应是:

{"name":"sandbox","status":"UNKNOWN"}

但是,当我调用 localhost:8080/health/readiness 时,我得到了正确的响应:

{"name":"sandbox","status":"UP","details":{"compositeDiscoveryClient()":{"name":"sandbox","status":"UP"},"diskSpace":{"name":"sandbox","status":"UP","details":{"total":499963174912,"free":384382533632,"threshold":10485760}},"service":{"name":"sandbox","status":"UP"}}}

我似乎遗漏了一些东西,但我在 Micronaut 文档中找不到任何关于此的内容...

在评论的帮助下,我设法解决了这个问题。

要使 liveness 正常工作,您需要添加类似于以下形式的 Liveness 实现:

@Singleton
@Liveness
class LivenessIndicator : HealthIndicator {
    override fun getResult(): Publisher<HealthResult> =
        Flowable.just(HealthResult.builder(LIVENESS_NAME).status(HealthStatus.UP).build())

    companion object {
        private const val LIVENESS_NAME = "liveness"
    }
}

当前接受的答案不会报告反映真实应用程序状态的状态,因为您强制您的应用程序始终 return UP 无论其中发生什么错误。

正确的设置应该适合您的 application.yaml:

endpoints:
  all:
    # Expose all management endpoints on a different port. This allows us to control access
    port: 8081
    sensitive: false
  info:
    enabled: true
    sensitive: false
  health:
    details-visible: ANONYMOUS
    enabled: true
    sensitive: false

你的 k8s deployment 应该正确设置,例如

      ports:
        - name: management
          containerPort: 8081
          protocol: TCP
      livenessProbe:
        failureThreshold: 3
        httpGet:
          path: /health
          port: 8081
        initialDelaySeconds: 50
      readinessProbe:
        failureThreshold: 3
        httpGet:
          path: /info
          port: 8081
        initialDelaySeconds: 15

如果这不起作用,请检查您的网络策略