Spring cloud gateway 路由后台匹配回前台出现404情况

简述

  • spring cloud gateway 路由过滤器修改传入的HTTP请求或传出的HTTP响应

  • spring cloud gateway通过不同的过滤器集成其他spring cloud组件

  • 过滤器的种类

    • GatewayFilter Factories: 过滤器工厂生成的网关过滤器
    • Global Filters: 全局过滤器

网关过滤器

StripPrefix 过滤器
  • 作用: 去掉部分URL路径
  • 配置示例(入门教程):
spring:
  cloud:
    gateway:
      routes:
        # 集成eureka注册中心的配置示例
      - id: hello_ribbon_route
        uri: lb://spring-cloud-producer
        predicates:
        - Path=/producerInEureka/**
        filters:
        - StripPrefix=1
  • 如上,我们访问网关地址http://host:port/producerInEureka/hello
    • 若无StripPrefix过滤器时,gateway 发送请求到后台服务spring-cloud-producer的url就是http://spring-cloud-producer/producerInEureka/hello
    • 若有StripPrefix过滤器时,gateway会根据StripPrefix=1所配的值(这里是1)去掉URL路径中的部分前缀(这里去掉一个前缀,即去掉producerInEureka
      • 发送请求到后台服务spring-cloud-producer的url变成http://spring-cloud-producer/hello
PrefixPath 过滤器
  • 作用: 它的作用和StripPrefix正相反,是在URL路径前面添加一部分的前缀
spring:
  cloud:
    gateway:
      routes:
      - id: prefixpath_route
        uri: http://example.org
        filters:
        - PrefixPath=/mypath
  • 这将会把/mypath添加到路由prefixpath_route匹配到的所有请求的路径的前面。
    • 所以对/hello的请求将会被发送到/mypath/hello
Hystrix 过滤器
  • 作用:Hystrix 过滤器允许您将断路器引入网关路由,保护您的服务免受级联故障的影响,并允许您在下游故障时提供回退响应。

  • 配置示例

spring:
  cloud:
    gateway:
      routes:
      - id: hystrix_route
        uri: http://example.org
        filters:
        - Hystrix=myCommandName
  • 如上配置后,gateway将使用myCommandName作为名称生成HystrixCommand对象来进行熔断管理。
  • 上面只是 Hystrix 过滤器的简单配置方式,若要配置一个失败回调则如下
spring:
  cloud:
    gateway:
      routes:
      - id: hystrix_route
        uri: lb://backing-service:8088
        predicates:
        - Path=/consumingserviceendpoint
        filters:
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/incaseoffailureusethis
        - RewritePath=/consumingserviceendpoint, /backingserviceendpoint
  • 其中fallbackUri: forward:/incaseoffailureusethis配置了fallback时要会调的路径
    • 当调用Hystrix的fallback被调用时,请求将转发到/incaseoffailureuset这个URI。
其他过滤器请看官方文档:http://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.0.0.RELEASE/single/spring-cloud-gateway.html#_gatewayfilter_factories

全局过滤器

全局过滤器的配置方式不同于网关过滤器;且虽然其作用范围是所有路由配置,但都有各自的启用条件

LoadBalancerClient负载均衡过滤器(整合eureka注册中心)
  • 作用:集成ribbon和eureka
  • 配置示例(入门教程):
spring:
  cloud:
    gateway:
      routes:
        # 集成eureka注册中心的配置示例
      - id: hello_ribbon_route
        uri: lb://spring-cloud-producer
        predicates:
        - Path=/producerInEureka/**
        filters:
        - StripPrefix=1
  • 当路由配置中uri所用的协议为lb时(以uri: lb://spring-cloud-producer为例),gateway将使用 LoadBalancerClient把spring-cloud-producer通过eureka解析为实际的主机和端口,并进行负载均衡。
Netty Routing Filter
  • 当路由配置中uri所用的协议为http或者https时,netty 路由过滤器就会启用
  • 作用:使用Netty的HttpClient转发请求给网关后面的服务。
Websocket Routing Filter
  • 作用: 使用Spring Web Socket的基础架构去转发Websocket请求
  • 示例配置如下
spring:
  cloud:
    gateway:
      routes:
      # 一般的 Websocket 路由
      - id: websocket_route
        uri: ws://localhost:3001
        predicates:
        - Path=/websocket/**
      # SockJS 路由
      - id: websocket_sockjs_route
        uri: http://localhost:3001
        predicates:
        - Path=/websocket/info/**
  • 当路由配置中uri所用的协议为ws或者wss时,Websocket 路由过滤器就会启用
  • 可以通过这样的方式lb:ws://serviceid,以在使用websocket路由过滤器的时候同时使用负载均衡过滤器
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值