Spring 执行器 CompositeHealthIndicator - 如何自动 use/display /health 上的所有指标
Spring actuator CompositeHealthIndicator - how to automatically use/display all indicators on /health
Spring 都是自动自动魔术,我对自己如何观察 HealthIndicator 的功能感到有点惊讶。
我预计,当我在上下文中有一个 HealthIndicator
bean 时,它会将它与它拥有的其他 bean 一起聚合,并在 /actuator/health
中生成一个摘要。相反,它会忽略所有 但 我的自定义项。我最终实现了自己的端点聚合它们。
我一定是漏掉了什么。要么我没有正确启用此功能(并且我承认这令人困惑),要么我期望的行为不是它实际应该做的。
我的问题:我的预期是否正确?如果是这样,我该如何让它自动聚合?如果我的期望不正确,执行器按照我的意愿工作的惯用方式是什么?
这是我的示例代码:
@Component
public class HelloWorldHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return Health.up().withDetail("hello","world").build();
}
}
/actuator/health
{
"status": "UP"
}
它也没有像我预期的那样打印 "hello" : "world" 但那是另一回事了。
这是我的自定义控制器和输出:
@RestController
public class CustomController {
private final Map<String,HealthIndicator> indicators;
@Autowired
public HealthController(Map<String, HealthIndicator> indicators) {
this.indicators = indicators;
}
@GetMapping("/health")
public Health getHealth(@RequestParam("deep") Boolean deep){
if(deep != null && deep)
return new CompositeHealthIndicator(new OrderedHealthAggregator(),indicators).health();
return Health.up().build();
}
}
/health?deep=true
{
"status": "UP",
"details": {
"helloWorldHealthIndicator": {
"status": "UP",
"details": {
"hello": "world"
}
},
"diskSpaceHealthIndicator": {
"status": "UP",
"details": {
"total": 74865782784,
"free": 65754009600,
"threshold": 10485760
}
},
"dbHealthIndicator": {
"status": "UP",
"details": {
"database": "MySQL",
"hello": 1
}
}
}
}
是什么让您认为您的自定义 HealthIndicator
没有被调用?您是否尝试过 运行 带有调试器的应用程序来检查它是否被调用?
该输出没有告诉我们任何信息:您可能只是获得所有健康指标的汇总视图(根本没有详细信息)
Health information is collected from all HealthIndicator beans defined in your ApplicationContext. Spring Boot includes a number of auto-configured HealthIndicators and you can also write your own.
上面的代码应该可以正常工作。我最好的猜测是没有显示详细信息(查看文档以启用它,请注意它们是 Spring Boot 1 和 Spring Boot 2 之间的差异)。
编辑:为了证明这一点,我创建了 a sample project that uses the exact same code as yours 并在调用 health
时得到了这个(禁用安全性以便向匿名用户显示详细信息):
{
"status": "UP",
"helloWorld": {
"status": "UP",
"hello": "world"
},
"diskSpace": {
"status": "UP",
"total": 499963170816,
"free": 217149034496,
"threshold": 10485760
}
}
Spring 都是自动自动魔术,我对自己如何观察 HealthIndicator 的功能感到有点惊讶。
我预计,当我在上下文中有一个 HealthIndicator
bean 时,它会将它与它拥有的其他 bean 一起聚合,并在 /actuator/health
中生成一个摘要。相反,它会忽略所有 但 我的自定义项。我最终实现了自己的端点聚合它们。
我一定是漏掉了什么。要么我没有正确启用此功能(并且我承认这令人困惑),要么我期望的行为不是它实际应该做的。
我的问题:我的预期是否正确?如果是这样,我该如何让它自动聚合?如果我的期望不正确,执行器按照我的意愿工作的惯用方式是什么?
这是我的示例代码:
@Component
public class HelloWorldHealthIndicator implements HealthIndicator {
@Override
public Health health() {
return Health.up().withDetail("hello","world").build();
}
}
/actuator/health
{
"status": "UP"
}
它也没有像我预期的那样打印 "hello" : "world" 但那是另一回事了。
这是我的自定义控制器和输出:
@RestController
public class CustomController {
private final Map<String,HealthIndicator> indicators;
@Autowired
public HealthController(Map<String, HealthIndicator> indicators) {
this.indicators = indicators;
}
@GetMapping("/health")
public Health getHealth(@RequestParam("deep") Boolean deep){
if(deep != null && deep)
return new CompositeHealthIndicator(new OrderedHealthAggregator(),indicators).health();
return Health.up().build();
}
}
/health?deep=true
{
"status": "UP",
"details": {
"helloWorldHealthIndicator": {
"status": "UP",
"details": {
"hello": "world"
}
},
"diskSpaceHealthIndicator": {
"status": "UP",
"details": {
"total": 74865782784,
"free": 65754009600,
"threshold": 10485760
}
},
"dbHealthIndicator": {
"status": "UP",
"details": {
"database": "MySQL",
"hello": 1
}
}
}
}
是什么让您认为您的自定义 HealthIndicator
没有被调用?您是否尝试过 运行 带有调试器的应用程序来检查它是否被调用?
该输出没有告诉我们任何信息:您可能只是获得所有健康指标的汇总视图(根本没有详细信息)
Health information is collected from all HealthIndicator beans defined in your ApplicationContext. Spring Boot includes a number of auto-configured HealthIndicators and you can also write your own.
上面的代码应该可以正常工作。我最好的猜测是没有显示详细信息(查看文档以启用它,请注意它们是 Spring Boot 1 和 Spring Boot 2 之间的差异)。
编辑:为了证明这一点,我创建了 a sample project that uses the exact same code as yours 并在调用 health
时得到了这个(禁用安全性以便向匿名用户显示详细信息):
{
"status": "UP",
"helloWorld": {
"status": "UP",
"hello": "world"
},
"diskSpace": {
"status": "UP",
"total": 499963170816,
"free": 217149034496,
"threshold": 10485760
}
}