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

相关推荐
洛神灬殇5 小时前
【LLM大模型技术专题】「入门到精通系列教程」基于ai-openai-spring-boot-starter集成开发实战指南
网络·数据库·微服务·云原生·架构
亚林瓜子6 小时前
AWS API Gateway配置日志
云计算·gateway·aws·log·cloudwatch
eternal__day11 小时前
Spring Cloud 多机部署与负载均衡实战详解
java·spring boot·后端·spring cloud·负载均衡
啾啾Fun13 小时前
【Java微服务组件】分布式协调P4-一文打通Redisson:从API实战到分布式锁核心源码剖析
java·redis·分布式·微服务·lua·redisson
惊鸿一博13 小时前
java_网络服务相关_gateway_nacos_feign区别联系
java·开发语言·gateway
记得开心一点嘛21 小时前
使用MinIO搭建自己的分布式文件存储
分布式·spring cloud·minio
后海 0_o1 天前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构
喵叔哟1 天前
24.【.NET8 实战--孢子记账--从单体到微服务--转向微服务】--单体转微服务--认证微服务
微服务·架构·.net
bing_1581 天前
跨多个微服务使用 Redis 共享数据时,如何管理数据一致性?
redis·微服务·mybatis
hanniuniu131 天前
网络安全厂商F5推出AI Gateway,化解大模型应用风险
人工智能·web安全·gateway