gpt4 book ai didi

java - 弹性4j - 请求超时

转载 作者:行者123 更新时间:2023-12-04 13:36:52 25 4
gpt4 key购买 nike

我有一个使用 Hystrix 断路器模式的服务,它调用 3rd 方服务。在...的帮助下
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")我已经定义了 3rd 方服务的超时时间。

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

我知道使用 @TimeLimiter 可以实现类似的功能这是resilience4j-timelimiter的一部分。但根据这个问题:https://github.com/resilience4j/resilience4j/issues/849 ,我得把我方法的返回类型修改为CompletableFuture .它将涉及对我现有服务的大量代码更改。我怎样才能用resilience4j 实现这一点?

最佳答案

我不得不处理和你一样的问题。
对我来说,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 <version>2.5.4</version> , <java.version>16</java.version>我的依赖项是:
 <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 - 弹性4j - 请求超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61276120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com