Spring Boot 自定义运行状况指示器未显示

Tom*_*ner 8 java spring spring-boot spring-boot-actuator

我相信遵循了互联网上几乎所有的教程并阅读了很多 SO 答案,但我仍然陷入困境。

1.简单的健康检查

@Component
public class HealthCheck implements HealthIndicator {

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

}
Run Code Online (Sandbox Code Playgroud)

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

management.endpoints.web.exposure.include: "*"
management.endpoint.health.show-details: ALWAYS
Run Code Online (Sandbox Code Playgroud)

3. Spring-boot-actuator 作为依赖项包含在内:

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'
Run Code Online (Sandbox Code Playgroud)

4.最近发布的Spring Boot:

id 'org.springframework.boot' version '2.2.0.RELEASE'
Run Code Online (Sandbox Code Playgroud)

5. 主应用程序类注释为@SpringBootApplication(隐含地带有@ComponentScan)。

management.endpoints.web.exposure.include: "*"
management.endpoint.health.show-details: ALWAYS
Run Code Online (Sandbox Code Playgroud)

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

{
    "status": "UP",
    "components": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 250685575168,
                "free": 99168997376,
                "threshold": 10485760
            }
        },
        "ping": {
            "status": "UP"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有什么我可能错过的吗?

Tom*_*ner 3

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

@SpringBootApplication(scanBasePackages="com.example.*")
Run Code Online (Sandbox Code Playgroud)

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