记一次Hystrix报错HystrixRuntimeException: HystrixCommandKey fallback execution rejected.

痛定思痛。 2022-12-22 11:27 457阅读 0赞

定位代码
在这里插入图片描述

/com/netflix/hystrix/AbstractCommand.java:851

  1. if (fallbackSemaphore.tryAcquire()) {
  2. try {
  3. if (isFallbackUserDefined()) {
  4. executionHook.onFallbackStart(this);
  5. fallbackExecutionChain = getFallbackObservable();
  6. } else {
  7. //same logic as above without the hook invocation
  8. fallbackExecutionChain = getFallbackObservable();
  9. }
  10. } catch (Throwable ex) {
  11. //If hook or user-fallback throws, then use that as the result of the fallback lookup
  12. fallbackExecutionChain = Observable.error(ex);
  13. }
  14. return fallbackExecutionChain
  15. .doOnEach(setRequestContext)
  16. .lift(new FallbackHookApplication(_cmd))
  17. .lift(new DeprecatedOnFallbackHookApplication(_cmd))
  18. .doOnNext(markFallbackEmit)
  19. .doOnCompleted(markFallbackCompleted)
  20. .onErrorResumeNext(handleFallbackError)
  21. .doOnTerminate(singleSemaphoreRelease)
  22. .doOnUnsubscribe(singleSemaphoreRelease);
  23. } else {
  24. return handleFallbackRejectionByEmittingError();
  25. }

fallbackSemaphore.tryAcquire()
如果是信号量隔离那么走
com.netflix.hystrix.AbstractCommand.TryableSemaphoreActual

  1. @Override
  2. public boolean tryAcquire() {
  3. int currentCount = count.incrementAndGet();
  4. if (currentCount > numberOfPermits.get()) {
  5. count.decrementAndGet();
  6. return false;
  7. } else {
  8. return true;
  9. }
  10. }

如果数量大于允许数量那么会返回false降级失败
也就是降级也有信号量的限制

数量初始化

在这里插入图片描述

通过构造函数进行设置

  1. public TryableSemaphoreActual(HystrixProperty<Integer> numberOfPermits) {
  2. this.numberOfPermits = numberOfPermits;
  3. }

fallbackSemaphorePerCircuit.putIfAbsent(commandKey.name(), new TryableSemaphoreActual(properties.fallbackIsolationSemaphoreMaxConcurrentRequests()));

数量是由HystrixCommandProperties的参数fallbackIsolationSemaphoreMaxConcurrentRequests决定

  1. protected TryableSemaphore getFallbackSemaphore() {
  2. if (fallbackSemaphoreOverride == null) {
  3. TryableSemaphore _s = fallbackSemaphorePerCircuit.get(commandKey.name());
  4. if (_s == null) {
  5. // we didn't find one cache so setup
  6. fallbackSemaphorePerCircuit.putIfAbsent(commandKey.name(), new TryableSemaphoreActual(properties.fallbackIsolationSemaphoreMaxConcurrentRequests()));
  7. // assign whatever got set (this or another thread)
  8. return fallbackSemaphorePerCircuit.get(commandKey.name());
  9. } else {
  10. return _s;
  11. }
  12. } else {
  13. return fallbackSemaphoreOverride;
  14. }
  15. }

在这里插入图片描述

/com/netflix/hystrix/HystrixCommandProperties.java:128

  1. this.fallbackIsolationSemaphoreMaxConcurrentRequests = getProperty(propertyPrefix, key, "fallback.isolation.semaphore.maxConcurrentRequests", builder.getFallbackIsolationSemaphoreMaxConcurrentRequests(), default_fallbackIsolationSemaphoreMaxConcurrentRequests);

如果不设置失败信号量,那么失败信号量的数量为default_fallbackIsolationSemaphoreMaxConcurrentRequests

private static final Integer default_fallbackIsolationSemaphoreMaxConcurrentRequests = 10;

可以通过withFallbackIsolationSemaphoreMaxConcurrentRequests方法对参数进行设置

  1. HystrixCommandProperties
  2. .Setter()
  3. .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)
  4. .withExecutionIsolationSemaphoreMaxConcurrentRequests(1)
  5. .withFallbackIsolationSemaphoreMaxConcurrentRequests(100)

发表评论

表情:
评论列表 (有 0 条评论,457人围观)

还没有评论,来说两句吧...

相关阅读