Spring Boot Actuator在OpenShift环境中失效问题排查求助
本地运行的Spring Boot应用Actuator功能正常,但部署到OpenShift后无法使用,添加健康检查配置后应用甚至无法启动。
本地配置信息
application.properties
management.endpoints.web.exposure.include=info,health management.health.probes.enabled=true management.endpoint.health.enabled=true management.endpoint.health.probes.enabled=true
build.gradle
implementation 'org.springframework.data:spring-data-commons:2.7.5' implementation 'org.springframework.boot:spring-boot-starter-actuator:2.7.5' implementation 'org.springframework.boot:spring-boot-actuator-autoconfigure:2.7.5'
本地可正常访问以下端点:
- http://localhost:8081/actuator/health/liveness
- http://localhost:8081/actuator/health/readiness
OpenShift环境变量配置
MANAGEMENT_SECURITY_ENABLED=false MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=* MANAGEMENT_ENDPOINTS_WEB_BASE_PATH=/actuator MANAGEMENT_HEALTH_PROBES_ENABLED=true MANAGEMENT_ENDPOINT_HEALTH_ENABLED=true MANAGEMENT_ENDPOINT_HEALTH_PROBES_ENABLED=true MANAGEMENT_ENDPOINTS_ENABLED_BY_DEFAULT=true SPRINGDOC_SHOW_ACTUATOR=true SERVER_SERVLET_CONTEXT_PATH=/proxy/bankroute/
应用可正常启动,但访问<域名>/proxy/bankroute/actuator/时始终返回404错误。
OpenShift健康检查配置
readinessProbe: failureThreshold: 3 httpGet: path: /proxy/bankroute/actuator/health/readiness port: 9080 scheme: HTTP initialDelaySeconds: 15 periodSeconds: 10 successThreshold: 1 timeoutSeconds: 1
添加上述健康检查配置后,应用完全无法启动。
排查与解决步骤
端口匹配验证
本地应用监听8081端口,但OpenShift健康检查配置的是9080端口,需确认容器内应用实际监听的端口:- 若本地通过
server.port=8081修改过端口,在OpenShift中添加环境变量SERVER_PORT=8081,或调整健康检查的port值与应用监听端口一致。
- 若本地通过
修复配置冲突
MANAGEMENT_SECURITY_ENABLED在Spring Boot 2.7+版本已被废弃,该环境变量无效且可能干扰配置,建议移除。调整健康检查启动延迟
OpenShift中应用启动速度通常慢于本地,initialDelaySeconds=15可能不足以让应用完成启动,导致健康检查提前失败。建议将该值调整为30或更长时间。验证内部端点可达性
进入OpenShift容器内部,执行curl http://localhost:<应用端口>/proxy/bankroute/actuator/health/readiness:- 若内部访问正常,说明问题出在外部路由或权限配置,需检查OpenShift路由是否正确映射了
/proxy/bankroute/路径; - 若内部也返回404,说明应用配置存在问题,可查看容器日志中的请求映射信息,确认Actuator端点的实际暴露路径。
- 若内部访问正常,说明问题出在外部路由或权限配置,需检查OpenShift路由是否正确映射了
确认上下文路径拼接逻辑
当设置SERVER_SERVLET_CONTEXT_PATH=/proxy/bankroute/后,Actuator的完整路径应为/proxy/bankroute/actuator/health/readiness,可通过Spring Boot启动日志中的Mapped "{[/actuator/health/readiness],methods=[GET]}"类信息,确认路径是否正确拼接了上下文路径。
内容的提问来源于stack exchange,提问作者Jason

