一、 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和后边的字符串接去掉转发,stripprefix=3就代表截取路径的个数,
这样配置后当请求/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
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论