circuit

在Hystrix调用服务时,难免会遇到异常,如对方服务不可用,在这种情况下如果仍然不停地调用就是不必要的,在Hystrix中可以配置使用circuit,当达到一定程度错误,就会自动调用fallback方法而不是用run方法。

配置

在Command的构造函数的CommandPropertiesDefaults中可以配置以下的参数

    circuitBreakerRequestVolumeThreshold; // 在时间窗口(默认10s)中需要达到的访问数量	默认20
circuitBreakerSleepWindowInMilliseconds; // 触发circuit后重试的间隔 默认5s
circuitBreakerEnabled; // 是否启用circuit 默认启用
circuitBreakerErrorThresholdPercentage; // 在时间窗口中触发circuit的错误访问百分比 默认50%

从配置可以粗略看出,circuit就是在一个时间窗口内,当访问达到一定数量且错误率达到一定阈值就直接调用fallback,过一定时间之后会尝试,如果成功就重新开始时间窗口,如果失败继续调用fallback,如此往复。

实现

Hystrix的默认circuit实现是HystrixCircuitBreakerImpl

//这个是主要的逻辑,用于判断是否进行circuit,返回false则直接调用fallback,返回true则调用run
@Override
public boolean allowRequest() {
//判断是否强制开启circuit,使用circuitBreakerForceOpen配置
if (properties.circuitBreakerForceOpen().get()) {
// properties have asked us to force the circuit open so we will allow NO requests
return false;
}
//判断是否强制关闭circuit,使用circuitBreakerForceClosed配置
if (properties.circuitBreakerForceClosed().get()) {
// we still want to allow isOpen() to perform it's calculations so we simulate normal behavior
isOpen();
// properties have asked us to ignore errors so we will ignore the results of isOpen and just allow all traffic through
return true;
}
//调用isOpen和allowSingleTest,前者是控制流量和错误率,后者是使用
return !isOpen() || allowSingleTest();
}
//判断是否触发circuit
@Override
public boolean isOpen() {
if (circuitOpen.get()) {
// if we're open we immediately return true and don't bother attempting to 'close' ourself as that is left to allowSingleTest and a subsequent successful test to close
return true;
}
//healthCounts是一个记录请求详细信息的subject,底层是注册在Rxjava上的
// we're closed, so let's see if errors have made us so we should trip the circuit open
HealthCounts health = metrics.getHealthCounts(); //校验是否到达对应的流量
// check if we are past the statisticalWindowVolumeThreshold
if (health.getTotalRequests() < properties.circuitBreakerRequestVolumeThreshold().get()) {
// we are not past the minimum volume threshold for the statisticalWindow so we'll return false immediately and not calculate anything
return false;
}
//校验是否到达对应的错误率
if (health.getErrorPercentage() < properties.circuitBreakerErrorThresholdPercentage().get()) {
return false;
} else {
//开启circuit,使用CAS来处理多线程的征用
// our failure rate is too high, trip the circuit
if (circuitOpen.compareAndSet(false, true)) {
// if the previousValue was false then we want to set the currentTime
circuitOpenedOrLastTestedTime.set(System.currentTimeMillis());
return true;
} else {
// How could previousValue be true? If another thread was going through this code at the same time a race-condition could have
// caused another thread to set it to true already even though we were in the process of doing the same
// In this case, we know the circuit is open, so let the other thread set the currentTime and report back that the circuit is open
return true;
}
}
} //当开启circuit在一段时间后需要重试,allowRequest中使用||短路符号来触发时间校验
public boolean allowSingleTest() {
//获得上次成功运行或测试时间
long timeCircuitOpenedOrWasLastTested = circuitOpenedOrLastTestedTime.get();
// 1) if the circuit is open
// 2) and it's been longer than 'sleepWindow' since we opened the circuit
//如果circuit开启并时间间隔大于响应的时间则进行测试,为了避免多线程的问题,这里也使用CAS进行比较时间
if (circuitOpen.get() && System.currentTimeMillis() > timeCircuitOpenedOrWasLastTested + properties.circuitBreakerSleepWindowInMilliseconds().get()) {
// We push the 'circuitOpenedTime' ahead by 'sleepWindow' since we have allowed one request to try.
// If it succeeds the circuit will be closed, otherwise another singleTest will be allowed at the end of the 'sleepWindow'.
if (circuitOpenedOrLastTestedTime.compareAndSet(timeCircuitOpenedOrWasLastTested, System.currentTimeMillis())) {
// if this returns true that means we set the time so we'll return true to allow the singleTest
// if it returned false it means another thread raced us and allowed the singleTest before we did
return true;
}
}
return false;
}

总结

Hystrix的circuit可以减少发生错误时的无用调用,其中最关键的是其中的HealthCounts用来记录错误率和流量,同时这个记录不是实时的是有一定的时间间隔的,这个是使用RxJava来实现的,后续会就Rxjava进行研究。

Hystrix框架4--circuit的更多相关文章

  1. Hystrix框架1--入门

    介绍 在开发应用中或多或少会依赖各种外界的服务,利用各个服务来完成自己的业务需求,现在流行的微服务架构更是离不开各个服务之间的调用,这就导致整体应用的可用性依赖于各个依赖服务的可用性. 比如一个依赖3 ...

  2. Hystrix框架5--请求缓存和collapser

    简介 在Hystrix中有个Request的概念,有一些操作需要在request中进行 缓存 在Hystrix调用服务时,如果只是查询接口,可以使用缓存进行优化,从而跳过真实访问请求. 应用 需要启用 ...

  3. Hystrix框架3--线程池

    线程池 在Hystrix中Command默认是运行在一个单独的线程池中的,线程池的名称是根据设定的ThreadPoolKey定义的,如果没有设置那么会使用CommandGroupKey作为线程池. 这 ...

  4. Hystrix框架2--超时

    timeout 在调用第三方服务时有些情况需要对服务响应时间进行把控,当超时的情况下进行fallback的处理 下面来看下超时的案例 public class CommandTimeout exten ...

  5. Hystrix 框架

    雪崩效应的产生原因:当一个服务突然受到高并发的请求,tomcat服务器承受不了的情况下会产生服务堆积,可能导致其他的服务也不可用. 服务保护:当服务产生堆积的时候,对服务实现保护功能. 服务隔离:每个 ...

  6. 微服务(二)hystrix

    特性 1.延迟和失败容忍 防止级联错误,错误回退,优雅降级.快速失败和恢复 线程和信号量隔离 2.实时监控和配置更改 3.并发 并行执行,请求缓存,自动批处理失败请求 总运行流程 当你发出请求后,hy ...

  7. 微服务—熔断器Hystrix

    前言在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元应用间通过服务注册与发现的方式互相依赖. 由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服 ...

  8. 基于hystrix的线程池隔离

    hystrix进行资源隔离,其实是提供了一个抽象,叫做command,就是说,你如果要把对某一个依赖服务的所有调用请求,全部隔离在同一份资源池内 对这个依赖服务的所有调用请求,全部走这个资源池内的资源 ...

  9. 使用Hystrix提高系统可用性

    今天稍微复杂点的互联网应用,服务端基本都是分布式的,大量的服务支撑起整个系统,服务之间也难免有大量的依赖关系,依赖都是通过网络连接起来. (图片来源:https://github.com/Netfli ...

随机推荐

  1. 深入浅出JMS(一) JMS基本概念

    摘要:The JavaMessage Service (JMS) API is a messaging standard that allows application components base ...

  2. 关于SVN出现 svn working copy locked的原因及解决方法

    今天使用SVN时电脑卡住了,于是结束进程中断了SVN的操作,于是出现了如题问题, 产生这种情况大多是因为上次svn更新命令执行失败且被自动锁定了. 如果cleanup没有效果的话只好手动删除锁定文件. ...

  3. iOS应用九宫格算法

      1.框框横向间隔为:( "界面的总宽度"-"界面每行的控件的宽度" * "每行要现实的列数(这里是3)") / ("列数 + ...

  4. 活动助手Beta用户试用报告

    用户试用报告 1.面向参与者用户 1.1 日常参加各类学习(水综测)活动中,有没有遇到以下问题: (1) 信息来源混乱,不知道靠不靠谱 (2) 每次报名都要重新填写自己的学号手机号,有时候填错了就没综 ...

  5. 查看java源码显示source not found

    除了下载的jar包之外,还要下载src源码的zip包. 进项目的Java Build Path里找到你想看源码的jar包 Source Attachment Configuration--Extern ...

  6. MongoDB实现分页(两种方法)

    1.插入实验数据 偷懒用下samus,100条. ; i < ; i++) { Document doc = new Document(); doc["ID"] = i; d ...

  7. 【Java】JDK类 CountDownLatch

    一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在当前计数到达 ...

  8. HTML常用标签总结

    HTML 的常用标签总结 <font size="字体大小1-7" color="red或0xff00ff" face="字体类型(楷体等)&q ...

  9. 腾讯网2016回响中国:华清远见荣获2016年度知名IT培训品牌

    12月1日,由腾讯网主办的“2016回响中国·腾讯网教育年度盛典”上,揭晓了“2016腾讯网教育年度总评榜”榜单.高端IT就业培训专家——华清远见教育集团凭借自身优质的高薪IT就业服务优势成功入围,荣 ...

  10. 中文字符匹配js正则表达式

    普遍使用的正则是[\u4e00-\u9fa5],但这个范围并不完整.例如:  /[\u4e00-\u9fa5]/.test( '⻏' ) // 测试部首⻏,返回false    根据Unicode 5 ...