You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SpringBoot 1.X启用Readiness/Liveness探针方案及版本支持咨询

SpringBoot 1.X 启用Liveness/Readiness探针方案

一、自定义实现拆分端点

SpringBoot 1.X的Actuator并未原生拆分健康检查为存活(Liveness)和就绪(Readiness)探针,需要通过自定义扩展实现:

1. 实现自定义HealthIndicator

分别编写两个指示器类,各自实现对应的检查逻辑:

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

// 存活探针:检查应用核心组件是否正常运行
@Component
public class LivenessHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // 示例逻辑:核心服务未崩溃则返回UP
        return Health.up().withDetail("component", "core-service").withDetail("status", "alive").build();
    }
}

// 就绪探针:检查应用是否已完成初始化、依赖是否就绪
@Component
public class ReadinessHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // 示例逻辑:数据库连接、外部依赖就绪则返回UP
        return Health.up().withDetail("dependencies", "ready").withDetail("status", "accept-traffic").build();
    }
}

2. 配置并暴露端点

在application.properties中开启健康端点并配置路径:

management.context-path=/actuator
endpoints.health.enabled=true
endpoints.health.sensitive=false

3. 拆分独立访问路径

通过自定义Controller将两个指示器映射到指定路径:

import org.springframework.boot.actuate.health.Health;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/actuator/health")
public class HealthProbeController {
    @Resource
    private LivenessHealthIndicator livenessIndicator;
    @Resource
    private ReadinessHealthIndicator readinessIndicator;

    @GetMapping("/liveness")
    public Health getLivenessStatus() {
        return livenessIndicator.health();
    }

    @GetMapping("/readiness")
    public Health getReadinessStatus() {
        return readinessIndicator.health();
    }
}

完成后即可通过/actuator/health/liveness和/actuator/health/readiness访问两个探针。

二、后续SpringBoot版本的原生支持

从SpringBoot 2.3版本开始,官方原生内置了Liveness和Readiness探针支持,无需自行实现:

  • 直接启用后可通过/actuator/health/liveness和/actuator/health/readiness访问
  • 核心配置示例:
management.endpoint.health.probes.enabled=true
management.health.livenessstate.enabled=true
management.health.readinessstate.enabled=true

同时官方已完成与Kubernetes探针的适配,可直接在K8s配置中引用这两个端点实现无缝部署。


内容的提问来源于stack exchange,提问作者Yore

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 06:22:12