Spring Boot Liveness 和 Readness 接口使用

在 Spring Boot 2.3+ 中,提供了单独的 liveness 和 readness,用于为 Kubernetes 提供相应检查接口

  • liveness
    用于检查应用是否存活,当应用组件因故障不健康时,可以通过这个接口的结果,配置相应策略,重启应用或重新调度 Pod
  • readness
    用于检查应用是否就绪,是否可以提供服务,如当流量太大超过应用的承载范围时,可以将这个接口的状态改为不健康,这样可以停止接收流量,当处理完后再次检查时变为健康,继续处理请求

配置

  • build.gradle 添加依赖
plugins {id 'org.springframework.boot' version '2.3.2.RELEASE'id 'io.spring.dependency-management' version '1.0.9.RELEASE'id 'java'
}
dependencies {implementation 'org.springframework.boot:spring-boot-starter-actuator'implementation 'org.springframework.boot:spring-boot-starter-web'
}
  • application.properties 添加配置

从 2.3.2 开始,/actuator/health 接口添加了分组的概念,默认分为 liveness 和 readness 两个组,需要显式指定后才可以使用,否则会返回 404;/actuator/health 接口包含所有的指标

当前应用支持的指标,可以设置 management.endpoint.health.show-details=always后从 /actuator/health 接口获取

先指定 readness 指标,之后可以将剩余的所有指标设置为 liveness 的指标

management.endpoint.health.show-details=always
management.endpoint.health.group.readiness.include=ping
management.endpoint.health.group.liveness.include=*
management.endpoint.health.group.liveness.exclude=${management.endpoint.health.group.readiness.include}

测试

  • 启动应用

请求相应的接口

  • health
curl http://localhost:8080/actuator/health
{"status": "UP","components": {"diskSpace": {"status": "UP","details": {"total": 499963174912,"free": 380364800000,"threshold": 10485760,"exists": true}},"livenessStateProbeIndicator": {"status": "UP"},"ping": {"status": "UP"},"readinessStateProbeIndicator": {"status": "UP"}},"groups": ["liveness","readiness"]
}
  • readiness
curl http://localhost:8080/actuator/health/readiness
{"components": {"ping": {"status": "UP"}},"status": "UP"
}
  • liveness
curl http://localhost:8080/actuator/health/liveness
{"components": {"custom": {"details": {"Status": "Health"},"status": "UP"},"diskSpace": {"details": {"exists": true,"free": 380286464000,"threshold": 10485760,"total": 499963174912},"status": "UP"}},"status": "UP"
}

自定义检查项

和自定义健康检查项一样,添加一个新的 HealthIndicator,添加到相应的分组即可

  • CustomHealthIndicator.java
@Component
public class CustomHealthIndicator implements HealthIndicator {private boolean health = true;@Overridepublic Health health() {if (health) {return Health.up().withDetail("Status", "Health").build();}return Health.down().withDetail("Status", "Not Health").build();}public void setHealth(boolean health) {this.health = health;}
}
  • applicaiton.properties
management.endpoint.health.group.readiness.include=ping,custom

测试

  • readiness
curl http://localhost:8080/actuator/health/readiness
{"components": {"custom": {"details": {"Status": "Health"},"status": "UP"},"ping": {"status": "UP"}},"status": "UP"
}

Spring-Boot Liveness 和 Readness 接口使用相关推荐

  1. STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)

    STS创建Spring Boot项目实战(Rest接口.数据库.用户认证.分布式Token JWT.Redis操作.日志和统一异常处理) 1.项目创建 1.新建工程 2.选择打包方式,这边可以选择为打 ...

  2. Spring Boot中使用Convert接口实现类型转换器

    在Spring3中引入了一个Converter接口,它支持从一个Object转为另一个Object.除了Converter接口之外,实现ConverterFactory接口和GenericConver ...

  3. boot spring 接口接收数据_基于 Spring Boot 实现 Restful 风格接口,实现增删改查功能...

    优质文章,及时送达 Spring Boot介绍 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配 ...

  4. Spring Boot设置访问url接口后缀

    传统的xml配置方式 <!--Spring MVC 配置--> <servlet><servlet-name>dispatcherServlet</servl ...

  5. 通过Spring boot编写数据查询接口-----练习题

    准备:在mysql中准备两张表 ,student表存学生信息的,score表存学生的各科成绩 studnet表 score表 例题: 一.通过班级名称查询班级人数(要求加入缓存,这里缓存用的数据库是r ...

  6. Spring Boot 项目的 API 接口防刷

    说明:使用了注解的方式进行对接口防刷的功能,非常高大上,本文章仅供参考 一,技术要点:springboot的基本知识,redis基本操作, 首先是写一个注解类: import java.lang.an ...

  7. Spring Boot 使用 Swagger3 生成 API 接口文档

    前言 在之前的文章中,我们已经讲了如何利用 Spring Boot 来集成 Swagger2,详情可戳:Spring Boot 集成 Swagger2,构建强大的 API 文档.但其实 Swagger ...

  8. Spring Boot 缓存应用实践

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源:cnblogs.com/jeffwongishan ...

  9. Spring Boot 2.x基础教程:使用JdbcTemplate访问MySQL数据库

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 翟永超 来源 | didispace.com/ ...

最新文章

  1. android8.1获取蓝牙地址,[蓝牙] Android 8.1 获取蓝牙设备地址无效;02:00:00:00:00:00
  2. python 标签树的遍历
  3. Spring基础知识及入门
  4. java base64 编码 类_java base64编码和解码的三种方式 | 学步园
  5. 研发和人力资源发展模式对比研究
  6. Redis 初次尝试
  7. mysql 检查_检查MySQL的健康状况
  8. 工作流实战_17_flowable 流程实例撤回
  9. 深度学习笔记(48) 内容代价函数
  10. 20155235 信息安全技术概论 第二次实验报告
  11. The Future of Compass ElasticSearch
  12. Notepad++ 的使用(插件)
  13. 鸿蒙os到底是什么,聊聊鸿蒙OS到底是什么!
  14. paip.提升效率----更改数组LIST对象值for与FOREACH
  15. 双击jar包 运行SpringBoot项目
  16. Chromium OS?本土化气息的的Flint OS
  17. 【NOI2017模拟3.30】原谅
  18. hdu - 1435 Stable Match 稳定婚姻问题、Gale-Shapley算法模板
  19. android实现拍照、相册选图、裁剪功能,兼容7.0以及小米
  20. 【零基础-3】PaddlePaddle学习Bert

热门文章

  1. spring boot mybatis 日志打印配置
  2. 单片机反相器_TTL反相器的基本电路
  3. 【闪亮的玻璃图标悬浮效果】
  4. VB中ByVal和ByRef
  5. Cloud IDEs For Web Developers – Best Of
  6. 医疗卫生行业如何做好数字化转型?
  7. sk_buff属性详解
  8. 抖音短连接v.douyin.com/xxx 怎么生成?
  9. 国内CDN加速哪个好?
  10. UI设计师为什么要学习字体设计?