0、注意

0.1、如果使用command 的 execute( )方法的话,其实在一个for循环,执行多次,其实每个的执行顺序并不是固定的,如果你想固定,需要使用queue

circuit breaker OPEN, execution not attempte

0.2、进入短路状态,不需要执行run,所以不需要一个新的线程,直接main就好了

具体代码参考 https://gitlab.com/hitxujian/test_hystrix/blob/master/src/main/java/com/unisound/test_hystrix/HelloWorldCommand2.java

一、修改参数

设置参数有3个地方:通过日志

2018-02-12 18:07:36.876 DEBUG HystrixPropertiesChainedProperty:93 - Flipping property: hystrix.command.HttpPostCommand.execution.isolation.thread.timeoutInMilliseconds to use NEXT property: hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 1000

2018-02-12 18:07:37.113 DEBUG HystrixPropertiesChainedProperty:236 - Property changed: 'hystrix.command.HttpPostCommand.execution.isolation.thread.timeoutInMilliseconds = 500'

可以看到3个参数

hystrix.command.HttpPostCommand.execution.isolation.thread.timeoutInMilliseconds

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds

hystrix.command.HttpPostCommand.execution.isolation.thread.timeoutInMilliseconds

》》其中default是在 command类之外

ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.circuitBreaker.forceOpen", true);

》》然后剩下的那个可以在command类内修改

二、在 callback中打印异常信息

@Override
protected String getFallback() {
// String reason=null;
// if (isCircuitBreakerOpen()) {
// reason = "Circuit Breaker Open";
// } else if (isResponseRejected()) {
// reason = "Response rejected";
// } else if (isResponseTimedOut()) {
// reason = "Response timed-out";
// }


//System.out.println("进入回调");
log.info("发生了回调异常信息是"+ getExecutionException());

return null;
}

三、熔断器3种状态,

熔断打开 就是trip,表示跳闸了

正常状态下,电路处于关闭状态(Closed),如果调用持续出错或者超时,电路被打开进入熔断状态(Open),后续一段时间内的所有调用都会被拒绝(Fail Fast),

这个拒绝时间withCircuitBreakerSleepWindowInMilliseconds控制默认是5s

一段时间以后,保护器会尝试进入半熔断状态(Half-Open),允许少量请求进来尝试,如果调用仍然失败,则回到熔断状态,如果调用成功,则回到电路闭合状态;

具体解释3种状态

Closed State

When the service dependency is healthy and no issues are detected, the Circuit Breaker is in state closed. All invocations are passed through to the service.

Open State

The Circuit Breaker considers the following invocations as failed and factors them in when deciding whether to trip the circuit open:

    • An exception thrown (e.g., cannot connect, or service returns HTTP error 500)

    • The call takes longer than the configured timeout (by default 1 second)

    • The internal thread pool (or semaphore) used by Hystrix for the command rejects execution (Hystrix uses thread pools to limit resources used by a dependency)

The circuit opens as soon as Hystrix determines that the error threshold over a statistical time window has been reached (by default 50% errors over a 10 seconds window). In the open state, the Circuit Breaker will reject invocations by either:

    • Throwing an exception (also termed "fail fast", this is the default behavior)

    • Returning a fallback result (e.g., returning a null, empty, or stubbed result)

Half-Open State

To be able to recover from the error condition, when the Circuit Breaker is in the open state, it periodically leaves through one invocation at a configurable interval (by default 5 seconds) - this is the half-open state. If the invocation succeeds, the circuit is closed again.----

注意这的说法熔断后,等一定时间间隔,比如5s,再次接受请求,如果这个请求成功,那么熔断器关闭

四、熔断器具体执行逻辑

  1. 将远程服务调用逻辑封装进一个HystrixCommand。

  2. 对于每次服务调用可以使用同步或异步机制,对应执行execute()或queue()。

  3. 判断熔断器(circuit-breaker)是否打开或者半打开状态,如果打开跳到步骤8,进行回退策略,如果关闭进入步骤4。

  4. 判断线程池/队列/信号量(使用了舱壁隔离模式)是否跑满,如果跑满进入回退步骤8,否则继续后续步骤5。

  5. run方法中执行了实际的服务调用。

    a. 服务调用发生超时时,进入步骤8。

  6. 判断run方法中的代码是否执行成功。

    a. 执行成功返回结果。

    b. 执行中出现错误则进入步骤8。

  7. 所有的运行状态(成功,失败,拒绝,超时)上报给熔断器,用于统计从而影响熔断器状态。

  8. 进入getFallback()回退逻辑。

    a. 没有实现getFallback()回退逻辑的调用将直接抛出异常。

    b. 回退逻辑调用成功直接返回。

    c. 回退逻辑调用失败抛出异常。

  9. 返回执行成功结果。

五、关于线程池说明

首先说明两个数据结构:  the set of tasks  和   a queue,然后分别介绍这两个数据结构,具体可以网上搜索allowMaximumSizeToDivergeFromCoreSize

There are 2 data structures involved in a thread pool: the set of tasks (the actual pool), and a queue in front of the thread pool.

The thread pool contains all tasks which are currently running. Configuring this pool sets up the number of threads that may execute concurrently. Relevant config:

    • coreSize: number of threads to keep in the pool at all times
    • maximumSize: maximum number of threads in the pool
    • allowMaximumSizeToDivergeFromCoreSize: allow the prior config values to differ (previously only coreSize was exposed, and maximumSize was forced to that value)

The queue is a different data structure. It contains tasks which have not yet begun to run. Tasks first get placed in this queue, and the thread pool picks them up from there. Relevant config:

    • maxQueueSize: size of the queue (static - can only get set at startup)
    • queueRejectionThreshold: some number below the maxQueueSize that controls maximum number of tasks in the queue. If queueRejectionThreshold is 5, and there are 5 tasks in the queue, the queue will reject the next task submitted.

参考

http://www.cnblogs.com/ulysses-you/p/7281662.html

http://www.lordofthejars.com/2014/09/defend-your-application-with-hystrix.html

https://www.slideshare.net/spjelkavik/hystrix-what-did-we-learn?qid=bc5fba99-23da-41bd-9530-8c3207770cac&v=&b=&from_search=2

http://bed-con.org/2014/files/slides/uwe_friedrichsen-hystrix.pdf  关于参数配置(*)

https://tech.meituan.com/service-fault-tolerant-pattern.html

http://www.hascode.com/2017/02/resilient-architecture-circuit-breakers-for-java-hystrix-vert-x-javaslang-and-failsafe-examples/

hystrix熔断机制修改配置的更多相关文章

  1. SpringCloud系列七:Hystrix 熔断机制(Hystrix基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控)

    1.概念:Hystrix 熔断机制 2.具体内容 所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝会自动烧断,以保护我们的电器, 那么如果换到了程序之中呢? 当现在服务的 ...

  2. Hystrix 熔断机制

    熔断机制相当于电路的跳闸功能,即在一定时间内,错误比例达到一定数目时业务从原来流程转移到另外的流程处理.在一段时间后,恢复到原业务逻辑. 测试代码如下 /** * @author zimu * @de ...

  3. spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 前言 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  4. spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  5. Hystrix熔断机制原理剖析

    一.前言 在分布式系统架构中多个系统之间通常是通过远程RPC调用进行通信,也就是 A 系统调用 B 系统服务,B 系统调用 C 系统的服务.当尾部应用 C 发生故障而系统 B 没有服务降级时候可能会导 ...

  6. Ribbon重试机制与Hystrix熔断机制的配置问题1

    Ribbon超时与Hystrix超时问题,为了确保Ribbon重试的时候不被熔断,我们就需要让Hystrix的超时时间大于Ribbon的超时时间,否则Hystrix命令超时后,该命令直接熔断,重试机制 ...

  7. Ribbon重试机制与Hystrix熔断机制的配置问题

    Ribbon超时与Hystrix超时问题,为了确保Ribbon重试的时候不被熔断,我们就需要让Hystrix的超时时间大于Ribbon的超时时间,否则Hystrix命令超时后,该命令直接熔断,重试机制 ...

  8. Hystrix 熔断机制原理

    相关配置 circuitBreaker.enabled 是否开启熔断 circuitBreaker.requestVolumeThreshold 熔断最低触发请求数阈值 circuitBreaker. ...

  9. Hystrix熔断机制导致误报请求超时错误

    问题的过程如下: (1)前端向服务端请求往HBase插入1000条数据: (2)请求经路由网关Zuul传递给HBaseService,HBaseService执行插入操作: (3)插入操作需要的时间超 ...

随机推荐

  1. python -- 程序异常与调试(异常处理)

    一.异常处理 针对在运行时可能会出错的语句块,可以提前设计好出现问题后的解决方案, 或者给出相应的提示信息.使用try-except语句来处理Python抛出的异常: # -------------- ...

  2. 微信小程序云开发-云存储-上传单张照片到云存储并显示到页面上

    一.wxml文件 页面上写上传图片的按钮,按钮绑定chooseImg. <button bindtap="chooseImg" type="primary" ...

  3. Guass消元总结

    Guass消元 约旦·高斯消元法 求线性方程组 我们用一个\(n*(n+1)\)的矩阵存储线性方程组各项系数和零次项系数. 每一次找到一个未知数系数最大的方程,交换当前行方程和该方程,并将其他行该未知 ...

  4. mybatis的增删改查返回值小析(六)

    本文验证了通过mybatis访问数据库时的,增删改查的返回值情况. 直接看代码. 1.service层 /** *@Author: Administrator on 2020/3/12 15:15 * ...

  5. Codeforces Round #735 (Div. 2) 题解

    比赛地址:https://codeforces.com/contest/1554. 只有 ABCD 的题解,E 不会. A 答案是 \(\max_i\{a_ia_{i+1}\}\).证明:(反证)如果 ...

  6. C实现奇偶校验

    奇偶校验原理(来自百度百科):奇偶校验(Parity Check)是一种校验代码传输正确性的方法.根据被传输的一组二进制代码的数位中"1"的个数是奇数或偶数来进行校验.采用奇数的称 ...

  7. YOLO-V4 实现口罩识别(附加数据、数据批量处理程序)

    一.YOLO-v4概念 如果想要了解和认识yolo-v4的基本概念,首先要提的就是它的基础版本yolo-v1,对于yolo来说,最经典的算是yolo-v3.如果想要了解它的由来和历史的话,可以自行搜索 ...

  8. [考试总结]noip模拟33

    连炸两场... 伤心... 第一个题目首先因为有期望坐镇,然后跳过... 然后第二个题目发现题目挺绕的,然后转化了一句话题意,然后..... \(\huge{\text{转化错了!!!!}}\) 然而 ...

  9. Java面向对象10——方法重写

    方法重写 static :  ​ ​ package oop.demon01.demon05; ​ public class Application {     public static void ...

  10. XCTF-ics-05(文件包含+preg_replace函数/e修正符下的代码执行漏洞)

    记一道preg_replace函数/e模式下的代码执行漏洞利用的题. 只有设备维护中心页面可以进入,页面没有什么可点击的,查看源代码,发现这里有个参数. 拼接到url,页面显示index,拼接/etc ...