Spring Cloud 微服务中 gateway 网关如何设置健康检测端点

主要是为了让 k8s 识别到网关项目已经就绪,但是又不想在里面通过 Controller 实现。因为在 Controller 中这样做并不是最佳实践,因为 Gateway 的设计初衷是专注于路由和过滤,而不是业务逻辑的处理。

Gateway 中配置健康检查端点可以通过以下方式进行(可根据实际需求进行扩展):

1. 自定义路由配置(推荐)

可以使用 Spring Cloud Gateway 的 Java DSL 配置自定义的路由,以在网关中添加一个专门用于健康检查的路由。例如:

java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.*;

@Configuration
public class GatewayConfig {

    @Bean
    public RouterFunction<ServerResponse> healthCheckRoute() {
        return route(GET("/health-check"),
                request -> ServerResponse.status(HttpStatus.OK)
                        .body(BodyInserters.fromValue("Gateway is healthy")));
    }
}

这样,就可以通过访问 http://localhost:8080/health-check 来执行健康检查。

上面代码的描述说明:

  • route 方法是 Spring Cloud Gateway 提供的 Java DSL(领域特定语言,Domain-Specific Language)中的一部分。这是一种声明性的路由配置方式,允许使用流畅的 API 配置路由规则。
  • GatewayConfig 类中,healthCheckRoute 方法返回一个 RouterFunction<ServerResponse>,这个函数式接口是用来配置路由规则的。route 方法是用于创建路由规则的,它接收一个请求谓词(RequestPredicate)和一个处理函数(HandlerFunction)作为参数。
  • 具体来说,route(GET("/health-check"), ...) 表示创建一个满足 GET 请求谓词,并且路径为 /health-check 的路由规则。接着,通过 ServerResponse 构建响应,这里设置为 HTTP 状态码 HttpStatus.OK,并返回一条消息 "Gateway is healthy"。

这种方式更加直观和类型安全,相比于配置文件,可以在代码中清晰地看到路由规则的定义。这是 Spring WebFlux 框架提供的一种路由方式,用于构建响应式的、非阻塞的 Web 应用程序。

2. 利用 Actuator 健康检查

可以通过配置属性来启用 Actuator,并将其端口设置为用于健康检查的端口。

要想使用 Actuator,项目中需要引入 Actuator 的依赖:

xml 复制代码
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

application.propertiesapplication.yml 配置文件中添加以下配置:

bash 复制代码
# application.properties
management.endpoints.web.exposure.include=health
management.server.port=8081  # 可与 server.port 相同或不同

或者

yaml 复制代码
# application.yaml
management:
  endpoints:
    web:
      exposure:
        include: health
  server:
    port: 8081  # 可与 server.port 相同或不同

上述配置将 Actuator 暴露的端点配置为仅包含 health,并将健康检查端口设置为 8081。此时,可以通过访问 http://localhost:8081/actuator/health 来执行健康检查。

【注】Actuator 中还有其他配置,实际使用过程中建议查看 官方文档 了解相应的配置,以免给自己挖坑。比如 /env/beans,它们可能会泄漏应用程序的敏感信息。确保只在受信任的环境中启用这些端点,并谨慎处理它们的输出。

3. 通过 RestController 实现(不推荐)

如果有业务逻辑需要处理,更推荐将业务逻辑集中在后端微服务中,而将 Gateway 专注于路由和过滤。这样可以更好地保持清晰的代码结构和单一责任原则。

java 复制代码
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author roc
 * @date 2024/1/16 14:14
 */
@RestController
@RequestMapping("/health")
public class HealthController {

    @GetMapping("/check")
    public String check() {
        return "Gateway is healthy";
    }
}

如果还有其他方式请大佬们分享一下。

个人博客:Roc's Blog

相关推荐
小猪咪piggy1 小时前
【微服务】(1) Spring Cloud 概述
java·spring cloud·微服务
choice of7 小时前
Sentinel:阿里云高并发流量控制
笔记·spring cloud·sentinel
xrkhy7 小时前
微服务之SpringCloud Alibaba(注册中心Nacos)
spring cloud·微服务·架构
会开花的二叉树7 小时前
C++微服务 UserServer 设计与实现
开发语言·c++·微服务
唐僧洗头爱飘柔95278 小时前
【SpringCloud(6)】Gateway路由网关;zuul路由;gateway实现原理和架构概念;gateway工作流程;静态转发配置
spring·spring cloud·架构·gateway·请求转发·服务降级·服务雪崩
来旺8 小时前
互联网大厂Java面试全解析及三轮问答专项
java·数据库·spring boot·安全·缓存·微服务·面试
Knight_AL9 小时前
Spring Boot 中使用自定义注解和 AOP 实现微服务日志记录(包含 URL、状态码和耗时信息)
linux·spring boot·微服务
鹿里噜哩10 小时前
Nacos跨Group及Namespace发现服务
java·spring cloud
一个处女座的暖男程序猿10 小时前
若依微服务 nacos的配置文件
微服务·云原生·架构
喵叔哟11 小时前
63.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--新增功能--预算告警
运维·微服务·.net