博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hystrix总结之限流
阅读量:4322 次
发布时间:2019-06-06

本文共 3965 字,大约阅读时间需要 13 分钟。

  hystrix使用舱壁隔离模式来隔离和限制各个请求,设计了两种隔离方式:信号量和线程池。线程池隔离:对每个command创建一个自己的线程池,执行调用。通过线程池隔离来保证不同调用不会相互干扰和每一个调用的并发限制。信号量隔热:对每个command创建一个自己的计数器,当并发量超过计数器指定值时,直接拒绝。使用信号量和线程池的一个区别是,信号量没有timeout机制。

  线程池隔离的本质是,如果在线程池执行模式下,调用响应的线程池,如果执行数量超过指定限制,线程池就会抛出异常。

if (properties.executionIsolationStrategy().get() == ExecutionIsolationStrategy.THREAD) {            return Observable.defer(new Func0
>() { @Override public Observable
call() { ... try { ... executionHook.onExecutionStart(_cmd); return getUserExecutionObservable(_cmd); } catch (Throwable ex) { return Observable.error(ex); } ... } }).subscribeOn(threadPool.getScheduler(new Func0
() { @Override public Boolean call() { return properties.executionIsolationThreadInterruptOnTimeout().get() && _cmd.isCommandTimedOut.get() == TimedOutStatus.TIMED_OUT; } })); }

  信号量隔离的本质是,针对每一个command使用一个原子变量,定义当前其执行并发量,如果在SEMAPHORE执行时,会尝试获取这个原子变量,如果超过了限制执行fallback降级流程。

private Observable
applyHystrixSemantics(final AbstractCommand
_cmd) { ...     final TryableSemaphore executionSemaphore = getExecutionSemaphore(); if (executionSemaphore.tryAcquire()) { try { ... return executeCommandAndObserve(_cmd) .doOnError(markExceptionThrown) .doOnTerminate(singleSemaphoreRelease) .doOnUnsubscribe(singleSemaphoreRelease); } catch (RuntimeException e) { return Observable.error(e); } } else { return handleSemaphoreRejectionViaFallback(); } ... }
protected TryableSemaphore getExecutionSemaphore() {        if (properties.executionIsolationStrategy().get() == ExecutionIsolationStrategy.SEMAPHORE) {            if (executionSemaphoreOverride == null) {                TryableSemaphore _s = executionSemaphorePerCircuit.get(commandKey.name());                if (_s == null) {                    // we didn't find one cache so setup                    executionSemaphorePerCircuit.putIfAbsent(commandKey.name(), new TryableSemaphoreActual(properties.executionIsolationSemaphoreMaxConcurrentRequests()));                    // assign whatever got set (this or another thread)                    return executionSemaphorePerCircuit.get(commandKey.name());                } else {                    return _s;                }            } else {                return executionSemaphoreOverride;            }        } else {            return TryableSemaphoreNoOp.DEFAULT;        }    }

  TryableSemaphoreActual封装了一个原子

static class TryableSemaphoreActual implements TryableSemaphore {        protected final HystrixProperty
numberOfPermits; private final AtomicInteger count = new AtomicInteger(0); public TryableSemaphoreActual(HystrixProperty
numberOfPermits) { this.numberOfPermits = numberOfPermits; } @Override public boolean tryAcquire() { int currentCount = count.incrementAndGet(); if (currentCount > numberOfPermits.get()) { count.decrementAndGet(); return false; } else { return true; } } @Override public void release() { count.decrementAndGet(); } @Override public int getNumberOfPermitsUsed() { return count.get(); } }

 

转载于:https://www.cnblogs.com/zhangwanhua/p/8250390.html

你可能感兴趣的文章
遍历datatable的几种方法(C# )
查看>>
Oracle记录(三) Scott用户的表结构
查看>>
centos静默式安装Oracle11g
查看>>
软件评测师下午题笔记
查看>>
性能测试的概念
查看>>
JavaScript中的函数上下文和apply,call
查看>>
中文排序
查看>>
少数股东损益
查看>>
SecureCRT的安装
查看>>
POJ2635-The Embarrassed Cryptographer
查看>>
css中font-family的中文字体
查看>>
学习笔记:CentOS 7学习之十二:查找命令
查看>>
delphi回调函数
查看>>
收到了TUSC寄来的V$ View For Oracle Database 11g
查看>>
gc buffer busy/gcs log flush sync与log file sync
查看>>
Java String.intern的深入研究
查看>>
动态上下线集群详解
查看>>
帝国cms修改栏目后文章列表的url错误怎么解决
查看>>
Linux下查看用户列表
查看>>
字符串左旋转操作
查看>>