我们目前有一个框架,用于检查我们的微服务,并检查URL以获取有关应用程序健康状况和信息的信息。这里的限制是该框架只能检查1个URL。
我想做的是将/health和/info的信息合并到一个URL中,并使/health还显示/info的信息,尤其是部署应用程序的版本信息。像这样的功能是否可以直接使用,还是我应该创建自己的健康检查来显示此信息?
我们目前有一个框架,用于检查我们的微服务,并检查URL以获取有关应用程序健康状况和信息的信息。这里的限制是该框架只能检查1个URL。
我想做的是将/health和/info的信息合并到一个URL中,并使/health还显示/info的信息,尤其是部署应用程序的版本信息。像这样的功能是否可以直接使用,还是我应该创建自己的健康检查来显示此信息?
我不相信有任何开箱即用的配置可以实现这一点,但是您应该能够编写自己的healthendpoint来实现这一点。以下方法对我有效。
Spring 1.5.x
@Component
public class InfoHealthEndpoint extends HealthEndpoint {
@Autowired
private InfoEndpoint infoEndpoint;
public InfoHealthEndpoint(HealthAggregator healthAggregator, Map<String, HealthIndicator> healthIndicators) {
super(healthAggregator, healthIndicators);
}
@Override
public Health invoke() {
Health health = super.invoke();
return new Health.Builder(health.getStatus(), health.getDetails())
.withDetail("info", infoEndpoint.invoke())
.build();
}
}
Spring 2.x
public class InfoHealthEndpoint extends HealthEndpoint {
private InfoEndpoint infoEndpoint;
public InfoHealthEndpoint(HealthIndicator healthIndicator, InfoEndpoint infoEndpoint) {
super(healthIndicator);
this.infoEndpoint = infoEndpoint;
}
@Override
@ReadOperation
public Health health() {
Health health = super.health();
return new Health.Builder(health.getStatus(), health.getDetails())
.withDetail("info", this.infoEndpoint.info())
.build();
}
}
@Configuration
class HealthEndpointConfiguration {
@Bean
@ConditionalOnEnabledEndpoint
public HealthEndpoint healthEndpoint(HealthAggregator healthAggregator,
HealthIndicatorRegistry registry, InfoEndpoint infoEndpoint) {
return new InfoHealthEndpoint(
new CompositeHealthIndicator(healthAggregator, registry), infoEndpoint);
}
}
接下来,如果您需要将此添加到多个微服务中,则可以创建自己的JAR文件,将此端点自动配置为替换执行器默认的健康检查。