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

Spring Boot如何配置Liveness和Readiness探针

什么是存活探针和就绪探针

当我们采用Kubernetes作为编排平台时,每个Pod节点上的kubelet都负责维持该节点内Pod的健康状态。

例如,某些应用程序在启动后可能需要短暂准备时间才能开始接收请求。kubelet可以确保只有当应用准备就绪时才会向其转发流量。此外,如果Pod的主进程因任何意外原因崩溃,kubelet将自动重启容器。

为履行这些职责,Kubernetes提供了两种健康检查机制:存活探针(liveness probes)和就绪探针(readiness probes)。kubelet 将使用就绪探针来判断应用何时准备好接收请求。更具体地说,当 Pod 的所有容器都就绪时,该 Pod 才会被视为就绪状态。类似地,kubelet 可以通过存活探针检查 Pod 是否仍在运行。基本上,存活探针会帮助 kubelet 判断何时需要重启容器。

SpringBoot中配置存活探针和就绪探针

自Spring Boot 2.3起,LivenessStateHealthIndicator与ReadinessStateHealthIndicator类将对外暴露应用程序的存活状态和就绪状态。当我们将应用部署到Kubernetes环境时,Spring Boot会自动注册这些健康指标。

如此一来,我们便可分别使用/actuator/health/liveness和/actuator/health/readiness端点作为存活探针和就绪探针的检测接口。

在Spring Boot 2.3.2和以上:

  • 首先添加actuator的依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 然后添加相应的配置:
management:endpoints:web:exposure:include: healthendpoint:health:probes:enabled: trueshow-details: alwayshealth:# /actuator/health/livenesslivenessState:enabled: true# /actuator/health/readinessreadinessState:enabled: true

浏览器访问:

// http://localhost:8080/actuator/health
{"status": "UP","components": {"diskSpace": {"status": "UP","details": {"total": 107369984000,"free": 59385589760,"threshold": 10485760,"path": "C:\\workspace\\demo\\.","exists": true}},"livenessState": {"status": "UP"},"ping": {"status": "UP"},"readinessState": {"status": "UP"}},"groups": ["liveness","readiness"]
}
//http://localhost:8080/actuator/health/liveness
{"status":"UP"}
//http://localhost:8080/actuator/health/readiness
{"status":"UP"}

Kubernetes中配置存活探针和就绪探针

标准的 Kubernetes YAML 写法:

containers:
- name: my-app...livenessProbe:httpGet:path: /actuator/health/liveness # 注意这里的写法port: 8080timeoutSeconds: 1periodSeconds: 10readinessProbe:httpGet:path: /actuator/health/readinessport: 8080timeoutSeconds: 1periodSeconds: 10

参考:https://www.baeldung.com/spring-liveness-readiness-probes

http://www.dtcms.com/a/341041.html

相关文章:

  • 【Android】Activity 如何进行数据传输
  • java17学习笔记-switch总结
  • 使用 GraalVM Native Image 将 Spring Boot 应用编译为跨平台原生镜像:完整指南
  • linux 内核 - 内存管理单元(MMU)与地址翻译(一)
  • yolo_RK3588系列(三)
  • mac电脑软件左上角的关闭/最小化/最大化按钮菜单的宽度和高度是多少像素
  • ijkplayer Android 编译
  • strncpy 函数使用及其模拟实现
  • AI重塑软件测试:质量保障的下一站
  • 成本管控:餐饮利润的隐形守护者
  • Zemax光学设计输出3D
  • 4位量化:常规的线性层被替换成了4位线性层(48)
  • 酶 EC number 预测工具CLEAN的安装和使用
  • QT官方库头文件找不到(添加模块方法)
  • C++模板元编程:从SFINAE到Concepts的进化史
  • mac 搭建docker-compose,部署docker应用
  • AI on Mac, Your Way!全本地化智能代理,隐私与性能兼得
  • pcl求平面点云的边界凸包点
  • Frida Hook Android Activity生命周期全方法监控方案
  • 哈希:字母异位词分组
  • RHCA07-Linux跟踪工具及CPU调优
  • Linux I/O 多路复用实战:深入剖析 Select 与 Poll
  • capsh 命令详解
  • 【机器学习深度学习】Ollama、vLLM、LMDeploy对比:选择适合你的 LLM 推理框架
  • Objective-C 版本的 LiveEventBus 效果
  • vue+openlayers示例:适配arcgis矢量瓦片服务以及样式(附源码下载)
  • 英伟达Blackwell架构下的中国特供版AI芯片:B30A与RTX 6000D,是技术妥协还是市场新策略?
  • 基于单片机太阳能充电器/太阳能转换电能
  • C端高并发项目都有哪些
  • Angular由一个bug说起之十八:伴随框架升级而升级ESLint遇到的问题与思考