簡體   English   中英

彈性4j - 請求超時

[英]resilience4j - Request timeout

我有一個使用 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 實現這一目標?

我不得不處理和你一樣的問題。

對我來說,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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM