繁体   English   中英

将 Spring Boot Actuator /health 和 /info 合二为一

[英]Combine Spring Boot Actuator /health and /info into one

我们目前有一个框架来检查我们的微服务,并检查一个 URL 以获取有关健康和应用程序信息的信息。 这里的限制是这个框架只能检查 1 个 URL。

我想要做的是将/health和/info的信息合二为一,让/health也显示/info的信息,最显着的是部署的应用程序的版本。 这样的事情是否可以开箱即用,还是我应该创建自己的健康检查来显示此信息?

您可以将以下代码段添加到application.yml文件中,以将/ health和/ info端点集成为/ info端点。

info:
  build:
    artifact: @project.artifactId@
    version: @project.version@
    status: UP

我不认为您可以直接进行任何配置来实现此目标,但是您应该能够编写自己的Healthendpoint来实现此目标。 下面为我​​工作。

春季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();
    }

}

春天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,该jar可自动配置此端点以替换执行器默认的运行状况检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM