我希望在Spring Boot REST服务上构建一个健壮且详细的运行状况检查端点(/health
)。我刚刚阅读了有关该主题的this excellent Baeldung article,但仍有一些问题。
理想情况下,我的/health
端点可以考虑所有子系统(其中10个)的个人健康状况,包括主机的健康状况(CPU,磁盘利用率,可用内存等) )。
我无法判断Spring Boot是否希望您构建一个且只有一个HealthIndicator
impl。 或如果想要为每个主要子系统构建一个HealthIndicator
impl(每个子系统可以独立地" up "或& #34; down "
此外,在Baeldung示例中,顶级status
和myHealthCheck.status
之间有什么区别?它们各自来自哪里(在代码中)?
{
"status" : "DOWN",
"myHealthCheck" : {
"status" : "DOWN",
"Error Code" : 1,
"Description" : "You custom MyHealthCheck endpoint is down"
},
"diskSpace" : {
"status" : "UP",
"free" : 209047318528,
"threshold" : 10485760
}
}
diskSpace
来自哪里?!
答案 0 :(得分:2)
1)您可以拥有任意数量的健康指标。只需创建将扩展org.springframework.boot.actuate.health.HealthIndicator
的bean,它们将被执行者的healthcheck端点自动拾取。 bean的名称将是某个运行状况指示器的名称。在您的示例中,myHealthCheck
和diskSpace
是弹出上下文中的bean,当您点击/health
时会调用它们。 diskSpace是来自org.springframework.boot.actuate.health.DiskSpaceHealthIndicator
的春季启动中预定义的健康指标之一。
2)最高级别状态是所有健康指标的累积状态。您可以配置它的工作方式,但默认情况下它会显示最低状态(您有一个健康指示器处于DOWN状态,因此累积状态显示为DOWN)
答案 1 :(得分:0)
您可以实施多个HealthIndicator
,它们都将为最终总体价值做出贡献 UP 或 DOWN 。最终值是所有其他值的总和(由StatusAggregator
确定)。
示例1(健康)
@Component
public class ExampleOneHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// do something to check health
// inspect the return
// healthy
return Health.up().build();
}
}
示例2(不健康)
@Component
public class ExampleTwoHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// do something to check health
// inspect the return
// unhealthy
return Health.down().build();
}
}
示例3(详细信息不健康)
@Component
public class ExampleThreeHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// do something to check health
// inspect the return
// unhealthy with details
return Health.down()
.withDetail("reason", "details about why it failed")
.build();
}
}
使用以上三个示例并将management.endpoint.health.show-details
设置为always
(在application.properties
中),调用/actuator/health
时将得到以下结果。
{
"status": "DOWN",
"components": {
"diskSpace": {
"status": "UP",
"details": {
"total": 1000240963584,
"free": 880857858048,
"threshold": 10485760
}
},
"exampleOne": {
"status": "UP"
},
"exampleThree": {
"status": "DOWN",
"details": {
"reason": "details about why it failed"
}
},
"exampleTwo": {
"status": "DOWN"
},
"ping": {
"status": "UP"
}
}
}
响应中的名称是HealthIndicator
之前的类名称(用驼峰大小写)(例如ExampleOneHealthIndicator
-> exampleOne
)
其他名称(例如diskSpace
或ping
)来自内置的运行状况检查,并且类名的命名与您期望的相同(DiskSpaceHealthIndicator
和{{1 }})
请注意,顶级PingHealthIndicator
是status
。这由DOWN
确定。默认值为https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/actuate/health/SimpleStatusAggregator.html
示例代码
https://github.com/dustinschultz/spring-boot-actuator-custom-health-checks