【问题标题】:Spring Boot custom health indicator not showing upSpring Boot 自定义运行状况指示器未显示
【发布时间】:2023-03-27 00:42:01
【问题描述】:

我相信几乎遵循了互联网上的所有教程并阅读了许多 SO 答案,但我仍然被卡住了。

1。一个简单的健康检查

@Component
public class HealthCheck implements HealthIndicator {

    @Override
    public Health health() {
        return Health.up().build();
    }

}

2。 Application.yaml 配置为显示所有详细信息:

management.endpoints.web.exposure.include: "*"
management.endpoint.health.show-details: ALWAYS

3。 Spring-boot-actuator 包含为依赖项:

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'

4。最新版本的 Spring Boot:

id 'org.springframework.boot' version '2.2.0.RELEASE'

5。主应用程序类使用 @SpringBootApplication 注释(隐含带 @ComponentScan)。

@SpringBootApplication
public class PaymentServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(PaymentServiceApplication.class, args);
    }

}

我的自定义运行状况检查必须针对 Apache Kafka 进行测试,但为简洁起见,我跳过了详细信息。 尽管如此,调用/actuator/health 端点我得到相同的默认结果:

{
    "status": "UP",
    "components": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 250685575168,
                "free": 99168997376,
                "threshold": 10485760
            }
        },
        "ping": {
            "status": "UP"
        }
    }
}

有什么我可能错过的吗?

【问题讨论】:

  • 我试过了,使用了健康端点。你能做一个system.out吗?在那里再试一次......如果控制台中有东西。
  • 验证组件是否实际配置为spring bean。如果没有组件扫描,仅仅注释为@Component 并不能保证它成为一个bean。
  • @Compass,我已经更新了这个问题。主类标注@SpringBootApplication,开启组件扫描。
  • 您的 HealthIndicator 类没有添加任何额外信息。

标签: java spring spring-boot spring-boot-actuator


【解决方案1】:

我找到了解决方案,但不确定原因。该类确实没有注册为 bean,令人惊讶的是,显式添加基本包属性有助于:

@SpringBootApplication(scanBasePackages="com.example.*")

有趣的是,没有必要在不同的项目中执行上述操作。

【讨论】:

  • 默认的 ComponentScan 将根据 Spring Boot 应用程序的包路径及其子目录进行扫描,但不会分层扫描。根据您的包裹安排,这很可能是一个原因,即如果您有一个子应用程序。
【解决方案2】:

有趣的是,应用程序容器无法识别 HealthIndicator 的基本包,而是将类声明为@component Stereotype。

修复问题-在主Spring启动应用程序类中声明HealthIndicator的基础包: @SpringBootApplication (scanBasePackages = "你的类 HealthCheck 的基础包") 公共类 PaymentServiceApplication {

public static void main(String[] args) {
    SpringApplication.run(PaymentServiceApplication.class, args);
}

}

编辑:注意-上面声明的 base package="" 不是主要的 spring boot 类是没有必要的。

您必须停止服务器,然后清理构建 maven 应用程序并重新启动服务器并重新运行应用程序

您现在可以运行查找您的自定义健康端点。

【讨论】:

    猜你喜欢
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多