天天看点

Spring Cloud Gateway 路由路径重写举例

举例

访问路径 http://internal-k8s.wyz.com/api/wyz/

变更为 http://internal-k8s.wyz.com/wyz/record/

有如下两种方法进行配置

生产环境用的方法一,方法二没有验证,如有问题感谢告知

方法一:

注意缩进

spring:
  cloud:
    gateway:
      routes:
        - id: wyz-route
          uri: http://internal-k8s.wyz.com
          predicates:
            - Path=/api/wyz/**
          filters:
            - RewritePath=/api/wyz/(?<segment>.*), /wyz/recode/$\{segment}
           

/api/wyz/是需要被替换

/wyz/recode/是替换后的URL

方法二

注意缩进

spring:
  cloud:
    gateway:
      routes:
        - id: wyz-route
          uri: http://internal-k8s.wyz.com
          predicates:
            - Path=/api/wyz/**
          filters:
            - StripPrefix=2
            - PrefixPath=/wyz/recode