spring cloud gateway配置断言(predicates)报404

话不多说直接上原配置

server:
  port: 18083

spring:
  application:
    name: nacos-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: nacos-route
          uri: lb://gateway-provider
          predicates:
            - Path=/nacos/**

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

做完上述配置之后启动gateway和provider,访问http://127.0.0.1:18083/nacos/echo/hello-world,结果报404,也没有日志。经过仔细比对spring-cloud-alibaba工程里的example发现少了一个filters的配置,于是就把filters加了上去,如下

server:
  port: 18083

spring:
  application:
    name: nacos-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        username: nacos
        password: nacos
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: nacos-route
          uri: lb://gateway-provider
          predicates:
            - Path=/nacos/**
          filters:
            - StripPrefix=1

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

然后再次访问http://127.0.0.1:18083/nacos/echo/hello-world他就通了。

大概百度了一下,说这个StripPrefix其功能是:将请求发送到下游之前要从请求中剥离部分路径。StripPrefile=1就是把uri的第一节去掉,也就是把请求时的/nacos/echo/hello-world变成/echo/hello-world。举一反三,StripPrefile=2就是把uri的前2节去掉。以此类推。

奇怪的是百度了一些配置好像都没加这个配置啊,他们的怎么就不报404。有大神能解释一下不。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值