【发布时间】:2016-08-06 04:45:19
【问题描述】:
我在 spring boot 项目中使用 spring boot actuator health with spring fox swagger。我在我的 Application.java 类中使用下面。
@Autowired
private HealthAggregator healthAggregator;
@Autowired
private Map<String, HealthIndicator> healthIndicators;
@Bean
public com.health.TestMeHealthEndpoint getHealthEndpoint() {
return new com.health.TestMeHealthEndpoint(healthAggregator, healthIndicators);
}
@Bean
public Docket testMeApi() {
return new Docket(DocumentationType.SWAGGER_2).useDefaultResponseMessages(false).apiInfo(apiInfo()).select()
.paths(testMePaths()).build();
}
private Predicate<String> testMePaths() {
return or(regex("/api/myservice1"), regex("/health"));
}
但是当我检查 swagger ui 时,我看到了多个健康端点,所有类型的 http 方法包括 POST、DELETE、OPTIONS 等。对于在 REST 控制器中实现的 myservice1,它只显示 GET 方法。
TestMeHealthEndpoint 扩展 AbstractEndpoint 并使用自定义健康信息覆盖调用方法。
我只想看看是不是健康路由的GET方法?
添加TestMeHealthEndpoint的来源:
@ConfigurationProperties(prefix = "endpoints.health", ignoreUnknownFields = true)
public class TestMeHealthEndpoint extends AbstractEndpoint<Health> {
//Some getter and setters for api name , version etc
public TestMeHealthEndpoint (final HealthAggregator healthAggregator,
final Map<String, HealthIndicator> healthIndicators) {
super("health", false);
final CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(healthAggregator);
for (final Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue());
}
this.healthIndicator = healthIndicator;
}
@Override
public Health invoke() {
final Health health = new Health();
health.setStatus(this.healthIndicator.health().getStatus().getCode());
health.setName(this.apiName);
health.setVersion(this.apiVersion);
final UriComponentsBuilder path = ServletUriComponentsBuilder.fromCurrentServletMapping()
.path(this.managementContextPath).pathSegment(this.getId());
health.add(new Link(path.build().toUriString()).withSelfRel());
return health;
}
}
【问题讨论】:
-
请出示源代码TestMeHealthEndpoint
-
@shazin 请完善编辑问题
-
这个 AbstractEndpoint 是什么?您使用的是哪个框架?
-
弹簧靴执行器
标签: java spring spring-boot swagger spring-boot-actuator