【问题标题】:Spring Boot Health EndpointSpring Boot 健康端点
【发布时间】:2021-04-15 17:01:40
【问题描述】:
我正在寻找一种方法来更改 spring boot 的健康检查的标题(添加一些东西)。我搜索了这个主题,但只找到了关于自定义健康功能等的问题(例如How to add a custom health check in spring boot health?)。
这不是我想要的。
我想使用标准/默认的健康功能(所以我不想更改响应的正文(“状态”:“UP”),也不想实现自己的健康功能或自定义默认值一。
我的目标是更改响应的标头以实现两件事:
- 在标题中添加一些内容(例如最大年龄)
- reaching cors(例如,我想将 allow-origin 设置为 *,allowed-method 设置为 GET 等等),我知道在管理属性中有一种方法可以做到这一点,但这适用于所有端点,我只想要这些 tgings 来申请健康端点
有什么方法可以使用默认的健康检查,只需修改标头和允许的来源等属性,还是我必须创建一个新的控制器?
感谢您的帮助。
【问题讨论】:
标签:
spring
header
endpoint
health-check
【解决方案1】:
只需使用获得足够指标的方法实现您自己的服务,然后当然是您的自定义控制器引用它。我不认为你可以修改那些默认端点..
以下是此自定义服务的示例:
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.MetricsEndpoint;
import org.springframework.stereotype.Component;
@Component
public class SpringActuator {
@Autowired
MetricsEndpoint metrics;
public Double getCPU() {
return metrics.metric("process.cpu.usage", Arrays.asList()).getMeasurements().get(0).getValue();
}
public Double getRAM() {
return metrics.metric("jvm.memory.used", Arrays.asList()).getMeasurements().get(0).getValue();
}
}