如何使@Endpoint(id ="health")在Spring Boot 2.0中正常工作? [英] How to make the `@Endpoint(id = "health")` working in Spring Boot 2.0?
问题描述
我尝试了在Spring Boot 2.0.0.M5中自定义运行状况执行器的新方法,如下所述:
I have tried the new way of customizing the health Actuator in Spring Boot 2.0.0.M5, as described here: 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
时,我仍然会获得标准的默认运行状况信息.我的代码被完全忽略了.
However, when I run HTTP GET to localhost:port/application/health
, I still get the standard default health info. My code is completely ignored.
当我使用传统方式"通过实现HealthIndicator
来自定义健康信息时,它可以按预期工作,并且使用给定的详细信息修饰健康信息:
When I use the "traditional way" of customizing the health info via implementation of HealthIndicator
, it works as expected, the health information is decorated with the given details:
@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")
解决方案能够正常工作,我还需要配置和/或实施更多什么?
QUESTION: What more shall I configure and/or implement to make the @Endpoint(id = "health")
solution working?
我的目的不是 创建自定义的执行器myhealth
,而是自定义现有的health
执行器.基于文档,我希望达到与实施HealthIndicator相同的结果.我在这个假设上错了吗?
My intention is not to create a custom actuator myhealth
, but to customize the existing health
actuator. Based on the documentation I expect to reach the same result as by implementing HealthIndicator. Am I wrong in that assumption?
Maven配置pom.xml
包含:
The Maven configuration pom.xml
contains:
<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
包含:
The Spring Boot configuration application.properties
contains:
endpoints.health.enabled=true
endpoints.autoconfig.enabled=true
endpoints.autoconfig.web.enabled=true
推荐答案
更新
-
文档不太清楚.尝试以现有的运行状况端点为例来说明新的端点基础结构.
Update
The documentation on the new Spring Actuator Endpoints is not very lucid. It's trying to explain the new endpoint infrastructure with the existing health endpoint as an example.
新的端点ID必须唯一,并且不得与现有的执行器端点相同.如果尝试将下面显示的示例的ID更改为
health
,则将出现以下异常:A new endpoint ID has to be unique and shouldn't be same as an existing actuator endpoint. If one tries to the change the ID of the example shown below to
health
, one will get the following exception:java.lang.IllegalStateException: Found two endpoints with the id 'health'
-
上面关于使用
@Bean
注释声明终结点类的注释是正确的. The above comment about declaring the endpoint classes with
@Bean
annotation is correct.在Spring Boot 2.0中自定义
health
终结点没有更改.您仍然必须实现HealthIndicator
才能添加自定义值.Customizing the
health
endpoint hasn't changed in Spring Boot 2.0. You still have to implementHealthIndicator
to add custom values.以下是在Spring Boot 2.0中创建自定义Actuator端点所需的更改.
Here are the changes needed to create a custom Actuator endpoint in Spring Boot 2.0.
包含您的自定义信息的域.
The domain containing your custom information.
@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的配置,
Actuator Configuration
Configuration to expose the two newly created actuator classes as beans,
@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
访问新的执行器端点.Once you start your application, you should be able to access the newly actuator endpoint at
http://<host>:<port>/application/myhealth
.您应该期望得到与以下所示类似的响应,
You should expect a response similar to one shown below,
{ "MyStatus": "is happy" }
可以在此处找到完整的工作示例.
A complete working example can be found here.
这篇关于如何使@Endpoint(id ="health")在Spring Boot 2.0中正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!