赞
踩
开发依赖 | 版本 |
---|---|
Spring Boot | 3.0.6 |
JDK | 20 |
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
因为/health
进行严格检查SpringBoot
各项组件服务,比如邮件服务、数据库服务、MQ服务等,当发现有一个组件处于非正常状态,其返回的内容会由{"status": "up"}
变为{"status": "down"}
,从而导致Liveness
探针失效,而有些情况下,还抛出异常,在特定情况下某些服务不正常属于正常现象,例如:邮件服务。
我们在一次邮件服务迁移的过程中,使用
Liveness
探针频繁访问/health
,触发了springboot
连续抛出堆栈信息导致服务直接宕机,非常恐怖,如果没有做到宕机快照,会导致查问题无从下手
management:
endpoint:
health:
probes:
enabled: true
health:
livenessstate:
enabled: true
readinessstate:
enabled: true
- 从 Spring Boot 2.3 开始,LivenessStateHealthIndicator 和 ReadinessStateHealthIndicator 类将公开应用程序的活跃度和就绪状态。 当将应用程序部署到 Kubernetes 时,Spring Boot 会自动注册这些健康指标。
- 因此,可以分别使用 /actuator/health/liveness 和 /actuator/health/readiness 端点作为liveness 和 readiness 探针。
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 18080
initialDelaySeconds: 5
failureThreshold: 2
periodSeconds: 60
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 18080
initialDelaySeconds: 5
periodSeconds: 60
startupProbe:
httpGet:
path: /actuator/health/readiness
port: 18080
failureThreshold: 30
periodSeconds: 10
Spring Boot
使用两个枚举来封装不同的就绪和活跃状态。 对于就绪状态,有一个名为 ReadinessState
的枚举,具有以下值:
ACCEPTING_TRAFFIC
状态表示应用程序已准备好接受流量REFUSING_TRAFFIC
状态意味着应用程序还不愿意接受任何请求同样,LivenessState
枚举使用两个值表示应用程序的活跃状态:
CORRECT
值表示应用程序正在运行并且其内部状态是正确的BROKEN
值意味着应用程序运行时出现了一些致命故障以下是 Spring
中应用程序生命周期事件方面的就绪和活跃状态如何变化:
bean
定义CORRECT
ACCEPTING_TRAFFIC
Spring
本身)就可以通过发布适当的 AvailabilityChangeEvents
来更改这些状态。由于预热可以看做实例能否正常提供服务的健康指标,所以我采用了rediness探针,实例代码如下:
public class SeaReadinessHealthIndicator extends AvailabilityStateHealthIndicator {
private Integer isChecking = 0;
private StringBuffer notCompleteExecuteClassBuffer = new StringBuffer();
@Override
protected void doHealthCheck(Health.Builder builder) {
switch (isChecking.get()) {
case 1:
builder.down().withDetail("message", "instance is starting.").build();
return;
case 2:
builder.outOfService().withDetail("message", String.format("some service start error. they are: %s", notCompleteExecuteClassBuffer.toString())).build();
return;
case 200:
builder.up().build();
return;
}
}
@Override
protected AvailabilityState getState(ApplicationAvailability applicationAvailability) {
return applicationAvailability.getReadinessState();
}
}
- 这么设置后,访问
/actuator/health/seaReadiness
,发现无法访问,再检查/actuator/health
目录,发现有一个cn.xxx.seaReadiness
的状态是{"status":"UP"}
,原来actuator health
的规则是SeaReadinessHealthIndicator
,HealthIndicator
之前的默认为名称。如果是加入扫描的方式就是这样的,但我现在是用starter的方式进行发布的。- 如果我要实现
/actuator/health/seaReadiness
访问怎么做呢?- 在
starter
扫描的类名中,加上以下别名即可:
@Component("seaReadiness")
public class SeaReadinessHealthIndicator extends AvailabilityStateHealthIndicator {
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。