发布于2021-08-03 08:34 阅读(512) 评论(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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!