【问题标题】:Spring Cloud Gateway Fallback on 404 Status Code from routeSpring Cloud Gateway Fallback on 404 Status Code from route
【发布时间】:2020-02-01 09:38:17
【问题描述】:

我想在任何路由返回 404 HTTP 代码时进行回退。原因是如果找不到资源,路由应该尝试从后备 Uri 中获取该资源。

以下是我定义的 .yml。我想要实现的是,如果弹性搜索没有找到我的记录并返回 404。在后备中搜索,即数据库。但在这种情况下,我从网关得到 404。有什么办法可以做到吗?

spring:
  application:
    name: gateway

  sleuth:
    sampler:
      probability: 1.0

  cloud:
    gateway:
      discovery:
        locator:
          enabled: true

        - id: search_route
          uri: lb://ELASTIC_SEARCH_SERVICE
          predicates:
            - Path=/unison/search/**
          filters:
            - name: Hystrix
              args:
                name: fallbackCommand
                fallbackUri: forward:/search/rdbms

【问题讨论】:

    标签: spring-boot spring-cloud-gateway


    【解决方案1】:

    你必须通过这个让网关知道尤里卡服务器

    eureka.client.service-url.defaultZone=http://user:pass@localhost:8761/eureka

    然后阻止网关自己注册到eureka

    eureka.client.register-with-eureka=false

    第二个选项很重要,因为它会导致使用 lb:servicename 结构进行负载平衡时出现 404 错误。

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 2021-07-23
      • 1970-01-01
      • 2017-08-22
      • 2019-06-10
      • 2021-10-20
      相关资源
      最近更新 更多