我已尝试在Spring Boot 2.0.0.M5中自定义健康执行器的新方法,如下所述: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();
}
}
但是,当我将HTTP GET运行到localhost:port/application/health
时,我仍然会获得标准的默认健康信息。我的代码完全被忽略了。
当我使用"传统方式"通过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
答案 0 :(得分:5)
新的端点ID必须唯一,并且不应与现有的执行器端点相同。如果尝试将下面显示的示例的ID更改为health
,则会出现以下异常:
java.lang.IllegalStateException: Found two endpoints with the id 'health'
上述关于使用@Bean
注释声明端点类的注释是正确的。
health
端点在Spring Boot 2.0中没有更改。您仍然需要实施HealthIndicator
来添加自定义值。以下是在Spring Boot 2.0中创建自定义Actuator端点所需的更改。
包含您的自定义信息的域。
@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class MyHealth {
private Map<String, Object> details;
@JsonAnyGetter
public Map<String, Object> getDetails() {
return this.details;
}
}
声明myhealth
端点,
@Endpoint(id = "myhealth")
public class MyHealthEndpoint {
@ReadOperation
public MyHealth health() {
Map<String, Object> details = new LinkedHashMap<>();
details.put("MyStatus", "is happy");
MyHealth health = new MyHealth();
health.setDetails(details);
return health;
}
}
myhealth
端点的扩展名
@WebEndpointExtension(endpoint = MyHealthEndpoint.class)
public class MyHealthWebEndpointExtension {
private final MyHealthEndpoint delegate;
public MyHealthWebEndpointExtension(MyHealthEndpoint delegate) {
this.delegate = delegate;
}
@ReadOperation
public WebEndpointResponse<MyHealth> getHealth() {
MyHealth health = delegate.health();
return new WebEndpointResponse<>(health, 200);
}
}
配置将两个新创建的执行器类暴露为bean,
@Configuration
public class ActuatorConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
public MyHealthEndpoint myHealthEndpoint() {
return new MyHealthEndpoint();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
@ConditionalOnBean({MyHealthEndpoint.class})
public MyHealthWebEndpointExtension myHealthWebEndpointExtension(
MyHealthEndpoint delegate) {
return new MyHealthWebEndpointExtension(delegate);
}
}
对application.yml
,
endpoints:
myhealth:
enabled: true
启动应用程序后,您应该能够在http://<host>:<port>/application/myhealth
访问新的执行器端点。
您应该期待类似于下面所示的响应,
{
"MyStatus": "is happy"
}
可以找到一个完整的工作示例here。
答案 1 :(得分:-2)
像这样提供您自己的@WebEndpoint
@Component
@WebEndpoint(id = "acmehealth")
public class AcmeHealthEndpoint {
@ReadOperation
public String hello() {
return "hello health";
}
}
和
通过application.properties
:
management.endpoints.web.exposure.include=acmehealth
management.endpoints.web.path-mapping.health=internal/health
management.endpoints.web.path-mapping.acmehealth=/health
这将完全覆盖/health,而不仅仅是像自定义HealthIndicator
那样将信息添加到现有/health中。问题是,您想要什么,因为@Endpoint(id = "health")
和“我的意图不是创建自定义执行器myhealth,而是自定义现有的健康执行器”。但是您可以 在AcmeHealthEndpoint中使用现有的HealthEndpoint
并完成这两项操作:
@Component
@WebEndpoint(id = "prettyhealth")
public class PrettyHealthEndpoint {
private final HealthEndpoint healthEndpoint;
private final ObjectMapper objectMapper;
@Autowired
public PrettyHealthEndpoint(HealthEndpoint healthEndpoint, ObjectMapper objectMapper) {
this.healthEndpoint = healthEndpoint;
this.objectMapper = objectMapper;
}
@ReadOperation(produces = "application/json")
public String getHealthJson() throws JsonProcessingException {
Health health = healthEndpoint.health();
ObjectWriter writer = objectMapper.writerWithDefaultPrettyPrinter();
return writer.writeValueAsString(health);
}
@ReadOperation
public String prettyHealth() throws JsonProcessingException {
return "<html><body><pre>" + getHealthJson() + "</pre></body></html>";
}
}