springCloud Gateway StripPrefix和PrefixPath過濾器的區(qū)別及說明
一、 StripPrefix Filter
StripPrefix Filter 是一個(gè)請(qǐng)求路徑截取的功能。
server:
port: 8080
spring:
application:
name: user
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: user
uri: lb://user
#uri: http://localhost:8080
predicates:
- Path=/zuul/api/user/**
filters:
- StripPrefix=3
主要看這里
- Path=/zuul/api/user/** filters: - StripPrefix=3
當(dāng)請(qǐng)求路徑匹配到/zuul/api/user/**會(huì)將包含zuul和后邊的字符串接去掉轉(zhuǎn)發(fā),StripPrefix=3就代表截取路徑的個(gè)數(shù),
這樣配置后當(dāng)請(qǐng)求/zuul/api/user/aaa后端匹配到的請(qǐng)求路徑,就會(huì)變成http://localhost:8080/aaa
二、PrefixPath Filter
PrefixPath Filter 的作用和 StripPrefix 正相反,是在 URL 路徑前面添加一部分的前綴。
server:
port: 8080
spring:
application:
name: user
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: user
uri: lb://user
predicates:
- Path=/**
filters:
- PrefixPath=/hi主要看這里
predicates:
- Path=/**
filters:
# 當(dāng)訪問 http://localhost:8080/aaa,加上前綴就變成 http://localhost:8080/hi/aaa
- PrefixPath=/hi當(dāng)訪問 http://localhost:8080/aaa,加上前綴就變成 http://localhost:8080/hi/aaa
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

