Spring Boot Actuator:缓存 /actuator/health/readiness 和 /actuator/health/custom 健康检查结果

发布于 2025-01-09 20:37:24 字数 596 浏览 5 评论 0原文

因此,我已将 Spring Boot Actuator 添加到我的应用程序中,并在 application.properties 属性 management.endpoint.health.cache.time-to-live=120s 中指定缓存健康检查结果。因此,当我调用 /actuator/health 时,结果会被缓存并且效果很好。

当我调用 /actuator/health/readiness 或自定义创建的组 /actuator/health/deep 时,问题就开始了,此请求结果不会被缓存。我浏览了 Spring Docs< /a> 只找到主要健康终点的信息,没有找到特定群体的信息。

所以我的问题是:我错过了什么吗?或者如何为特定的 Spring Boot 执行器端点实现缓存?

谢谢

So I have added the Spring Boot Actuator to my application and I specified in the application.properties property management.endpoint.health.cache.time-to-live=120s to cache health check results. So when I call /actuator/health, the result is cached and it's good.

The problem starts when I call /actuator/health/readiness or custom created group /actuator/health/deep this request results are not cached. I went through Spring Docs and found only information for the main health endpoint, nothing for specific groups.

So my question is: did I miss sth? Or how can I implement caching for specific spring boot actuator endpoints?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘 2025-01-16 20:37:24

所以我没有找到开箱即用的解决方案,所以决定采用 AOP 方法。

我找到了管理健康休息端点的 HealthEndpointWebExtension 类。
该类有两个健康方法,其中一个调用公共 /health 端点,第二个调用 health/

因此,我围绕第二种方法创建了 Aspect,它允许我为我需要的端点添加缓存。

@Around("execution(@org.springframework.boot.actuate.endpoint.annotation.ReadOperation * org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(..,java.lang.String...))) && args(.., path)")
public Object cache(ProceedingJoinPoint joinPoint, String... path) throws Throwable {
    // Logic to get cache per endpoint and process if cache specifed.
}

最后 application.properties 看起来像这样:

// out of the box
management.endpoint.health.cache.time-to-live=120s 

// customly created properties
management.endpoint.health.group.readiness.cache.time-to-live=60s
management.endpoint.health.group.deep.cache.time-to-live=120s

So I didn't find out of a box solution, so decided to go with the AOP approach.

I found HealthEndpointWebExtension class that manages health rest endpoints.
This class has two health methods, one one of them called for common /health endpoint and second when we call health/<group name>.

So I created Aspect around the second method, which allowed me to add cache for endpoints I need.

@Around("execution(@org.springframework.boot.actuate.endpoint.annotation.ReadOperation * org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(..,java.lang.String...))) && args(.., path)")
public Object cache(ProceedingJoinPoint joinPoint, String... path) throws Throwable {
    // Logic to get cache per endpoint and process if cache specifed.
}

At the end application.properties looks like this:

// out of the box
management.endpoint.health.cache.time-to-live=120s 

// customly created properties
management.endpoint.health.group.readiness.cache.time-to-live=60s
management.endpoint.health.group.deep.cache.time-to-live=120s
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文