【发布时间】:2018-03-29 13:03:13
【问题描述】:
我已经尝试过在 Spring Boot 2.0.0.M5 中自定义 health Actuator 的新方法,如下所述:https://spring.io/blog/2017/08/22/introducing-actuator-endpoints-in-spring-boot-2-0:
@Endpoint(id = "health")
public class HealthEndpoint {
@ReadOperation
public Health health() {
return new Health.Builder()
.up()
.withDetail("MyStatus", "is happy")
.build();
}
}
但是,当我对localhost:port/application/health 运行 HTTP GET 时,我仍然会得到标准的默认健康信息。我的代码完全被忽略了。
当我通过HealthIndicator 的实现使用自定义健康信息的“传统方式”时,它按预期工作,健康信息用给定的细节装饰:
@Component
public class MyHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return new Health.Builder()
.up()
.withDetail("MyStatus 1.1", "is happy")
.withDetail("MyStatus 1.2", "is also happy")
.build();
}
}
问题:我还应该配置和/或实施什么才能使@Endpoint(id = "health") 解决方案工作?
我的意图不是创建自定义执行器myhealth,而是自定义现有的health 执行器。根据文档,我希望达到与实施 HealthIndicator 相同的结果。我的假设错了吗?
Maven 配置pom.xml 包含:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M5</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
Spring Boot 配置application.properties 包含:
endpoints.health.enabled=true
endpoints.autoconfig.enabled=true
endpoints.autoconfig.web.enabled=true
【问题讨论】:
-
您是否将
HealthEndpoint配置为bean?为此,您通常会为其声明一个@Bean方法,或者,如果您使用组件扫描,请使用@Component注释它 -
@AndyWilkinson 好吧,我希望
@Endpoint-annotated 类在 Spring Boot 中被自动扫描。但是,现在我尝试在@Endpoint之前添加@Component并且没有区别。 -
@AndyWilkinson:Indra Basak 已经在下面的评论中回答我,我的失败是基于错误的假设。所以可能你也不能让它工作:)我编辑了我的问题,所以它应该更清楚。 Indra:“文档试图以现有的健康端点为例来解释新的端点基础设施。新的端点 ID 必须是唯一的,并且不应与现有的执行器端点相同。”
标签: java spring spring-boot spring-boot-actuator