我已经尝试了在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,我仍然会获得标准的默认运行状况信息。我的代码被完全忽略。
localhost:port/application/health
当我使用通过实现实现自定义健康信息的“传统方式”时HealthIndicator,它可以按预期工作,该健康信息将使用给定的细节进行修饰:
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")解决方案正常工作?
@Endpoint(id = "health")
我的意图 不是 创建定制的执行器myhealth,而是定制现有的health执行器。基于文档,我希望达到与实施HealthIndicator相同的结果。我在这个假设上错了吗?
myhealth
health
Maven配置pom.xml包含:
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包含:
application.properties
endpoints.health.enabled=true endpoints.autoconfig.enabled=true endpoints.autoconfig.web.enabled=true
新的端点ID必须是 唯一的, 并且不应与现有的执行器端点相同。如果尝试将下面显示的示例的ID更改为health,则将出现以下异常:
java.lang.IllegalStateException: Found two endpoints with the id 'health'
上面有关使用@Bean注释声明端点类的注释是正确的。
@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,
application.yml
endpoints: myhealth: enabled: true
启动应用程序后,您应该可以访问处的新执行器端点http://<host>:<port>/application/myhealth。
http://<host>:<port>/application/myhealth
您应该期望得到类似于以下所示的响应,
{ "MyStatus": "is happy" }
在这里可以找到完整的工作示例。