Spring Cloud Gateway中沿着断路器实现重试机制

发布于 2025-01-09 22:26:54 字数 1028 浏览 6 评论 0原文

我想在 Spring Cloud Gateway 中实现重试机制和断路器。目前,当我将断路器过滤器单独添加到 Spring Cloud Gateway 时,它工作正常。如果我单独添加重试过滤器,它也可以正常工作。但我希望在电路更改为打开状态之前自动重试该请求至少 3 次。有什么方法可以集成重试和断路器过滤器并使其工作。

请求的流程将是: 用户请求-> Spring Cloud网关->微服务

示例 Spring Cloud 网关配置将类似于:

application.yml

spring:
   cloud:
    gateway:
      routes:
        - id: msOne
          uri: http://localhost:8081/
          predicates:
            - Path=/ms-one-services/**
          filters:
            - RewritePath=/ms-one-services(?<segment>/?.*), $\{segment}
            - name: CircuitBreaker
              args:
                name: msOneServices
                fallbackUri: forward:/ms-one-services-fallback
            - name: Retry
              args:
                name: msOneServices
                retries: 3
                statuses: BAD_GATEWAY
                backoff:
                  firstBackOff: 5s
                  maxBackOff: 20s
                  factor: 2
                  basedOnPreviousValue: false

I want to implement retry mechanism along with circuit breaker in spring cloud gateway. Currently when I add Circuit Breaker filter alone to the spring cloud gateway, it works fine. And if I add Retry filter alone, it also works fine. But I want the request to be retried atleast 3 times automatically before the circuit changes to open state. Is there any way that I can integrate both retry and circuit breaker filter and make it work.

The flow of request will be:
User Request -> Spring Cloud Gateway -> Microservice

Sample spring cloud gateway configuration will be like:

application.yml

spring:
   cloud:
    gateway:
      routes:
        - id: msOne
          uri: http://localhost:8081/
          predicates:
            - Path=/ms-one-services/**
          filters:
            - RewritePath=/ms-one-services(?<segment>/?.*), $\{segment}
            - name: CircuitBreaker
              args:
                name: msOneServices
                fallbackUri: forward:/ms-one-services-fallback
            - name: Retry
              args:
                name: msOneServices
                retries: 3
                statuses: BAD_GATEWAY
                backoff:
                  firstBackOff: 5s
                  maxBackOff: 20s
                  factor: 2
                  basedOnPreviousValue: false

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文