簡(jiǎn)述
spring cloud gateway 路由過濾器修改傳入的HTTP請(qǐng)求或傳出的HTTP響應(yīng)
spring cloud gateway通過不同的過濾器集成其他spring cloud組件
-
過濾器的種類
- GatewayFilter Factories: 過濾器工廠生成的網(wǎng)關(guān)過濾器
- Global Filters: 全局過濾器
網(wǎng)關(guān)過濾器
StripPrefix 過濾器
- 作用: 去掉部分URL路徑
- 配置示例(入門教程):
spring:
cloud:
gateway:
routes:
# 集成eureka注冊(cè)中心的配置示例
- id: hello_ribbon_route
uri: lb://spring-cloud-producer
predicates:
- Path=/producerInEureka/**
filters:
- StripPrefix=1
- 如上,我們?cè)L問網(wǎng)關(guān)地址
http://host:port/producerInEureka/hello時(shí)- 若無
StripPrefix過濾器時(shí),gateway 發(fā)送請(qǐng)求到后臺(tái)服務(wù)spring-cloud-producer的url就是http://spring-cloud-producer/producerInEureka/hello - 若有
StripPrefix過濾器時(shí),gateway會(huì)根據(jù)StripPrefix=1所配的值(這里是1)去掉URL路徑中的部分前綴(這里去掉一個(gè)前綴,即去掉producerInEureka)- 發(fā)送請(qǐng)求到后臺(tái)服務(wù)
spring-cloud-producer的url變成http://spring-cloud-producer/hello
- 發(fā)送請(qǐng)求到后臺(tái)服務(wù)
- 若無
PrefixPath 過濾器
- 作用: 它的作用和
StripPrefix正相反,是在URL路徑前面添加一部分的前綴
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: http://example.org
filters:
- PrefixPath=/mypath
- 這將會(huì)把
/mypath添加到路由prefixpath_route匹配到的所有請(qǐng)求的路徑的前面。- 所以對(duì)
/hello的請(qǐng)求將會(huì)被發(fā)送到/mypath/hello。
- 所以對(duì)
Hystrix 過濾器
作用:Hystrix 過濾器允許您將斷路器引入網(wǎng)關(guān)路由,保護(hù)您的服務(wù)免受級(jí)聯(lián)故障的影響,并允許您在下游故障時(shí)提供回退響應(yīng)。
配置示例
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: http://example.org
filters:
- Hystrix=myCommandName
- 如上配置后,gateway將使用
myCommandName作為名稱生成HystrixCommand對(duì)象來進(jìn)行熔斷管理。 - 上面只是 Hystrix 過濾器的簡(jiǎn)單配置方式,若要配置一個(gè)失敗回調(diào)則如下
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
- 其中
fallbackUri: forward:/incaseoffailureusethis配置了fallback時(shí)要會(huì)調(diào)的路徑- 當(dāng)調(diào)用Hystrix的fallback被調(diào)用時(shí),請(qǐng)求將轉(zhuǎn)發(fā)到
/incaseoffailureuset這個(gè)URI。
- 當(dāng)調(diào)用Hystrix的fallback被調(diào)用時(shí),請(qǐng)求將轉(zhuǎn)發(fā)到
其他過濾器請(qǐng)看官方文檔:http://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.0.0.RELEASE/single/spring-cloud-gateway.html#_gatewayfilter_factories
全局過濾器
全局過濾器的配置方式不同于網(wǎng)關(guān)過濾器;且雖然其作用范圍是所有路由配置,但都有各自的啟用條件
LoadBalancerClient負(fù)載均衡過濾器(整合eureka注冊(cè)中心)
- 作用:集成ribbon和eureka
- 配置示例(入門教程):
spring:
cloud:
gateway:
routes:
# 集成eureka注冊(cè)中心的配置示例
- id: hello_ribbon_route
uri: lb://spring-cloud-producer
predicates:
- Path=/producerInEureka/**
filters:
- StripPrefix=1
- 當(dāng)路由配置中
uri所用的協(xié)議為lb時(shí)(以uri: lb://spring-cloud-producer為例),gateway將使用 LoadBalancerClient把spring-cloud-producer通過eureka解析為實(shí)際的主機(jī)和端口,并進(jìn)行負(fù)載均衡。
Netty Routing Filter
- 當(dāng)路由配置中
uri所用的協(xié)議為http或者https時(shí),netty 路由過濾器就會(huì)啟用 - 作用:使用Netty的
HttpClient轉(zhuǎn)發(fā)請(qǐng)求給網(wǎng)關(guān)后面的服務(wù)。
Websocket Routing Filter
- 作用: 使用
Spring Web Socket的基礎(chǔ)架構(gòu)去轉(zhuǎn)發(fā)Websocket請(qǐng)求 - 示例配置如下
spring:
cloud:
gateway:
routes:
# 一般的 Websocket 路由
- id: websocket_route
uri: ws://localhost:3001
predicates:
- Path=/websocket/**
# SockJS 路由
- id: websocket_sockjs_route
uri: http://localhost:3001
predicates:
- Path=/websocket/info/**
- 當(dāng)路由配置中
uri所用的協(xié)議為ws或者wss時(shí),Websocket 路由過濾器就會(huì)啟用 - 可以通過這樣的方式
lb:ws://serviceid,以在使用websocket路由過濾器的時(shí)候同時(shí)使用負(fù)載均衡過濾器