目录安排上
- Gateway路由断言器
- Gateway过滤器工厂
- 1.AddRequestHeader GatewayFilter Factory
- 2.AddRequestHeader GatewayFilter Factory
- 3.AddResponseHeader GatewayFilter Factory
- 4.Hystrix GatewayFilter Factory
- 5.FallbackHeaders GatewayFilter Factory
- 6.FallbackHeaders GatewayFilter Factory
- 7.PreserveHostHeader GatewayFilter Factory
- 8.RequestRateLimiter GatewayFilter Factory
- 9.RemoveRequestHeader GatewayFilter Factory
- 10.RemoveResponseHeader GatewayFilter Factory
- 11.RewritePath GatewayFilter Factory
- 12.RewriteResponseHeader GatewayFilter Factory
- 13.SaveSession GatewayFilter Factory
- 14.StripPrefix GatewayFilter Factory
- 15.RequestSize GatewayFilter Factory
Gateway路由断言器
1.After 路由断言 Factory
指定日期时间之后
- 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 路由断言 Factory
指定日期时间之前
- Before Route Predicate Factory采用一个参数——日期时间。在该日期时间之前发生的请求都将被匹配。
spring:
cloud:
gateway:
routes:
- id: before_route
uri: http://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
3.Between 路由断言 Factory
指定日期时间之间
- Between 路由断言 Factory有两个参数,datetime1和datetime2。在datetime1和datetime2之间的请求将被匹配。datetime2参数的实际时间必须在datetime1之后。
spring:
cloud:
gateway:
routes:
- id: between_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 路由断言 Factory
指定cookie(正则)
- Cookie 路由断言 Factory有两个参数,cookie名称和正则表达式。请求包含次cookie名称且正则表达式为真的将会被匹配。
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: http://example.org
predicates:
- Cookie=chocolate, ch.p
5.Header 路由断言 Factory
指定请求头(正则)
- Header 路由断言 Factory有两个参数,header名称和正则表达式。请求包含次header名称且正则表达式为真的将会被匹配。
spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://example.org
predicates:
- Header=X-Request-Id, \d+
6.Host 路由断言 Factory
指定Host
- Host 路由断言 Factory包括一个参数:host name列表。使用Ant路径匹配规则,
.
作为分隔符。表达式为真的将会被匹配。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
7.Method 路由断言 Factory
指定请求方式(GET/POST)
- Method 路由断言 Factory只包含一个参数: 需要匹配的HTTP请求方式
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://example.org
predicates:
# 所有GET请求都将被路由
- Method=GET
8.Path 路由断言 Factory
指定路径
- Path 路由断言 Factory 有2个参数: 一个Spring
PathMatcher
表达式列表和可选matchOptionalTrailingSeparator
标识 .
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://example.org
predicates:
# segment为Map类型变量
- Path=/foo/{segment},/bar/{segment}
# 也可以直接写出 **代表所有请求都通过
# - Path=/consumer/**
例如: /foo/1
or /foo/bar
or /bar/baz
的请求都将被匹配
URI 模板变量 (如上例中的 segment
) 将以Map的方式保存于ServerWebExchange.getAttributes()
key为ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE
. 这些值将在GatewayFilter Factories使用
可以使用以下方法来更方便地访问这些变量。
Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);
String segment = uriVariables.get("segment");
9.Query 路由断言 Factory
指定参数名
- Query 路由断言 Factory 有2个参数: 必选项
param
和可选项regexp
.
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://example.org
predicates:
- Query=baz
则包含了请求参数 baz
的都将被匹配。
指定参数名和参数值
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://example.org
predicates:
- Query=foo, ba.
如果请求参数里包含foo
参数,并且值匹配为ba.
表达式,则将会被路由,如:bar
and baz
10.RemoteAddr 路由断言 Factory
指定IP
- RemoteAddr 路由断言 Factory的参数为 一个CIDR符号(IPv4或IPv6)字符串的列表,最小值为1,例如192.168.0.1/16(其中192.168.0.1是IP地址并且16是子网掩码)。
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: http://example.org
predicates:
- RemoteAddr=192.168.1.1/24
如果请求的remote address 为 192.168.1.10
则将被路由
Gateway过滤器工厂
过滤器允许以某种方式修改传入的HTTP请求或返回的HTTP响应。过滤器的作用域是某些特定路由。Spring Cloud Gateway包括许多内置的 Filter工厂。
注意:有关如何使用以下任何过滤器的更详细示例,请查看unit tests.。
1.AddRequestHeader GatewayFilter Factory
指定请求头(键值对)
- 采用一对名称和值作为参数
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: http://example.org
filters:
- AddRequestHeader=X-Request-Foo, Bar
对于所有匹配的请求,这将向下游请求的头中添加 x-request-foo:bar
header
2.AddRequestHeader GatewayFilter Factory
指定参数值(键值对)
- 采用一对名称和值作为参数
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: http://example.org
filters:
- AddRequestParameter=foo, bar
对于所有匹配的请求,这将向下游请求添加foo=bar
查询字符串
3.AddResponseHeader GatewayFilter Factory
指定响应头(键值对)
- 采用一对名称和值作为参数
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: http://example.org
filters:
- AddResponseHeader=X-Response-Foo, Bar
对于所有匹配的请求,这会将x-response-foo:bar
头添加到下游响应的header中
4.Hystrix GatewayFilter Factory
Hystrix服务熔断
-
Hystrix 是Netflix开源的断路器组件。Hystrix GatewayFilter允许你向网关路由引入断路器,保护你的服务不受级联故障的影响,并允许你在下游故障时提供fallback响应。
-
要在项目中启用Hystrix网关过滤器,需要添加对
spring-cloud-starter-netflix-hystrix
的依赖 Spring Cloud Netflix. -
Hystrix GatewayFilter Factory 需要一个name参数,即
HystrixCommand
的名称。
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: http://example.org
filters:
- Hystrix=myCommandName
这将剩余的过滤器包装在命令名为“myCommandName”的HystrixCommand
中。
Hystrix服务熔断(指定uri)
- hystrix过滤器还可以接受可选的
fallbackUri
参数。目前,仅支持forward:
预设的URI,如果调用fallback,则请求将转发到与URI匹配的控制器。
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: lb://backing-service:8088
predicates:
- Path=/consumingserviceendpoint
filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/incaseoffailureusethis
- RewritePath=/consumingserviceendpoint, /backingserviceendpoint
当调用hystrix fallback时,这将转发到/incaseoffailureusethis
uri。注意,这个示例还演示了(可选)通过目标URI上的’lb`前缀,使用Spring Cloud Netflix Ribbon 客户端负载均衡。
Hystrix服务熔断调用外部
- 主要场景是使用
fallbackUri
到网关应用程序中的内部控制器或处理程序。但是,也可以将请求重新路由到外部应用程序中的控制器或处理程序,如:
spring:
cloud:
gateway:
routes:
- id: ingredients
uri: lb://ingredients
predicates:
- Path=//ingredients/**
filters:
- name: Hystrix
args:
name: fetchIngredients
fallbackUri: forward:/fallback
- id: ingredients-fallback
uri: http://localhost:9994
predicates:
- Path=/fallback
在本例中,gateway应用程序中没有 fallback
实现,但是另一个应用程序中有一个接口实现,注册为“http://localhost:9994”。
在将请求转发到fallback的情况下,Hystrix Gateway过滤还支持直接抛出Throwable
。它被作为ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR
属性添加到ServerWebExchange
中,可以在处理网关应用程序中的fallback时使用。
对于外部控制器/处理程序方案,可以添加带有异常详细信息的header。可以在 FallbackHeaders GatewayFilter Factory section.中找到有关它的更多信息。
hystrix配置参数(如 timeouts)可以使用全局默认值配置,也可以使用Hystrix wiki中所述属性进行配置。
要为上面的示例路由设置5秒超时,将使用以下配置:
hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 5000
5.FallbackHeaders GatewayFilter Factory
Hystrix服务熔断调用外部
FallbackHeaders
允许在转发到外部应用程序中的FallbackUri
的请求的header中添加Hystrix异常详细信息,如下所示:
spring:
cloud:
gateway:
routes:
- id: ingredients
uri: lb://ingredients
predicates:
- Path=//ingredients/**
filters:
- name: Hystrix
args:
name: fetchIngredients
fallbackUri: forward:/fallback
- id: ingredients-fallback
uri: http://localhost:9994
predicates:
- Path=/fallback
filters:
- name: FallbackHeaders
args:
executionExceptionTypeHeaderName: Test-Header
在本例中,在运行HystrixCommand
发生执行异常后,请求将被转发到 localhost:9994
应用程序中的 fallback
终端或程序。异常类型、消息(如果可用)cause exception类型和消息的头,将由FallbackHeaders
filter添加到该请求中。
通过设置下面列出的参数值及其默认值,可以在配置中覆盖headers的名称:
executionExceptionTypeHeaderName
("Execution-Exception-Type"
)executionExceptionMessageHeaderName
("Execution-Exception-Message"
)rootCauseExceptionTypeHeaderName
("Root-Cause-Exception-Type"
)rootCauseExceptionMessageHeaderName
("Root-Cause-Exception-Message"
)
Hystrix 如何实现的更多细节可以参考 Hystrix GatewayFilter Factory section.
6.FallbackHeaders GatewayFilter Factory
添加路径前缀
- PrefixPath GatewayFilter 只有一个
prefix
参数.
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: http://example.org
filters:
- PrefixPath=/mypath
这将给所有匹配请求的路径加前缀/mypath
。因此,向/hello
发送的请求将发送到/mypath/hello
。
7.PreserveHostHeader GatewayFilter Factory
原始请求头
- 该filter没有参数。设置了该Filter后,GatewayFilter将不使用由HTTP客户端确定的host header ,而是发送原始host header 。
spring:
cloud:
gateway:
routes:
- id: preserve_host_route
uri: http://example.org
filters:
- PreserveHostHeader
8.RequestRateLimiter GatewayFilter Factory
是否允许继续执行当前请求
- RequestRateLimiter使用
RateLimiter
实现是否允许继续执行当前请求。如果不允许继续执行,则返回HTTP 429 - Too Many Requests
(默认情况下)。 - 这个过滤器可以配置一个可选的
keyResolver
参数和rate limiter参数(见下文)。 keyResolver
是KeyResolver
接口的实现类.在配置中,按名称使用SpEL引用bean。#{@myKeyResolver}
是引用名为’myKeyResolver’的bean的SpEL表达式。- KeyResolver.java.
public interface KeyResolver {
Mono<String> resolve(ServerWebExchange exchange);
}
KeyResolver
接口允许使用可插拔策略来派生限制请求的key。在未来的里程碑版本中,将有一些KeyResolver
实现。KeyResolver
的默认实现是PrincipalNameKeyResolver
,它从ServerWebExchange
检索Principal
并调用Principal.getName()
。- 默认情况下,如果
KeyResolver
没有获取到key,请求将被拒绝。此行为可以使用spring.cloud.gateway.filter.request-rate-limiter.deny-empty-key
(true or false) 和spring.cloud.gateway.filter.request-rate-limiter.empty-key-status-code
属性进行调整。
说明
无法通过"shortcut" 配置RequestRateLimiter。以下示例*无效
- application.properties.
# INVALID SHORTCUT CONFIGURATION
spring.cloud.gateway.routes[0].filters[0]=RequestRateLimiter=2, 2, #{@userkeyresolver}
9.RemoveRequestHeader GatewayFilter Factory
删除请求头
- 有一个
name
参数. 这是要删除的header的名称。
spring:
cloud:
gateway:
routes:
- id: removerequestheader_route
uri: http://example.org
filters:
- RemoveRequestHeader=X-Request-Foo
这将在X-Request-Foo
header被发送到下游之前删除它。
10.RemoveResponseHeader GatewayFilter Factory
删除响应头
- 有一个
name
参数. 这是要删除的header的名称。
spring:
cloud:
gateway:
routes:
- id: removeresponseheader_route
uri: http://example.org
filters:
- RemoveResponseHeader=X-Response-Foo
这将在返回到网关client之前从响应中删除x-response-foo
头。
11.RewritePath GatewayFilter Factory
重写请求路径
- 包含一个
regexp
正则表达式参数和一个replacement
参数. 通过使用Java正则表达式灵活地重写请求路径。
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: http://example.org
predicates:
- Path=/foo/**
filters:
- RewritePath=/foo(?<segment>/?.*), $\{segment}
对于请求路径/foo/bar
,将在发出下游请求之前将路径设置为/bar
。注意,由于YAML规范,请使用 $\
替换 $
。
12.RewriteResponseHeader GatewayFilter Factory
重写响应头
- 包含
name
,regexp
和replacement
参数.。通过使用Java正则表达式灵活地重写响应头的值。
spring:
cloud:
gateway:
routes:
- id: rewriteresponseheader_route
uri: http://example.org
filters:
- RewriteResponseHeader=X-Response-Foo, , password=[^&]+, password=***
对于一个/42?user=ford&password=omg!what&flag=true
的header值,在做下游请求时将被设置为/42?user=ford&password=***&flag=true
,由于YAML规范,请使用 $\
替换 $
。
13.SaveSession GatewayFilter Factory
Spring Session
- SaveSession GatewayFilter Factory将调用转发到下游之前强制执行
WebSession::save
操作。这在使用 Spring Session 之类时特别有用,需要确保会话状态在进行转发调用之前已保存。
spring:
cloud:
gateway:
routes:
- id: save_session
uri: http://example.org
predicates:
- Path=/foo/**
filters:
- SaveSession
如果你希望要将[Spring Security](https://projects.spring.io/Spring Security/)与Spring Session集成,并确保安全详细信息已转发到远程的进程,这一点至关重要。
14.StripPrefix GatewayFilter Factory
去除的路径中的节点数
- StripPrefix GatewayFilter Factory 包括一个
parts
参数。parts
参数指示在将请求发送到下游之前,要从请求中去除的路径中的节点数。
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: http://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2
当通过网关发出/name/bar/foo
请求时,向nameservice
发出的请求将是http://nameservice/foo
。
15.RequestSize GatewayFilter Factory
限制请求大小
- 当请求大小大于允许的限制时,RequestSize GatewayFilter Factory可以限制请求不到达下游服务。过滤器以
RequestSize
作为参数,这是定义请求的允许大小限制(以字节为单位)。
spring:
cloud:
gateway:
routes:
- id: request_size_route
uri: http://localhost:8080/upload
predicates:
- Path=/upload
filters:
- name: RequestSize
args:
maxSize: 5000000
当请求因大小而被拒绝时, RequestSize GatewayFilter Factory 将响应状态设置为413 Payload Too Large
,并带有额外的header errorMessage
。下面是一个 errorMessage
的例子。
errorMessage : Request size is larger than permissible limit. Request size is 6.0 MB where permissible limit is 5.0 MB
注意
如果未在路由定义中作为filter参数提供,则默认请求大小将设置为5 MB。