From f7328b0b37571a11e00111a89713db050dd49156 Mon Sep 17 00:00:00 2001 From: liuxiaohua Date: Fri, 10 Jan 2025 15:18:30 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20[2025-01-10]=20K8s=20=E5=81=A5?= =?UTF-8?q?=E5=BA=B7=E6=A3=80=E6=9F=A5=E8=BF=81=E7=A7=BB=E8=BF=9B=E5=BA=A6?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 文档/20250110-K8s健康检查迁移进度表.md | 99 ++++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 5 deletions(-) diff --git a/文档/20250110-K8s健康检查迁移进度表.md b/文档/20250110-K8s健康检查迁移进度表.md index d9a2a15..694fa35 100644 --- a/文档/20250110-K8s健康检查迁移进度表.md +++ b/文档/20250110-K8s健康检查迁移进度表.md @@ -22,13 +22,102 @@ # K8s健康检查迁移进度表 -## K8s 探针 - -- [K8s配置存活、就绪和启动探针](https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/) -- 后端服务使用 httpGet 方式,请求 `/actuator/health` 做服务就绪检查 - ## 服务迁移进度 | 服务 | 开发 | 测试 | 预发布 | 生产 | 开发K8s | 测试K8s | 预发布K8s | 生产K8s | |------------------------|----|----|-----|----|-------|-------|--------|-------| | qifu-saas-eg(企赋南北流量网关) | √ | × | × | × | × | × | × | × | + +## 迁移步骤 + +### 后端服务 + +#### 第一种:后端服务有引用 web-core 模块 + +- 更新 `parent` 版本 + +```xml + + + qifu-saas-parent + com.yuanmeng.qifu + 1.0.12-SNAPSHOT + +``` + +#### 第二种:自定义实现 + +- 自定义实现健康检查接口 `/actuator/health` +- 可以参考 [后端健康检查接口](#后端健康检查接口) 下的 代码示例 + +### K8s + +- 就绪探针使用 httpGet 方式,请求 `/actuator/health` 做服务就绪检查 + +----- + +## K8s 探针 + +- [K8s配置存活、就绪和启动探针](https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/) +- 使用 httpGet 方式,请求 `/actuator/health` 做服务就绪检查 + +## 后端健康检查接口 + +- 使用 `Filter` 实现健康检查接口处理 +- 代码集成在 `web-core` 中 +- 代码示例: + +```java +package com.yuanmeng.engine.web.core.config; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.Ordered; +import org.springframework.http.HttpStatus; + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@Configuration +@ConditionalOnProperty(prefix = "yuanmeng.web-core", name = "enable-health-check", matchIfMissing = true) +public class HealthCheckFilterConfig { + + @Bean + public FilterRegistrationBean healthCheckFilter() { + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); + registrationBean.setFilter(new HealthCheckFilter()); + registrationBean.addUrlPatterns("/actuator/health"); + registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE); + return registrationBean; + } + + @Slf4j + public static class HealthCheckFilter implements Filter { + + @Override + public void init(FilterConfig filterConfig) { + log.info("[web-core]: HealthCheckFilter init"); + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) + throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest) request; + HttpServletResponse httpResponse = (HttpServletResponse) response; + + if (httpRequest.getRequestURI().contains("/actuator/health")) { + log.info("[web-core]: receive health check"); + httpResponse.setStatus(HttpStatus.OK.value()); + httpResponse.getWriter().write("{\"status\":\"UP\"}"); + } else { + chain.doFilter(request, response); + } + } + } +} +``` \ No newline at end of file