赞
踩
预热方案有目前以下手段:
方案一在流量过高的时候本身就是必须存在的,但是目前我们的sentinel还在建设中
方案二和方案三需要微服务实例能够感知到服务是否已启动完成,如果感知不到,预热就无法进行了
什么是springboot actuator?
由此可见,actuator是springboot设计的精髓之一,而springboot或者jvm预热属于应用个性化功能,actuator目前的版本包括3.0的版本都没有照顾到。
另外,说到预热少不了就绪探针和存活探针:
熟悉了这些概念,看看 Spring Boot 集成是如何工作的。
从 Spring Boot 2.3 开始,LivenessStateHealthIndicator 和 ReadinessStateHealthIndicator 类将公开应用程序的活跃度和就绪状态。 当将应用程序部署到 Kubernetes 时,Spring Boot 会自动注册这些健康指标。
因此,可以分别使用 /actuator/health/liveness 和 /actuator/health/readiness 端点作为liveness 和 readiness 探针。
例如,以将这些添加到 pod 定义中,以将活性探针配置为 HTTP GET 请求:
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
如果使用 Spring Boot 2.3.2 +,可以使用以下属性来启用 liveness 和 readiness 探针:
management:
endpoint:
health:
probes:
enabled: true
health:
livenessstate:
enabled: true
readinessstate:
enabled: true
Spring Boot 使用两个枚举来封装不同的就绪和活跃状态。 对于就绪状态,有一个名为 ReadinessState 的枚举,具有以下值:
同样,LivenessState 枚举使用两个值表示应用程序的活跃状态:
以下是 Spring 中应用程序生命周期事件方面的就绪和活跃状态如何变化:
其实有了readiness就绪探针,我们就可以完成我们的预热工作,而Liveness存活探针用于k8s检测实例是否存活,这里有一个坑点:
不能使用/actuator/health来做存活探针!
因为/health进行严格检查springboot各项组件服务,比如邮件服务、数据库服务、mq服务等,当发现有一个组件处于非正常状态,其返回的内容会由{"status": "up"}
变为{"status": "down"}
,从而导致Liveness探针失效,而有些情况下,还抛出异常,在特定情况下某些服务不正常属于正常现象,例如:邮件服务。
ps: 我们在一次邮件服务迁移的过程中,使用Liveness探针频繁访问/health,触发了springboot连续抛出堆栈信息导致服务直接宕机,非常恐怖,如果没有做到宕机快照,会导致查问题无从下手
由于预热可以看做实例能否正常提供服务的健康指标,所以我采用了rediness探针,实例代码如下:
public class SeaReadinessHealthIndicator extends AvailabilityStateHealthIndicator { private Integer isChecking = 0; private StringBuffer notCompleteExecuteClassBuffer = new StringBuffer(); @Override protected void doHealthCheck(Health.Builder builder) { switch (isChecking.get()) { case 1: builder.down().withDetail("message", "instance is starting.").build(); return; case 2: builder.outOfService().withDetail("message", String.format("some service start error. they are: %s", notCompleteExecuteClassBuffer.toString())).build(); return; case 200: builder.up().build(); return; } } @Override protected AvailabilityState getState(ApplicationAvailability applicationAvailability) { return applicationAvailability.getReadinessState(); } }
这么设置后,访问/actuator/health/seaReadiness,发现无法访问,再检查/actuator/health目录,发现有一个"cn.xxx.seaReadiness"的状态是{"status":"UP"}
,原来actuator health的规则是SeaReadinessHealthIndicator
,HealthIndicator之前的默认为名称。如果是加入扫描的方式就是这样的,但我现在是用starter的方式进行发布的。
如果我要实现/actuator/health/seaReadiness访问怎么做呢?
在starter扫描的类名中,加上以下别名即可:
@Component("seaReadiness")
public class SeaReadinessHealthIndicator extends AvailabilityStateHealthIndicator
早期我们项目在几次生产过程中,为了安全,已将acuator目录做了禁止访问处理,只允许在k8s内部才能进行访问,所以可以考虑不用上spring security来对acuator目录进行保护。
参考资料:
https://blog.csdn.net/qq_39149842/article/details/118995017
https://mobilabsolutions.com/2020/04/a-proper-kubernetes-readiness-probe-with-spring-boot-actuator/
https://www.jdon.com/57471
https://mobilabsolutions.com/2020/04/a-proper-kubernetes-readiness-probe-with-spring-boot-actuator/
https://blog.51cto.com/u_11418075/4870067
https://kubesphere.io/zh/blogs/kubesphere-devops-java-microservice/
https://www.kancloud.cn/java-jdxia/java/1388077#health_73
https://blog.csdn.net/weixin_45503796/article/details/119246564
https://www.jianshu.com/p/ac0566c28562
https://spring.io/blog/2020/03/25/liveness-and-readiness-probes-with-spring-boot
https://blog.csdn.net/JHIII/article/details/126601858
https://blog.csdn.net/jiang18238032891/article/details/109745682
https://blog.csdn.net/weixin_43790623/article/details/104287216
https://blog.csdn.net/weixin_51291483/article/details/126596612
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。