摘要:Spring Cloud网关路由匹配是Spring WebFlux HandlerMapping处理的一部分。Spring Cloud网关包含许多内置的路由断言工厂,这些内置断言可以匹配HTTP请求的不同属性,同时多个断言可以组合在一起使用。本节主要讲解Spr
Spring Cloud网关路由匹配是Spring WebFlux HandlerMapping处理的一部分。Spring Cloud网关包含许多内置的路由断言工厂,这些内置断言可以匹配HTTP请求的不同属性,同时多个断言可以组合在一起使用。本节主要讲解Spring Cloud Gateway中的一些常用配置。
1. After Route Predicate Factory配置
After Route Predicate Factory接收一个时间参数,当在指定时间之后进行请求时,匹配After Route Predicate断言。下面是AfterRoute Predicate断言配置示例,代码如下:
spring:
cloud:
gateway:
routes:
-id: after_route
uri: https://example.org
predicates:
-After=2017-01-20T17:42:47.789-07:00[America/Denver]
2. Before Route Predicate Factory配置
Before Route Predicate Factory接收一个时间参数,当在指定时间之前进行请求时,匹配Before Route Predicate断言。下面是Before Route Predicate断言配置示例代码:
spring:
cloud:
gateway:
routes:
-id: before_route
uri: https://example.org
predicates:
-Before=2017-01-20T17:42:47.789-07:00[America/Denver]
3. Between Route Predicate Factory配置
Between Route Predicate Factory接收两个时间参数。当请求时间在两个时间参数之间时,匹配Between Route Predicate断言。代码如下:
spring:
cloud:
gateway:
routes:
-id: between_route
uri: https://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 Route Predicate Factory接收两个参数,一个是cookie名称,另一个是正则表达式。当请求中的cookie名称和值匹配断言设置的cookie名称和正则表达式时通过。下面的这个例子中,断言要求的cookie名称是chocolate,cookie的值匹配ch.p正则表达式。
spring:
cloud:
gateway:
routes:
-id: cookie_route
uri: https://example.org
predicates:
-Cookie=chocolate, ch.p
5. header Route Predicate Factory配置
Header Route Predicate Factory接收两个参数,一个是header名称,另一个是正则表达式。当请求中的cookie名称和值匹配断言设置的header名称和正则表达式时通过。下面的这个例子中,断言要求的header名称是X-Request-Id,header属性值匹配\d+正则表达式。
spring:
cloud:
gateway:
routes:
-id: header_route
uri: https://example.org
predicates:
-Header=X-Request-Id, \d+
6. Host Route Predicate Factory配置
Host Route Predicate Factory接收一个参数,参数值包含一系列域名。下面的示例代码可以匹配www.somehost.org、beta.somehost.org或www.anotherhost.org域名。
spring:
cloud:
gateway:
routes:
-id: host_route
uri: https://example.org
predicates:
-Host=**.somehost.org,**.anotherhost.org
7. Method Route Predicate Factory配置
Method Route Predicate Factory接收一个HTTP方法参数,可以有一个或多个值。匹配GET和POST方法的请求,示例代码如下:
spring:
cloud:
gateway:
routes:
-id: method_route
uri: https://example.org
predicates:
-Method=GET,POST
8. Path Route Predicate Factory配置
Path Route Predicate Factory匹配请求路径的表达式。匹配/red/1、/red/blue和/blue/green请求的示例代码如下:
spring:
cloud:
gateway:
routes:
-id: path_route
uri: https://example.org
predicates:
-Path=/red/{segment},/blue/{segment}
9. Query Route Predicate Factory配置
Query Route Predicate Factory接收两个参数,一个是必传参数,另一个是可选表达式。在下面的例子中,要求请求需要匹配red参数及gree.表达式参数。
spring:
cloud:
gateway:
routes:
-id: query_route
uri: https://example.org
predicates:
-Query=red, gree.
10. RemoteAddr Route Predicate Factory配置
Remoteaddr Route Predicate Factory接收一系列IPv4/IPv6地址。匹配192.168.1.10地址请求的示例代码如下:
spring:
cloud:
gateway:
routes:
-id: remoteaddr_route
uri: https://example.org
predicates:
-RemoteAddr=192.168.1.1/24
11. Weight Route Predicate Factory配置
Weight Route Predicate Factory接收两个参数,即group和weight。下面的例子表示80%的请求被路由到weighthigh.org域名,20%的请求被路由调用到weighlow.org域名。
spring:
cloud:
gateway:
routes:
-id: weight_high
uri: https://weighthigh.org
predicates:
-Weight=group1, 8
-id: weight_low
uri: https://weightlow.org
predicates:
-Weight=group1, 2
路由过滤器允许修改HTTP请求和HTTP响应数据。Spring Cloud网关内置了很多过滤器。本节主要介绍这些内置的过滤器与匹配方式。
1. AddRequestHeader GatewayFilter Factory配置
AddRequestHeader GatewayFilter Factory接收两个参数:name和value。为请求添加X-Request-red:blue header属性的示例代码如下:
spring:
cloud:
gateway:
routes:
-id: add_request_header_route
uri: https://example.org
filters:
-AddRequestHeader=X-Request-red, blue.
2. AddRequestParameter GatewayFilter Factory配置
AddRequestParameter GatewayFilter Factory可以为HTTP请求增加请求参数。为请求添加red=blue参数的示例代码如下:
spring:
cloud:
gateway:
routes:
-id: add_request_parameter_route
uri: https://example.org
filters:
-AddRequestParameter=red, blue.
3. AddResponseHeader GatewayFilter Factory配置
AddResponseHeader GatewayFilter Factory为HTTP响应头添加属性。为请求响应头添加X-Response-Foo:Bar属性的示例代码如下:
spring:
cloud:
gateway:
routes:
-id: add_response_header_route
uri: https://example.org
filters:
-AddResponseHeader=X-Response-Foo, Bar
4. MapRequestHeader GatewayFilter Factory配置
MapRequestHeader GatewayFilter Factory会将fromHeader中的值添加到toHeader中。将Blue的属性值添加到X-Request-Red头属性中,示例代码如下:
spring:
cloud:
gateway:
routes:
-id: map_request_header_route
uri: https://example.org
filters:
-MapRequestHeader=Blue, X-Request-Red
5. PrefixPath GatewayFilter Factory配置
PrefixPath GatewayFilter Factory为请求路径添加前缀。为请求的路径添加/mypath路径,例如请求/hello,则被修改为/mypath/hello,示例代码如下:
spring:
cloud:
gateway:
routes:
-id: prefixpath_route
uri: https://example.org
filters:
-PrefixPath=/mypath
6. RedirectTo GatewayFilter Factory配置
RedirectTo GatewayFilter Factory是重定向配置。例如,返回状态码302,并且重定向到https://acme.org,代码如下:
spring:
cloud:
gateway:
routes:
-id: prefixpath_route
uri: https://example.org
filters:
-RedirectTo=302, https://acme.org
7. RemoveRequestHeader GatewayFilter Factory配置
RemoveRequestHeader GatewayFilter Factory用于删除请求中的特定header。例如,删除X-Request-Foo属性,代码如下:
spring:
cloud:
gateway:
routes:
-id: removerequestheader_route
uri: https://example.org
filters:
-RemoveRequestHeader=X-Request-Foo
8. RemoveResponseHeader GatewayFilter Factory配置
RemoveResponseHeader GatewayFilter Factory用于删除响应头属性。例如,删除响应头中的X-Response-Foo属性,代码如下:
spring:
cloud:
gateway:
routes:
-id: removeresponseheader_route
uri: https://example.org
filters:
-RemoveResponseHeader=X-Response-Foo
9. RemoveRequestParameter GatewayFilterFactory配置
RemoveRequestParameter GatewayFilter Factory用于删除特定的请求参数。例如,删除请求参数red,代码如下:
spring:
cloud:
gateway:
routes:
-id: removerequestparameter_route
uri: https://example.org
filters:
-RemoveRequestParameter=red
10. Retry GatewayFilter Factory配置
Retry GatewayFilter Factory提供请求重试配置。例如,一个简单的重试配置方式的代码如下:
spring:
cloud:
gateway:
routes:
-id: retry_test
uri: http://localhost:8080/flakey
predicates:
-Host=*.retry.com
filters:
-name: Retry
args:
retries: 3
statuses: BAD_GATEWAY
methods: GET,POST
backoff:
firstBackoff: 10ms
maxBackoff: 50ms
factor: 2
basedOnPreviousValue: false
11. FallbackHeaders GatewayFilter Factory配置
FallbackHeaders GatewayFilter Factory可以提供熔断降级功能。Fallback配置方式的示例代码如下:
12. RequestRateLimiter GatewayFilter Factory配置
RequestRateLimiter GatewayFilter Factory提供请求重试配置。一个简单的重试配置方式的示例代码如下:
Spring Cloud网关除了核心配置外,还提供了其他的全局配置信息,如超时配置和CORS配置。
超时配置方式的示例代码如下:
spring:
cloud:
gateway:
connect-timeout: 1000
response-timeout: 5s
cors配置方式的示例代码如下:
spring:
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "https://docs.spring.io"
allowedMethods:
-GET
来源:大数据架构师