目錄安排上
- Gateway路由斷言器
- 1.After 路由斷言 Factory
- 2.Before 路由斷言 Factory
- 3.Between 路由斷言 Factory
- 4.Cookie 路由斷言 Factory
- 5.Header 路由斷言 Factory
- 6.Host 路由斷言 Factory
- 7.Method 路由斷言 Factory
- 8.Path 路由斷言 Factory
- 9.Query 路由斷言 Factory
- 10.RemoteAddr 路由斷言 Factory
- 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。