java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springCloud Gateway StripPrefix和PrefixPath过滤器区别

springCloud Gateway StripPrefix和PrefixPath过滤器的区别及说明

作者:不会吉他的肌肉男不是好的挨踢男

这篇文章主要介绍了springCloud Gateway StripPrefix和PrefixPath过滤器的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

一、 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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

阅读全文