本文主要研究一下hystrix的execution.isolation.semaphore.maxConcurrentRequests属性

AbstractCommand.applyHystrixSemantics

hystrix-core-1.5.12-sources.jar!/com/netflix/hystrix/AbstractCommand.java

    private Observable<R> applyHystrixSemantics(final AbstractCommand<R> _cmd) {// mark that we're starting execution on the ExecutionHook// if this hook throws an exception, then a fast-fail occurs with no fallback.  No state is left inconsistentexecutionHook.onStart(_cmd);/* determine if we're allowed to execute */if (circuitBreaker.attemptExecution()) {final TryableSemaphore executionSemaphore = getExecutionSemaphore();final AtomicBoolean semaphoreHasBeenReleased = new AtomicBoolean(false);final Action0 singleSemaphoreRelease = new Action0() {@Overridepublic void call() {if (semaphoreHasBeenReleased.compareAndSet(false, true)) {executionSemaphore.release();}}};final Action1<Throwable> markExceptionThrown = new Action1<Throwable>() {@Overridepublic void call(Throwable t) {eventNotifier.markEvent(HystrixEventType.EXCEPTION_THROWN, commandKey);}};if (executionSemaphore.tryAcquire()) {try {/* used to track userThreadExecutionTime */executionResult = executionResult.setInvocationStartTime(System.currentTimeMillis());return executeCommandAndObserve(_cmd).doOnError(markExceptionThrown).doOnTerminate(singleSemaphoreRelease).doOnUnsubscribe(singleSemaphoreRelease);} catch (RuntimeException e) {return Observable.error(e);}} else {return handleSemaphoreRejectionViaFallback();}} else {return handleShortCircuitViaFallback();}}

这个方法调用了getExecutionSemaphore来获取TryableSemaphore,执行之前进行tryAcquire,执行结束之后进行release

AbstractCommand.getExecutionSemaphore

hystrix-core-1.5.12-sources.jar!/com/netflix/hystrix/AbstractCommand.java

    /*** Get the TryableSemaphore this HystrixCommand should use for execution if not running in a separate thread.* * @return TryableSemaphore*/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 setupexecutionSemaphorePerCircuit.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 NoOp implementation since we're not using SEMAPHORE isolationreturn TryableSemaphoreNoOp.DEFAULT;}}
  • 这里针对ExecutionIsolationStrategy进行判断,如果是SEMAPHORE,则根据commandKey获取或新建对应的TryableSemaphore
  • 创建的话,使用的是TryableSemaphoreActual,其numberOfPermits参数就是execution.isolation.semaphore.maxConcurrentRequests的值
  • 如果ExecutionIsolationStrategy是THREAD的话,这里TryableSemaphore返回的是TryableSemaphoreNoOp.DEFAULT,也就是不做任何操作,都放行

TryableSemaphore

    /* package */static interface TryableSemaphore {/*** Use like this:* <p>* * <pre>* if (s.tryAcquire()) {* try {* // do work that is protected by 's'* } finally {* s.release();* }* }* </pre>* * @return boolean*/public abstract boolean tryAcquire();/*** ONLY call release if tryAcquire returned true.* <p>* * <pre>* if (s.tryAcquire()) {* try {* // do work that is protected by 's'* } finally {* s.release();* }* }* </pre>*/public abstract void release();public abstract int getNumberOfPermitsUsed();}

定义了三个方法,tryAcquire,release、getNumberOfPermitsUsed

TryableSemaphoreActual

    /*** Semaphore that only supports tryAcquire and never blocks and that supports a dynamic permit count.* <p>* Using AtomicInteger increment/decrement instead of java.util.concurrent.Semaphore since we don't need blocking and need a custom implementation to get the dynamic permit count and since* AtomicInteger achieves the same behavior and performance without the more complex implementation of the actual Semaphore class using AbstractQueueSynchronizer.*//* package */static class TryableSemaphoreActual implements TryableSemaphore {protected final HystrixProperty<Integer> numberOfPermits;private final AtomicInteger count = new AtomicInteger(0);public TryableSemaphoreActual(HystrixProperty<Integer> numberOfPermits) {this.numberOfPermits = numberOfPermits;}@Overridepublic boolean tryAcquire() {int currentCount = count.incrementAndGet();if (currentCount > numberOfPermits.get()) {count.decrementAndGet();return false;} else {return true;}}@Overridepublic void release() {count.decrementAndGet();}@Overridepublic int getNumberOfPermitsUsed() {return count.get();}}

内部是使用AtomicInteger来进行计数,tryAcquire方法,是先增,如果超过限制,则再减

TryableSemaphoreNoOp.DEFAULT

    /* package */static class TryableSemaphoreNoOp implements TryableSemaphore {public static final TryableSemaphore DEFAULT = new TryableSemaphoreNoOp();@Overridepublic boolean tryAcquire() {return true;}@Overridepublic void release() {}@Overridepublic int getNumberOfPermitsUsed() {return 0;}}

TryableSemaphoreNoOp.DEFAULT是TryableSemaphoreNoOp的单例,默认放行

小结

hystrix的ExecutionIsolationStrategy分为SEMAPHORE及THREAD模式,在command的执行前后逻辑,内置了对TryableSemaphore的tryAcquire及release操作。只是在获取TryableSemaphore实现类的时候,针对SEMAPHORE模式才真正根据execution.isolation.semaphore.maxConcurrentRequests属性进行限制,而如果是THREAD模式,则返回一个都放行的TryableSemaphoreNoOp实例。

doc

  • execution.isolation.semaphore.maxConcurrentRequests

聊聊hystrix的execution.isolation.semaphore.maxConcurrentRequests属性相关推荐

  1. 聊聊hystrix的semaphore.maxConcurrentRequests属性

    为什么80%的码农都做不了架构师?>>>    序 本文主要研究一下hystrix的execution.isolation.semaphore.maxConcurrentReques ...

  2. 聊聊flink的Execution Plan Visualization

    为什么80%的码农都做不了架构师?>>>    序 本文主要研究一下flink的Execution Plan Visualization 实例 代码 @Testpublic void ...

  3. 聊聊数据库中的关键字——字段、属性、列、元组、记录、表、主键、外键

    学完数据库,我们对SQL SERVER 2008中的部分关键字有了大概的了解,下面我来总结一下几个比较重要的关键字:字段.属性.列.记录(元组).表.主键.外键. 一.字段:某一个事物的一个特征,或者 ...

  4. SpringCloud中Hystrix容错保护原理及配置,看它就够了!

    点击关注公众号,Java干货及时送达 作者:kosamino cnblogs.com/jing99/p/11625306.html 1 什么是灾难性雪崩效应? 如下图的过程所示,灾难性雪崩形成原因就大 ...

  5. 使用Hystrix实现自动降级与依赖隔离[微服务]

    来自:云时代架构 这篇文章是记录了自己的一次集成Hystrix的经验,原本写在公司内部wiki里,所以里面有一些内容为了避免重复,直接引用了其他同事的wiki,而发布到外网,这部分就不能直接引用了,因 ...

  6. 服务容错保护断路器Hystrix之二:Hystrix工作流程解析

    一.总运行流程 当你发出请求后,hystrix是这么运行的  详细解释个步骤 1.创建  HystrixCommand or HystrixObservableCommand Object   Hys ...

  7. hystrix相关配置

    Execution相关的属性的配置 hystrix.command.default.execution.isolation.strategy 隔离策略,默认是Thread, 可选Thread| Sem ...

  8. hystrix熔断 简介_Hystrix简介– Hello World

    hystrix熔断 简介 在以前的博客文章中,我介绍了需要像Netflix Hystrix这样的库的动机. 在这里,我将跳入一些非常基本的方法来开始使用Hystrix,并在更复杂的用例中进行跟进. 你 ...

  9. Hystrix简介– Hello World

    在先前的博客文章中,我谈到了需要像Netflix Hystrix这样的库的动机. 在这里,我将跳入一些非常基本的方法来开始使用Hystrix,并在更复杂的用例中进行后续介绍. 你好,世界 以下是&qu ...

最新文章

  1. 6月27日任务 配置Tomcat监听80端口、配置Tomcat虚拟主机、Tomcat日志
  2. 全球及中国天然肠衣行业投资盈利分析及竞争格局展望报告2022-2027年
  3. SLF4J: Failed to load class org.slf4j.impl.StaticLoggerBinder
  4. Hive的基本操作-数据库的创建和删除
  5. .NET Core使用IO合并技巧轻松实现千万级消息推送
  6. jboss fuse 教程_JBoss Fuse:使用JEXL的动态蓝图文件
  7. UVA 2474 - Balloons in a Box 爆搜
  8. 数据库之MySQL补充
  9. 最老程序员创业札记:全文检索、数据挖掘、推荐引擎应用36
  10. Swift NSDate的一个分类,把Mon Apr 04 19:45:37 +0800 2016这种格式的时间转换为2016-04-04 11:45:37 +0000
  11. SpringAOP底层API之代理对象执行流程
  12. Android 蓝牙手柄开发
  13. html新的页面打开新页面,javascript如何打开新窗口?
  14. php5.0 cms安装教程,小浣熊CMS5.0漫画系统安装教程和采集教程
  15. VC无进程木马下载器源码
  16. matlab 秩和检验,多个独立样本比较的秩和检验(Kruskal-Wallis H)
  17. 转page类事件执行顺序
  18. 【多模态】《Visual7W: Grounded Question Answering in Images》论文阅读笔记
  19. gateway统一网关
  20. 手机坏了微信聊天记录怎么恢复?不用怕,用这招

热门文章

  1. GridView RowCommand 获取列值
  2. vim中开shell
  3. Silverlight 开发入门1
  4. win10打开本机telnet客户端
  5. win32 API 遍历一个目录下的文件
  6. PowerDesigner中Stereotype的创建图解
  7. 统计_statistics_不同的人_大样本_分析_统计方法_useful ?
  8. JavaScript RegExp 对象
  9. bitbucket迁移
  10. 浅谈WebGIS开放数据(矢量数据)