从 Hystrix 获取异常
Posted
技术标签:
【中文标题】从 Hystrix 获取异常【英文标题】:Getting exception from Hystrix 【发布时间】:2021-01-31 01:20:39 【问题描述】:我正在将 hystrix 用于 Spring Boot 项目,但出现超时异常。
详情请查看以下控制器代码
@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 次,然后出现超时异常。在检查日志的同时找到下面的日志
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]
谁能告诉我如何解决这个问题?
【问题讨论】:
【参考方案1】:您需要添加 hystrix 属性execution.isolation.semaphore.maxConcurrentRequests 并将其设置为更高的数字(200)。 execution.isolation.semaphore.maxConcurrentRequests 的默认值为“10”,因此方法 getData() 将被允许并行执行不超过 10 次。
同样,您需要添加 fallback.isolation.semaphore.maxConcurrentRequests 并将其设置为 200 作为您的回退方法。
@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 获取异常的主要内容,如果未能解决你的问题,请参考以下文章
hystrix源码小贴士之之hystrix-metrics-event-stream