如何扩展现有的执行器健康检查端点?



存在现有的执行器运行状况终结点,例如:

/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 接口。在此示例中,自定义的运行状况服务返回要添加到运行状况终结点的所需值的映射。

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

尽管这不仅限于运行状况检查操作。

最新更新