当前位置: 首页 > news >正文

解决Spring Boot应用在Kubernetes上健康检查接口返回OUT_OF_SERVICE的问题

现象

在将Spring Boot应用部署到Kubernetes上时,健康检查接口/healthcheck返回的状态为{"status":"OUT_OF_SERVICE","groups":["liveness","readiness"]},而期望的是返回正常的健康状态。值得注意的是,我司统一的规范是自己实现的/healthcheck接口,并且三种探针的HTTP检查也都是/healthcheck同一路径。

问题原因

根据返回结果判断是Spring Boot自带的健康检查机制actutor,估计是依赖升级导致自动启用了actutor机制,并且/actuator/health重定向到了/healthcheck接口。由文档得知,从 Spring Boot 2.3 开始,LivenessStateHealthIndicator 和RereadynessStateHealthIndicator类将公开应用程序的活动性和就绪状态。当我们将应用程序部署到 Kubernetes 时,Spring Boot 将自动注册这些健康指标。而本次的问题是一次dubbo客户端升级导致的,目前不清楚是否是dubbo升级导致了其他依赖的版本更新。

解决方法


为了解决这个问题,我们可以采取以下步骤:

https://springdoc.cn/spring-boot/actuator.html#actuator.endpoints.health.writing-custom-health-indicators

该链接展示了Spring Boot Actutor在几种健康状态下返回的HTTP状态代码,如下图:

 

1.创建一个自定义的HealthEndpoint来处理健康检查请求,并将readiness或liveness的状态映射为UP/UNKNOWN状态。

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;@Component
@Endpoint(id = "health")
public class CustomHealthEndpoint {private final HealthEndpoint healthEndpoint;public CustomHealthEndpoint(HealthEndpoint healthEndpoint) {this.healthEndpoint = healthEndpoint;}@ReadOperationpublic Health health() {Health health = healthEndpoint.health();Status status = health.getStatus();// 如果状态是readiness或liveness,则设置为UNKNOWN,否则返回原始健康状态if (status.getCode().equals("readiness") || status.getCode().equals("liveness")) {return Health.unknown().withDetails(health.getDetails()).build();} else {return health;}}
}

2.将out_of_service返回的状态码映射成200。

application.properties:

management.endpoint.health.status.http-mapping.out-of-service=200

application.yml:

management:endpoint:health:status:http-mapping.out-of-service:200

通过上述配置,当应用程序的健康状态被判断为"out-of-service"时,Actuator将使用HTTP响应码200来表示该状态。这意味着当使用Actuator的健康检查端点时,如果应用程序的健康状态为"out-of-service",将返回HTTP响应码200。

总结


通过自定义HealthEndpoint和配置探针的HTTP路径,我们成功解决了Spring Boot应用在Kubernetes上健康检查接口返回OUT_OF_SERVICE的问题。现在,健康检查接口返回正确的健康状态,并且探针路径也与公司的重定向配置保持一致。这样,我们可以确保应用在Kubernetes环境中的健康检查正常运行,同时满足公司的需求。

http://www.lryc.cn/news/241958.html

相关文章:

  • Java对象逃逸
  • Greenplum的数据库年龄检查处理
  • [HCIE] IPSec-VPN (IKE自动模式)
  • Qt/QML编程学习之心得:一个Qt工程的学习笔记(九)
  • c++ 课程笔记
  • ELK企业级日志分析平台——ES集群监控
  • Twincat使用:EtherCAT通信扫描硬件设备链接PLC变量
  • 手机APP-MCP走蓝牙无线遥控智能安全帽~执法记录仪~拍照录像,并可做基础的配置,例如修改服务器IP以及配置WiFi等
  • 网络互联与IP地址
  • Android设计模式--模板方法模式
  • 大语言模型——BERT和GPT的那些事儿
  • Docker 命令详解
  • ios打包,证书获取
  • linux(nginx安装配置,tomcat服务命令操作)
  • jQuery_03 dom对象和jQuery对象的互相转换
  • Mysql 中如何导入数据?
  • 深入了解前馈网络、CNN、RNN 和 Hugging Face 的 Transformer 技术!
  • Flink Table API 读写MySQL
  • Nginx 开源版安装
  • 『亚马逊云科技产品测评』活动征文|低成本搭建物联网服务器thingsboard
  • 【Pytorch】Visualization of Feature Maps(3)
  • 人工智能对我们的生活影响
  • Mysql存储引擎分类
  • 基于Python+TensorFlow+Django的交通标志识别系统
  • 【Java 进阶篇】Jedis:让Java与Redis轻松对话的利器
  • 【数据分享】我国12.5米分辨率的DEM地形数据(免费获取/地理坐标系)
  • C++设计模式之策略模式
  • spring-webflux的一些概念的理解
  • OpenCV快速入门:特征点检测与匹配
  • 旋转的数组