spring cloud gateway系列教程目錄
- spring cloud gateway系列教程1—Route Predicate
- spring cloud gateway系列教程2——GatewayFilter_上篇
- spring cloud gateway系列教程2——GatewayFilter_下篇
- spring cloud gateway系列教程3—Global Filters
- spring cloud gateway系列教程4—其他配置
GatewayFilter Factories
Route filters可以通過一些方式修改HTTP請求的輸入和輸出,針對某些特殊的場景,Spring Cloud Gateway已經(jīng)內置了很多不同功能的GatewayFilter Factories驴娃。
下面就來通過例子逐一講解這些GatewayFilter Factories。
1. AddRequestHeader GatewayFilter Factory
AddRequestHeader GatewayFilter Factory通過配置name和value可以增加請求的header劲件。
application.yml:
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: http://www.google.com
filters:
- AddRequestHeader=X-Request-Foo, Bar
對匹配的請求,會額外添加X-Request-Foo:Bar
的header约急。
2. AddRequestParameter GatewayFilter Factory
AddRequestParameter GatewayFilter Factory通過配置name和value可以增加請求的參數(shù)零远。
application.yml:
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: http://www.google.com
filters:
- AddRequestParameter=foo, bar
對匹配的請求,會額外添加foo=bar
的請求參數(shù)厌蔽。
3. AddResponseHeader GatewayFilter Factory
AddResponseHeader GatewayFilter Factory通過配置name和value可以增加響應的header牵辣。
application.yml:
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: http://www.google.com
filters:
- AddResponseHeader=X-Response-Foo, Bar
對匹配的請求,響應返回時會額外添加X-Response-Foo:Bar
的header返回奴饮。
4. Hystrix GatewayFilter Factory
Hystrix是Netflix實現(xiàn)的斷路器模式工具包纬向,The Hystrix GatewayFilter就是將斷路器使用在gateway的路由上择浊,目的是保護你的服務避免級聯(lián)故障,以及在下游失敗時可以降級返回逾条。
項目里面引入spring-cloud-starter-netflix-hystrix
依賴琢岩,并提供HystrixCommand
的名字,即可生效Hystrix GatewayFilter师脂。
application.yml:
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: http://www.google.com
filters:
- Hystrix=myCommandName
那么剩下的過濾器担孔,就會包裝在名為myCommandName
的HystrixCommand中運行。
Hystrix過濾器也是通過配置可以參數(shù)fallbackUri
危彩,來支持路由熔斷后的降級處理攒磨,降級后,請求會跳過fallbackUri
配置的路徑汤徽,目前只支持forward:
的URI協(xié)議。
application.yml:
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: lb://backing-service:8088
predicates:
- Path=/consumingserviceendpoint
filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/incaseoffailureusethis
當Hystrix降級后就會將請求轉發(fā)到/incaseoffailureusethis
灸撰。
整個流程其實是用fallbackUri
將請求跳轉到gateway內部的controller或者handler谒府,然而也可以通過以下的方式將請求轉發(fā)到外部的服務:
application.yml:
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降級后就會將請求轉發(fā)到http://localhost:9994
浮毯。
Hystrix Gateway filter在轉發(fā)降級請求時完疫,會將造成降級的異常設置在ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR
屬性中,在處理降級時也可以用到债蓝。
比如下一節(jié)講到的FallbackHeaders GatewayFilter Factory壳鹤,就會通過上面的方式拿到異常信息,設置到降級轉發(fā)請求的header上饰迹,來告知降級下游異常信息芳誓。
通過下面配置可以設置Hystrix的全局超時信息:
application.yml:
hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 5000
5. FallbackHeaders GatewayFilter Factory
FallbackHeaders GatewayFilter Factory可以將Hystrix執(zhí)行的異常信息添加到外部請求的fallbackUri
header上。
application.yml:
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
在這個例子中啊鸭,當請求lb://ingredients
降級后锹淌,FallbackHeaders
filter會將HystrixCommand
的異常信息,通過Test-Header
帶給http://localhost:9994
服務赠制。
你也可以使用默認的header赂摆,也可以像上面一下配置修改header的名字:
-
executionExceptionTypeHeaderName
("Execution-Exception-Type"
) -
executionExceptionMessageHeaderName
("Execution-Exception-Message"
) -
rootCauseExceptionTypeHeaderName
("Root-Cause-Exception-Type"
) -
rootCauseExceptionMessageHeaderName
("Root-Cause-Exception-Message"
)
6. PrefixPath GatewayFilter Factory
The PrefixPath GatewayFilter Factor通過設置prefix
參數(shù)來路徑前綴。
application.yml:
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: http://www.google.com
filters:
- PrefixPath=/mypath
如果一個請求是/hello
钟些,通過上面路由烟号,就會將請求修改為/mypath/hello
。
7. PreserveHostHeader GatewayFilter Factory
PreserveHostHeader GatewayFilter Factory會保留原始請求的host
頭信息政恍,并原封不動的轉發(fā)出去汪拥,而不是被gateway的http客戶端重置。
application.yml:
spring:
cloud:
gateway:
routes:
- id: preserve_host_route
uri: http://www.google.com
filters:
- PreserveHostHeader
由于GatewayFilter Factory比較多抚垃,分開兩篇來寫喷楣,下一篇
如果想查看其他spring cloud gateway的案例和使用趟大,可以點擊查看