Spring 通过示例启动 HealthIndicator
Spring Boot HealthIndicator by Example
我希望在我的 Spring Boot REST 服务上构建一个强大而详细的健康检查端点 (/health
)。我刚刚阅读了有关该主题的 this excellent Baeldung article,但仍有一些顾虑。
理想情况下,我的 /health
端点能够考虑到我所有子系统(超过 10 个子系统)的个体健康状况,包括主机的健康状况(CPU 、磁盘利用率、可用内存等)。
我无法判断 Spring Boot 是否希望您构建一个且唯一的 HealthIndicator
impl。 或如果想法是为每个主要子系统构建一个HealthIndicator
实现(每个子系统都可以独立地“up”或“ 向下”分别。
此外,在那个 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
是从哪里来的?!
1) 您可以拥有任意多的健康指标。只需创建将扩展 org.springframework.boot.actuate.health.HealthIndicator
的 bean,它们将被执行器的健康检查端点自动拾取。您的 bean 的名称将是某些健康指标的名称。在您的示例中,myHealthCheck
和 diskSpace
是 spring 上下文中的 bean,它们在您点击 /health
时被调用。磁盘空间是来自 org.springframework.boot.actuate.health.DiskSpaceHealthIndicator
的 spring 引导中预定义的健康指标之一。
2) 顶级状态是您所有健康指标的累积状态。您可以配置它的工作方式,但默认情况下它会显示最低状态(您的健康指标之一处于 DOWN 状态,因此累积状态显示为 DOWN)
您可以实施多个 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
之前的 class 名称(驼峰式)(例如 ExampleOneHealthIndicator
--> exampleOne
)
其他名称(例如 diskSpace
或 ping
)来自内置的健康检查,class 名称的命名与您预期的一样(DiskSpaceHealthIndicator
& PingHealthIndicator
)
请注意,顶级 status
是 DOWN
。这是由StatusAggregator
决定的。默认的是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
我希望在我的 Spring Boot REST 服务上构建一个强大而详细的健康检查端点 (/health
)。我刚刚阅读了有关该主题的 this excellent Baeldung article,但仍有一些顾虑。
理想情况下,我的 /health
端点能够考虑到我所有子系统(超过 10 个子系统)的个体健康状况,包括主机的健康状况(CPU 、磁盘利用率、可用内存等)。
我无法判断 Spring Boot 是否希望您构建一个且唯一的 HealthIndicator
impl。 或如果想法是为每个主要子系统构建一个HealthIndicator
实现(每个子系统都可以独立地“up”或“ 向下”分别。
此外,在那个 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
是从哪里来的?!
1) 您可以拥有任意多的健康指标。只需创建将扩展 org.springframework.boot.actuate.health.HealthIndicator
的 bean,它们将被执行器的健康检查端点自动拾取。您的 bean 的名称将是某些健康指标的名称。在您的示例中,myHealthCheck
和 diskSpace
是 spring 上下文中的 bean,它们在您点击 /health
时被调用。磁盘空间是来自 org.springframework.boot.actuate.health.DiskSpaceHealthIndicator
的 spring 引导中预定义的健康指标之一。
2) 顶级状态是您所有健康指标的累积状态。您可以配置它的工作方式,但默认情况下它会显示最低状态(您的健康指标之一处于 DOWN 状态,因此累积状态显示为 DOWN)
您可以实施多个 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
之前的 class 名称(驼峰式)(例如 ExampleOneHealthIndicator
--> exampleOne
)
其他名称(例如 diskSpace
或 ping
)来自内置的健康检查,class 名称的命名与您预期的一样(DiskSpaceHealthIndicator
& PingHealthIndicator
)
请注意,顶级 status
是 DOWN
。这是由StatusAggregator
决定的。默认的是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