<!-- Space: qifu -->
<!-- Parent: 后端技术&知识&规范 -->
<!-- Parent: 技术方案 -->
<!-- Parent: 基建 -->
<!-- Parent: 02-技术方案 -->
<!-- Parent: 20250110-K8s健康检查技术方案 -->
<!-- Title: 20250110-K8s健康检查迁移进度表 -->

<!-- Macro: :anchor\((.*)\):
     Template: ac:anchor
     Anchor: ${1} -->
<!-- Macro: \!\[.*\]\((.+)\)\<\!\-\- width=(.*) \-\-\>
     Template: ac:image
     Url: ${1}
     Width: ${2} -->
<!-- Macro: \<\!\-\- :toc: \-\-\>
     Template: ac:toc
     Printable: 'false'
     MinLevel: 2 
     MaxLevel: 4 -->
<!-- Include: 杂项/声明文件.md -->

<!-- :toc: -->

# K8s健康检查迁移进度表

## 服务迁移进度

| 服务                     | 开发 | 测试 | 预发布 | 生产 | 开发K8s | 测试K8s | 预发布K8s | 生产K8s |
|------------------------|----|----|-----|----|-------|-------|--------|-------|
| qifu-saas-eg(企赋南北流量网关) | √  | ×  | ×   | ×  | ×     | ×     | ×      | ×     |

## 迁移步骤

### 后端服务

#### 第一种：后端服务有引用 web-core 模块

- 更新 `parent` 版本

```xml

<parent>
    <artifactId>qifu-saas-parent</artifactId>
    <groupId>com.yuanmeng.qifu</groupId>
    <version>1.0.12-SNAPSHOT</version>
</parent>
```

#### 第二种：自定义实现

- 自定义实现健康检查接口 `/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> healthCheckFilter() {
        FilterRegistrationBean<HealthCheckFilter> 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);
            }
        }
    }
}
```