如何在 Spring Boot 中聚合健康指标 [英] How to aggregate health indicators in Spring boot

查看:34
本文介绍了如何在 Spring Boot 中聚合健康指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用此 Baeldung 文章为 Spring 2.0.4 应用程序添加了 Actuator 支持一>.在第 4.4 节中,它谈到了

I've added Actuator support for my Spring 2.0.4 application using this Baeldung article. In section 4.4 it talks about

健康指标的一个方便特性是我们可以将它们聚合为层次结构的一部分

A handy feature of health indicators is that we can aggregate them as part of a hierarchy

但它没有讨论如何进行这种聚合.我也找不到任何有关如何执行此操作的文档.

but it doesn't go into any discussion of how to do this aggregation. Nor have I been able to find any documentation on how to do this.

问题有没有人知道有关创建此类聚合的教程、示例或其他文档?

Question Do any of you know of a tutorial, example or other documentation on creating such an aggregation?

更多信息我的应用程序中有一个依赖于几个子组件的服务.仅当所有这些子组件都关闭时,才认为服务本身已关闭.只要有一个,那么服务就启动了.当前使用正常的 HealthIndicator 机制,如果其中一个子组件关闭,它会将服务器标记为关闭.

More Info I have a service in my application which relies on several sub-components. The service itself is only considered down if all these sub-components are down. So long as one is up then the service is up. Currently using the normal HealthIndicator mechanism if one of the sub-components is down it marks the server as down.

似乎我想使用 CompositeHealthIndicator 但不清楚如何在没有系统拾取的情况下创建子 HealthIndicators.警告 是这些子组件中的每一个都使用 @Scheduled 注释,我的理解是为了使其工作该类必须使用 @Component 注释(或一些类似的)将导致它被创建并被吸收到应用程序健康中.

It seems I would want to use the CompositeHealthIndicator but it's not clear how I create the child HealthIndicators without the system picking them up. The caveat is that each of these sub-components uses the @Scheduled annotation, my understanding is that in order for that to work the class must use the @Component annotation(or some such) which will cause it to be created and sucked up into the application health.

澄清我添加了执行器,健康 URL 如下所示:

Clarification I have added actuators and the health URL comes up as this:

{"status":"UP","details":{"MyServ1":{"status":"UP","details":{"Latency":...}},"MyServ2":{"status":"UP","details":{"Latency":...}},"diskSpace":{"status":"UP","details":{"total":...,"免费":...,"阈值":...}}}}

{"status":"UP","details":{"MyServ1":{"status":"UP","details":{"Latency":...}},"MyServ2":{"status":"UP","details":{"Latency":...}},"diskSpace":{"status":"UP","details":{"total":...,"free":...,"threshold":...}}}}

但如果MyServ1"或MySrv2"关闭,则整体状态关闭,但我只希望在diskSpace"关闭时发生这种情况ORMyServ1"MyServ2"已关闭.

but if 'MyServ1' or 'MySrv2' are down the overall status is down, but I only want that to happen if 'diskSpace' is down OR 'MyServ1' and 'MyServ2' is down.

看起来 CompositeHealthIndicator 将是合适的类,只是不清楚我如何为其创建儿童健康指标(只需使用 new)?

It would appear that CompositeHealthIndicator would be the appropriate class for this, it is just unclear how I create the children health indicators for it (just use new)?

提前致谢

推荐答案

将每个健康指标的状态聚合为一个整体状态是通过 org.springframework.boot.actuate.health.HealthAggregator<的实现完成的/代码>.Spring Boot 自动配置 OrderedHealthAggregator 的一个实例.如果您提供自己的实现 HealthAggregator 的 bean,自动配置的聚合器将退出以支持您的自定义实现.

Aggregating the statuses from each health indicator into a single overall status is done by an implementation of org.springframework.boot.actuate.health.HealthAggregator. Spring Boot auto-configures an instance of OrderedHealthAggregator. If you provide your own bean that implements HealthAggregator the auto-configured aggregator will back off in favour of your custom implementation.

聚合器的 aggregate 方法使用 Map 调用,其中键是健康指标的名称,值是它们的状态.了解子组件健康指标的名称后,您就可以为它们执行自定义聚合.

The aggregator's aggregate method is called with a Map<String, Status> where the keys are the names of the health indicators and the values are their statuses. Knowing the names of your sub-components' health indicators should allow you to perform custom aggregation for them.

这篇关于如何在 Spring Boot 中聚合健康指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆