【问题标题】:resilience4j - Request timeout弹性4j - 请求超时
【发布时间】:2020-07-31 05:17:00
【问题描述】:

我有一个使用 Hystrix 断路器模式的服务,它调用 3rd 方服务。在

的帮助下

@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") 我已经为第 3 方服务定义了超时。

由于 Hystrix 处于维护模式,我正在从 Hystrix 迁移到 Resilience4j 断路器模式。如何在 resiience4j 中实现类似的超时处理。

我知道可以通过使用@TimeLimiter 来实现类似的事情,它是resilience4j-timelimiter 的一部分。但是根据这个问题:https://github.com/resilience4j/resilience4j/issues/849,我必须将我的方法的返回类型修改为CompletableFuture。它将涉及对我现有服务的大量代码更改。我如何使用 Resilience4j 实现这一目标?

【问题讨论】:

  • 看看这个 - vinsguru.com/…
  • 我检查了那里的示例代码。它使用 RestTemplateBuilder 在 RestTemplate 中设置超时。我正在通过resilience4j 寻找属性。有可能吗?

标签: java spring-boot circuit-breaker resilience4j


【解决方案1】:

我不得不和你处理同样的问题。

对我来说,resilience4j 的 TimeLimiter 解决了我的问题,不再需要使用 Hystrix。

这是我的 application.properties:

resilience4j.timelimiter.configs.default.timeout-duration=3s
resilience4j.timelimiter.instances.paymentCalc.base-config=default
# The max amount of time a call can last
resilience4j.timelimiter.instances.paymentCalc.timeout-duration=1s
# Cancel the Running Completable Futures After TimeOut.
resilience4j.timelimiter.instances.paymentCalc.cancel-running-future=true

# Max amount of parallel executions allowed by the bulkhead
resilience4j.bulkhead.configs.default.max-concurrent-calls=2
# Max amount of time a thread should be blocked for when attempting to enter a saturated bulkhead.
resilience4j.bulkhead.configs.default.max-wait-duration=0
resilience4j.bulkhead.instances.paymentCalc.base-config=default

这是我的实现:

    // Bulkhead module is added because TimeLimiter needs separate execution thread instead of request thread
    @Bulkhead(name = "paymentCalc", fallbackMethod = "localPaymentGenerate", type = Bulkhead.Type.THREADPOOL)
    @TimeLimiter(name = "paymentCalc", fallbackMethod = "localPaymentGenerate")
    @GetMapping(value = "/{workerId}/days/{days}")
    public CompletableFuture<ResponseEntity<Payment>> getPayment(@PathVariable Long workerId, @PathVariable Integer days){
            
        ...     
    }
    
    
    public CompletableFuture<ResponseEntity<Payment>> localPaymentGenerate(Long workerId, Integer days, Exception e){
        
        System.out.println(e.getMessage()); // prints "TimeLimiter 'paymentCalc' recorded a timeout exception"
    
        ...
}

但是,请注意,使用 spring-cloud-starter-circuitbreaker-resilience4j 依赖项的代码不起作用,我花了很多时间试图解决它。 要使@TimeLimiter 正常工作,我唯一需要更改的就是更改我的依赖项。

我正在使用 spring-boot &lt;version&gt;2.5.4&lt;/version&gt;&lt;java.version&gt;16&lt;/java.version&gt;,我的依赖项是:

 <dependency>
        <groupId>io.github.resilience4j</groupId>
        <artifactId>resilience4j-spring-boot2</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

【讨论】:

  • 看起来不错。我需要试试你的建议。知道这是否适用于 Java 11?目前,我只能使用最高 11 的 Java 版本。不过我可以尝试一下。
  • 嗨,我刚刚将我的微服务项目更改为 Java 版本 11,并且该服务继续按预期工作,我认为它也适合您。尝试在上面进行更改以使用 TimeLimiter 并让我知道它是否有效。
  • 太棒了!感谢您的确认。我会试一试的。
猜你喜欢
  • 2019-07-12
  • 2020-11-03
  • 2021-03-07
  • 1970-01-01
  • 2011-02-26
  • 2021-04-17
  • 2016-04-26
  • 2020-11-20
  • 2019-10-23
相关资源
最近更新 更多