过滤器 SetPath · SpringCloud微服务实战 · 看云

11. 过滤器 SetPath

导航

SetPath``GatewayFilter工厂采用的路径template参数。通过允许路径的模板段,它提供了一种操作请求路径的简单方法。这使用了Spring Framework中的URI模板。允许多个匹配段

本节代码地址


11.1 应用配置

我们在配置中设置Path的路径和SetPath的路径,实际在转发的时候会走SetPath的路径,这里与RewritePath的区别主要是SetPath中的路径参数就是Path中的路径,在某些场景上是也是可以启动同样的效果。

server:
  port: 8699
spring:
  application:
    name: fw-gateways-gateway
  profiles:
    active: setpath_route
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
---
spring:
  cloud:
    gateway:
      routes:
        - id: setpath_route
          uri: lb://fw-cloud-ribbon-server
          predicates:
            - Path=/{segment}
          filters:
            - SetPath=/user/{segment}
  profiles: setpath_route

11.2 应用启动

可以看到localhost:8699/1实际走的是localhost:8699/user/1

5802bdea8b2bb4d6f38b0c7efaff38a9_MD5.webp