All checks were successful
Publish to Confluence / confluence (push) Successful in 3m39s
90 lines
3.0 KiB
Markdown
90 lines
3.0 KiB
Markdown
<!-- Space: qifu -->
|
|
<!-- Parent: 后端技术&知识&规范 -->
|
|
<!-- Parent: 技术方案 -->
|
|
<!-- Parent: 基建 -->
|
|
<!-- Parent: 02-技术方案 -->
|
|
<!-- 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配置存活、就绪和启动探针](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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
``` |