从Hystrix获取异常

huangapple go评论76阅读模式
英文:

Getting exception from Hystrix

问题

我正在使用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() {

}

/**
 * getData的降级方法
 */
public ResponseEntity<Object> getDataFallBack(Throwable e) {
    LOGGER.info("在降级方法中", e);
}

在进行更多调用的测试,例如在5分钟内进行1500次调用时,会出现超时异常。在检查日志时,发现以下日志:

java.lang.RuntimeException: 无法获取执行信号量
	at com.netflix.hystrix.AbstractCommand.handleSemaphoreRejectionViaFallback(AbstractCommand.java:966) ~[hystrix-core-1.5.18.jar!/:1.5.18]
	...

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

英文:

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

Please find below controller code for details

@GetMapping(&quot;/getData&quot;)
@HystrixCommand(fallbackMethod = &quot;getDataFallBack&quot;, commandProperties = { @HystrixProperty(name = &quot;execution.isolation.thread.timeoutInMilliseconds&quot;, value = &quot;60000&quot;), @HystrixProperty(name = &quot;execution.isolation.strategy&quot;, value = &quot;SEMAPHORE&quot;) })
public ResponseEntity&lt;Object&gt; getData() {

}

/**
 * Fallback method for getData
 */
public ResponseEntity&lt;Object&gt; getDataFallBack(Throwable e) {
	LOGGER.info(&quot;In fallback method&quot;, e)
}

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?

答案1

得分: 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() {

}
英文:

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.

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

    @GetMapping(&quot;/getData&quot;)
    @HystrixCommand(fallbackMethod = &quot;getDataFallBack&quot;, commandProperties = { @HystrixProperty(name = &quot;execution.isolation.thread.timeoutInMilliseconds&quot;, value = &quot;60000&quot;), 
    @HystrixProperty(name = &quot;execution.isolation.strategy&quot;, value = &quot;SEMAPHORE&quot;),@HystrixProperty(name = &quot;execution.isolation.semaphore.maxConcurrentRequests&quot;, value = &quot;200&quot;) })
    public ResponseEntity&lt;Object&gt; getData() {
    
    }

huangapple
  • 本文由 发表于 2020年10月16日 21:49:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64390516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定