簡體   English   中英

繼續調用第 3 方,直到它通過 Hystrix 返回預期的響應

[英]Keep calling 3rd party until it returns expected response with Hystrix

我正在尋找一種從我的代碼(Spring Boot 應用程序)調用 3rd 方服務的方法,如果它沒有響應,我想重復調用 x 次,然后提供默認回退。 我找到了一個示例偽代碼,它可能在我的情況下與 Hystrix 一起使用

public class ExampleClass {

    @HystrixCommand(fallbackMethod = "example_Fallback")
    public String myMethod() {

        // third party service
        String response = httpClient.execute();

        return "OK";
    }

    private String example_Fallback() {

        return "ERROR HAPPENED";
    }
}

但是,如果它返回意外的正常響應,我還想重復調用相同的第三方服務 x 次。(將該特定響應視為第三方沒有響應)。 原因是因為第三方可能無法為請求提供服務,我只能在響應中檢查。 有人能指出我正確的方向或提供一個例子,如何用 Hystrix 解決這個問題?

...我想重復調用 x 次,然后提供默認后備。

在這里配置circuitBreaker.requestVolumeThreshold可能會有所幫助。 看看其他Hystrix 屬性

@HystrixCommand(fallbackMethod = "example_Fallback", commandProperties = {
   @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"),
   @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "2000")
   }
)
public String myMethod() {
  ...
}

請注意, circuitBreaker.requestVolumeThreshold (quote) “...在滾動窗口中設置將使電路跳閘的最小請求數”。 滾動窗口持續時間 - metrics.rollingStats.timeInMilliseconds - 默認為 10 秒。

Spring 中還有@Retryable注釋。

暫無
暫無

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

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