springCloud Gateway StripPrefix和PrefixPath過濾器的區(qū)別及說明
更新時間:2025年06月23日 10:23:14 作者:不會吉他的肌肉男不是好的挨踢男
這篇文章主要介紹了springCloud Gateway StripPrefix和PrefixPath過濾器的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
一、 StripPrefix Filter
StripPrefix Filter 是一個請求路徑截取的功能。
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
當請求路徑匹配到/zuul/api/user/**會將包含zuul和后邊的字符串接去掉轉發(fā),StripPrefix=3就代表截取路徑的個數(shù),
這樣配置后當請求/zuul/api/user/aaa后端匹配到的請求路徑,就會變成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:
# 當訪問 http://localhost:8080/aaa,加上前綴就變成 http://localhost:8080/hi/aaa
- PrefixPath=/hi當訪問 http://localhost:8080/aaa,加上前綴就變成 http://localhost:8080/hi/aaa
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

