endpoint - 如何扩展现有的执行器健康检查端点?
问题描述
现有的执行器健康端点,例如:
/actuator/health
我将如何扩展这个现有的端点说:
/actuator/health/myendpoint
为了进行一些健康检查?
当前代码:
package com.example.actuatordemo.health;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Component;
@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
// Use the builder to build the health status details that should be reported.
// If you throw an exception, the status will be DOWN with the exception message.
builder.up()
.withDetail("app", "Testing endpoint extension!")
.withDetail("error", "Oops.");
}
}
解决方案
为了扩展 /health 端点,您必须像这样实现 HealthIndicator 接口。在此示例中,自定义 HealthService 返回您要添加到健康端点的所需值的映射。
import com.metavera.tako.fc.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.*;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class HealthCheck implements HealthIndicator {
@Autowired
HealthService healthService;
@Override
public Health health() {
return myCustomHealth();
}
private Health myCustomHealth() {
Health.Builder builder = new Health.Builder(Status.UP);
Map<String, Object> response = healthService.getHealthStatus();
for (Map.Entry<String, Object> entry : response.entrySet()) {
String key = entry.getKey();
Object value = response.get(key);
builder.withDetail(key, value);
}
return builder.build();
}
}
虽然上述解决方案允许您修改现有的 /health 端点,但在第 53.7 节下还有关于如何在此处创建自定义端点的附加文档。
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html
虽然这不仅限于健康检查操作。
推荐阅读
- c# - 是否有用于浏览重载方法的 Visual Studio 功能?
- kotlin - 找不到流量的合同附件
- azure-devops - 如何将工件标记为“由 QA 验证”?
- sql - 我可以永久更改 SQL Server 表的日期格式吗?
- swisscomdev - 如何将应用程序从一个空间移动到另一个空间?
- javascript - 垂直求和数组(矩阵)的数组
- react-native - React Native Development Server 构建成功后自动关闭
- ms-access - 合并表以获取行的排列 - 访问
- keras - ValueError:图形在 vgg16 中断开连接
- php - 数组数据一直被不相关的代码修改