深入探索Spring Cloud Gateway:微服务网关的最佳实践
深入探索Spring Cloud Gateway:微服务网关的最佳实践
Spring Cloud Gateway作为Spring Cloud框架的第二代网关,以其强大的功能和优秀的性能,逐渐取代了第一代网关Zuul。本文将深入探讨Spring Cloud Gateway的核心概念、使用方法和最佳实践,帮助开发者更好地理解和应用这一重要的微服务网关组件。
一、网关定义
API网关是一个反向路由,屏蔽内部细节,为调用者提供统一入口,接收所有调用者请求,通过路由机制转发到服务实例。API网关是一组“过滤器Filter”集合,可以实现一系列与核心业务无关的横切面功能,如安全认证、限流熔断、日志监控。
二、快速开始
网关启动步骤(代码演示)
- 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
- 配置文件
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: example_route
uri: lb://example-service
predicates:
- Path=/example/**
filters:
- AddRequestHeader=X-Example, ExampleValue
- StripPrefix=1
- id: rate_limited_route
uri: http://ratelimited.org
predicates:
- Path=/ratelimited/**
filters:
- RequestRateLimiter=redis-rate-limiter
- id: retry_route
uri: http://retry.org
predicates:
- Path=/retry/**
filters:
- Retry=5
default-filters:
- AddResponseHeader=X-Response-Default, Default
globalcors:
corsConfigurations:
'[/**]':
allowedOrigins: "*"
allowedMethods:
- GET
- POST
- DELETE
- PUT
三、Spring Cloud Gateway架构图
客户端向Spring Cloud Gateway发出请求。在Gateway Handler Mapping中找到请求相对匹配路由(这个时候就用到predicate),则将其发送到Gateway web handler处理。handler处理请求时会经过一系列的过滤器链。过滤器链被虚线划分的原因是过滤器链可以在发送代理请求之前或之后执行过滤逻辑。先执行所有pre过滤器逻辑,然后进行代理请求。在发出代理请求之后,收到代理服务的响应之后执行post过滤器逻辑。这跟Zuul的处理过程很类似。在执行所有pre过滤器逻辑时,往往进行了鉴权、限流、日志输出等功能,以及请求头的更改、协议的转换;转发之后收到响应之后,会执行所有post过滤器的逻辑,在这里可以响应数据进行了修改,比如响应头、协议的转换等。在上面的处理过程中,有一个重要的点就是将请求和路由进行匹配,这时候就需要用到predicate,它是决定了一个请求走哪一个路由。
四、Spring Cloud Gateway核心组件
1. Route路由
Gateway的基本构建模块,它由ID、目标URL、断言集合和过滤器集合组成。如果聚合断言结果为真,则匹配到该路由。
2. Predicate断言
这是一个Java 8 Function Predicate。输入类型是Spring Framework ServerWebExchange。允许开发人员匹配来自HTTP请求的任何内容,例如Header或参数。Predicate接受一个输入参数,返回一个布尔值结果。
3. Filter过滤器
支持通过spi扩展。实现GatewayFilter,Ordered接口。
五、Gateway限流
在Spring Cloud Gateway中,有Filter过滤器,因此可以在“pre”类型的Filter中自行实现上述三种过滤器。但是限流作为网关最基本的功能,Spring Cloud Gateway官方就提供了RequestRateLimiterGatewayFilterFactory这个类,适用Redis和Lua脚本实现了令牌桶链接的方式。
六、Zuul与Spring Cloud Gateway比较
特性 | Gateway | Zuul |
---|---|---|
线程开销 | 小 | 大 |
编码模型 | 复杂 | 简单 |
功能丰富度 | 丰富 | 基本 |
官方支持 | 重点支持 | 已弃用 |
七、Spring Cloud Gateway与Nginx组合使用
Nginx通常用于处理静态内容、SSL终止、负载均衡等,而Spring Cloud Gateway主要用于动态路由、过滤和服务网关功能。下面是一个基本的配置示例,展示了如何将Nginx和Spring Cloud Gateway结合使用。
nginx.conf配置示例
http {
upstream gateway {
server localhost:8080;
}
server {
listen 80;
location / {
proxy_pass http://gateway;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
Spring Cloud Gateway与Nacos组合使用
bootstrap.yml配置
spring:
application:
name: gateway-service
cloud:
nacos:
discovery:
server-addr: ${NACOS_SERVER_ADDR:localhost:8848}
config:
server-addr: ${NACOS_SERVER_ADDR:localhost:8848}
file-extension: yaml
在Nacos中创建配置:
- 登录Nacos控制台。
- 创建一个新的配置,例如gateway-service.yaml。
- 在配置文件中添加路由规则,例如:
spring:
cloud:
gateway:
routes:
- id: example-service
uri: lb://example-service
predicates:
- Path=/example/**
- id: another-service
uri: lb://another-service
predicates:
- Path=/another/**