程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

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

发布于2021-08-03 08:34     阅读(496)     评论(0)     点赞(28)     收藏(2)


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

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


解决方案


我不相信有任何类型的配置可以开箱即用来实现这一点,但是您应该能够编写自己的 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 来自动配置此端点以替换执行器的默认健康检查。



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/254444/ab27d12b4a0eb4f78e6c/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

28 0
收藏该文
已收藏

评论内容:(最多支持255个字符)