首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >resilience4j -请求超时

resilience4j -请求超时
EN

Stack Overflow用户
提问于 2020-04-18 00:18:54
回答 1查看 1.6K关注 0票数 4

我有一个使用Hystrix断路器模式的服务,它调用第三方服务。在的帮助下

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

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

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

EN

回答 1

Stack Overflow用户

发布于 2021-09-10 18:25:50

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

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

这是我的application.properties:

代码语言:javascript
复制
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

这是我的实现:

代码语言:javascript
复制
    // 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 get正常工作,我需要做的唯一一件事就是更改依赖项。

我使用的是spring-boot <version>2.5.4</version><java.version>16</java.version>和我的依赖项是:

代码语言:javascript
复制
 <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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61276120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档