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

2. 过滤器 StripPrefix

导航

?StripPrefix GatewayFilter工厂采用一个参数:part, 这个参数在请求转发到下游之前去除路径的前part部分

本节代码地址


2.1 应用配置

新增的StripPrefix可以接受一个非负整数,对应的具体实现是StripPrefixGatewayFilterFactory,从名字就可以看出它的作用是去掉前缀的,那个整数即对应层数。

server:
  port: 8699
spring:
  application:
    name: fw-gateways-gateway
  profiles:
    active: strippath_route
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
---
spring:
  cloud:
    gateway:
      routes:
        - id: strippath_route
          uri: lb://fw-cloud-ribbon-server
          predicates:
            - Path=/gateway

2.2 应用启动

我们通过 Spring Cloud Gateway 访问/geatway/user/2,那么当网关服务向后转发请求时,会去掉/user/2
dc8bd34c0d19dc81d2b090f072465a13_MD5.webp