本文介绍了从 Hystrix 获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring Boot 项目中使用 hystrix,但出现超时异常.

I am using hystrix for spring boot project but getting timeout exception.

详情请查看以下控制器代码

Please find below controller code for details

@GetMapping("/getData")
@HystrixCommand(fallbackMethod = "getDataFallBack", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "60000"), @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE") })
public ResponseEntity<Object> getData() {

}

/**
 * Fallback method for getData
 */
public ResponseEntity<Object> getDataFallBack(Throwable e) {
    LOGGER.info("In fallback method", e)
}

在测试更多调用时,例如 5 分钟内点击 1500 次,然后出现超时异常.在检查日志时找到以下日志

While testing with more calls like 1500 hits in 5 min then getting timeout exception. While checking logs then find below logs

java.lang.RuntimeException: could not acquire a semaphore for execution
    at com.netflix.hystrix.AbstractCommand.handleSemaphoreRejectionViaFallback(AbstractCommand.java:966) ~[hystrix-core-1.5.18.jar!/:1.5.18]
    at com.netflix.hystrix.AbstractCommand.applyHystrixSemantics(AbstractCommand.java:554) ~[hystrix-core-1.5.18.jar!/:1.5.18]
    at com.netflix.hystrix.AbstractCommand.access$200(AbstractCommand.java:60) ~[hystrix-core-1.5.18.jar!/:1.5.18]
    at com.netflix.hystrix.AbstractCommand$4.call(AbstractCommand.java:419) ~[hystrix-core-1.5.18.jar!/:1.5.18]
    at com.netflix.hystrix.AbstractCommand$4.call(AbstractCommand.java:413) ~[hystrix-core-1.5.18.jar!/:1.5.18]
    at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) [rxjava-1.3.8.jar!/:1.3.8]
    at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) [rxjava-1.3.8.jar!/:1.3.8]
    at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar!/:1.3.8]
    at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:48) [rxjava-1.3.8.jar!/:1.3.8]
    at rx.internal.operators.OnSubscribeMap.call(OnSubscribeMap.java:33) [rxjava-1.3.8.jar!/:1.3.8]
    at rx.Observable.unsafeSubscribe(Observable.java:10327) [rxjava-1.3.8.jar!/:1.3.8]
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) [rxjava-1.3.8.jar!/:1.3.8]
    at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) [rxjava-1.3.8.jar!/:1.3.8]
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) [rxjava-1.3.8.jar!/:1.3.8]
    at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) [rxjava-1.3.8.jar!/:1.3.8]

有人可以告诉我如何解决这个问题吗?

Can someone please let me know how to solve this issue?

推荐答案

您需要添加 hystrix 属性 execution.isolation.semaphore.maxConcurrentRequests 并将其设置为更高的数字 (200).execution.isolation.semaphore.maxConcurrentRequests 的默认值为10";因此 getData() 方法将被允许并行执行不超过 10 次.

You need to add hystrix property execution.isolation.semaphore.maxConcurrentRequests and set it to a higher number (200).The default value of execution.isolation.semaphore.maxConcurrentRequests is "10" so the method getData() will be allowed to be executed in parallel for not more than 10 times.

同样,您需要添加 fallback.isolation.semaphore.maxConcurrentRequests 并将其设置为 200 以用于您的回退方法.

Similarly you need to add fallback.isolation.semaphore.maxConcurrentRequests and set it to 200 for your fall back method.

    @GetMapping("/getData")
    @HystrixCommand(fallbackMethod = "getDataFallBack", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "60000"),
    @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),@HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "200") })
    public ResponseEntity<Object> getData() {

    }

这篇关于从 Hystrix 获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 11:02