Gateway 简介
Spring Cloud Gateway 基于 Spring 5、Spring Boot 2 和 Project Reactor 等技术,是在 Spring 生态系统之上构建的 API 网关服务,Gateway 旨在提供一种简单而有效的方式来对 API 进行路由以及提供一些强大的过滤器功能,例如熔断、限流、重试等
Spring Cloud Gateway 具有如下特性:
- 基于 Spring Framework 5、Project Reactor 以及 Spring Boot 2.0 进行构建
- 能够匹配任何请求属性
- 可以对路由指定 Predicate(断言)和 Filter(过滤器)
- 集成 Hystrix 的断路器功能
- 集成 Spring Cloud 服务发现功能
- 易于编写的 Predicate 和 Filter
- 请求限流功能
- 路径重写
Gateway 快速入门
创建项目,引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
在配置文件 application.yml 添加如下配置
server:
port: 9201 # 指定运行端口
spring:
application:
name: gateway-service # 指定服务名称
cloud:
gateway:
routes:
- id: path_route # 路由ID
uri: http://localhost:8201/user/getUser # 匹配后路由地址
predicates: # 断言,路径相匹配的路由
- Path=/user/getUser
也可以按如下配置
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route2", r -> r.path("/user/getUserInfo")
.uri("http://localhost:8201/user/getUserInfo"))
.build();
}
}
Gateway 路由工厂
Spring Cloud Gateway 包括许多内置的路由断言工厂,所有这些断言都与 HTTP 请求的不同属性匹配,多个路由断言工厂可以进行组合
1. After Route Predicate Factory
在指定时间之后的请求会匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
2. Before Route Predicate Factory
在指定时间之前的请求会匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
3. Between Route Predicate Factory
在指定时间区间内的请求会匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
4. Cookie Route Predicate Factory
带有指定 Cookie 的请求会匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- Cookie=milk, yili # cookie为milk=yili
5. Header Route Predicate Factory
带有指定请求头的请求会匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- Header=X-Request-Id, 1 # 请求头为X-Request-Id=1
6. Host Route Predicate Factory
带有指定 Host 的请求会匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- Host=**.somehost.org # 请求头为Host:www.somehost.org的请求可以匹配该路由
7. Method Route Predicate Factory
发送指定方法的请求会匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- Method=GET,POST
8. Path Route Predicate Factory
发送指定路径的请求会匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- Path=/red/(segment],/blue/(segment) # /red/1或/blue/1路径请求可以匹配该路由
9. Query Route Predicate Factory
带指定查询参数的请求可以匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- Query=green # 带green=l查询参数的请求可以匹配该路由
10. RemoteAddr Route Predicate Factory
从指定远程地址发起的请求可以匹配该路由
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://example.org
predicates:
- RemoteAddr=192.168.1.1/24 # 从192.168.1.1发起请求可以匹配该路由