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

9. 过滤器 RewritePath

导航

RewritePath``GatewayFilter工厂采用的路径regexp参数和replacement参数。这使用Java正则表达式提供了一种灵活的方式来重写请求路径。

本节代码地址


9.1 应用配置

这里我们配置Rewrite的规则,配置两个匹配地址,前面一个是http请求的,后面一个是被转发的。

server:
  port: 8699
spring:
  application:
    name: fw-gateways-gateway
  profiles:
    active: rewritepath_route
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
---
spring:
  cloud:
    gateway:
      routes:
        - id: rewritepath_route
          uri: lb://fw-cloud-ribbon-server
          predicates:
            - After=2020-01-08T18:30:11.965+08:00[Asia/Shanghai]
          filters:
            - RewritePath=/gateway/(?<segment>.*), /$\{segment}
  profiles: rewritepath_route

9.2 应用启动

浏览器或Postman输入http://localhost:8699/gateway/user/1
路径http://localhost:8699/gateway/user/1会被映射到http://localhost:8699/user/1
affb6b2f862d8306af37e8d3d216b600_MD5.webp