转自https://www.cnblogs.com/wanggangblog/p/8550218.html

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service
public class ConsumerService { @Autowired
private RestTemplate restTemplate; @HystrixCommand(commandKey = "testCommand", groupKey = "testGroup", threadPoolKey = "testThreadKey",
fallbackMethod = "hiConsumerFallBack", ignoreExceptions = {NullPointerException.class},
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "30"),
@HystrixProperty(name = "maxQueueSize", value = "101"),
@HystrixProperty(name = "keepAliveTimeMinutes", value = "2"),
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"),
@HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
}
)
public String hiConsumer(String id) { //SERVICE_HI是服务端的spring.application.name,并且大写,hi为服务端提供的接口
return restTemplate.getForEntity("http://SERVICE_HI/hi", String.class).getBody();
} public String hiConsumerFallBack(String id, Throwable e) {
return "This is a error";
} }

  

让我们来逐个介绍下@HystrixCommand注解的各个参数:

  1:commandKey:配置全局唯一标识服务的名称,比如,库存系统有一个获取库存服务,那么就可以为这个服务起一个名字来唯一识别该服务,如果不配置,则默认是@HystrixCommand注解修饰的函数的函数名。

  2:groupKey:一个比较重要的注解,配置全局唯一标识服务分组的名称,比如,库存系统就是一个服务分组。通过设置分组,Hystrix会根据组来组织和统计命令的告、仪表盘等信息。Hystrix命令默认的线程划分也是根据命令组来实现。默认情况下,Hystrix会让相同组名的命令使用同一个线程池,所以我们需要在创建Hystrix命令时为其指定命令组来实现默认的线程池划分。此外,Hystrix还提供了通过设置threadPoolKey来对线程池进行设置。建议最好设置该参数,使用threadPoolKey来控制线程池组。

  3:threadPoolKey:对线程池进行设定,细粒度的配置,相当于对单个服务的线程池信息进行设置,也可多个服务设置同一个threadPoolKey构成线程组。

  4:fallbackMethod:@HystrixCommand注解修饰的函数的回调函数,@HystrixCommand修饰的函数必须和这个回调函数定义在同一个类中,因为定义在了同一个类中,所以fackback method可以是public/private均可。

  5:commandProperties:配置该命令的一些参数,如executionIsolationStrategy配置执行隔离策略,默认是使用线程隔离,此处我们配置为THREAD,即线程池隔离。参见:com.netflix.hystrix.HystrixCommandProperties中各个参数的定义。

  6:threadPoolProperties:线程池相关参数设置,具体可以设置哪些参数请见:com.netflix.hystrix.HystrixThreadPoolProperties
  7:ignoreExceptions:调用服务时,除了HystrixBadRequestException之外,其他@HystrixCommand修饰的函数抛出的异常均会被Hystrix认为命令执行失败而触发服务降级的处理逻辑(调用fallbackMethod指定的回调函数),所以当需要在命令执行中抛出不触发降级的异常时来使用它,通过这个参数指定,哪些异常抛出时不触发降级(不去调用fallbackMethod),而是将异常向上抛出。
  8:observableExecutionMode:定义hystrix observable command的模式;
      9:raiseHystrixExceptions:任何不可忽略的异常都包含在HystrixRuntimeException中;
      10:defaultFallback:默认的回调函数,该函数的函数体不能有入参,返回值类型与@HystrixCommand修饰的函数体的返回值一致。如果指定了fallbackMethod,则fallbackMethod优先级更高。
 

Hystrix使用说明,配置参数说明   这个文章对所有配置有一个详细的说明

hystrix熔断器之配置

command和pool和collapser的配置参数

HystrixCommandProperties命令执行相关配置:

  hystrix.command.[commandkey].execution.isolation.strategy 隔离策略THREAD或SEMAPHORE 默认HystrixCommands使用THREAD方式 HystrixObservableCommands使用SEMAPHORE

  hystrix.command.[commandkey].execution.timeout.enabled 是否开启超时设置,默认true。

  hystrix.command.[commandkey].execution.isolation.thread.timeoutInMilliseconds 默认超时时间 默认1000ms

  hystrix.command.[commandkey].execution.isolation.thread.interruptOnTimeout是否打开超时线程中断 默认值true

  hystrix.command.[commandkey].execution.isolation.thread.interruptOnFutureCancel 当隔离策略为THREAD时,当执行线程执行超时时,是否进行中断处理,即Future#cancel(true)处理,默认为false。

  hystrix.command.[commandkey].execution.isolation.semaphore.maxConcurrentRequests 信号量最大并发度 默认值10该参数当使用ExecutionIsolationStrategy.SEMAPHORE策略时才有效。如果达到最大并发请求数,请求会被拒绝。理论上选择semaphore size的原则和选择thread size一致,但选用semaphore时每次执行的单元要比较小且执行速度快(ms级别),否则的话应该用thread。

  hystrix.command.[commandkey].fallback.isolation.semaphore.maxConcurrentRequests fallback方法的信号量配置,配置getFallback方法并发请求的信号量,如果请求超过了并发信号量限制,则不再尝试调用getFallback方法,而是快速失败,默认信号量为10。

  hystrix.command.[commandkey].fallback.enabled 是否启用降级处理,如果启用了,则在超时或异常时调用getFallback进行降级处理,默认开启。

  hystrix.command.[commandkey].circuitBreaker.enabled 是否开启熔断机制,默认为true。

  hystrix.command.[commandkey].circuitBreaker.forceOpen 强制开启熔断,默认为false。

  hystrix.command.[commandkey].circuitBreaker.forceClosed 强制关闭熔断,默认为false。

  hystrix.command.[commandkey].circuitBreaker.sleepWindowInMilliseconds  熔断窗口时间,默认为5s。

  hystrix.command.[commandkey].circuitBreaker.requestVolumeThreshold 当在配置时间窗口内达到此数量后的失败,进行短路。默认20个

  hystrix.command.[commandkey].circuitBreaker.errorThresholdPercentage 出错百分比阈值,当达到此阈值后,开始短路。默认50%

  hystrix.command.[commandkey].metrics.rollingStats.timeInMilliseconds   设置统计滚动窗口的长度,以毫秒为单位。用于监控和熔断器 默认10s

  hystrix.command.[commandkey].metrics.rollingStats.numBuckets  设置统计窗口的桶数量 默认10

  hystrix.command.[commandkey].metrics.rollingPercentile.enabled 设置执行时间是否被跟踪,并且计算各个百分比,50%,90%等的时间 默认true

  hystrix.command.[commandkey].metrics.rollingPercentile.timeInMilliseconds 设置执行时间在滚动窗口中保留时间,用来计算百分比 默认60000ms

  hystrix.command.[commandkey].metrics.rollingPercentile.numBuckets 设置rollingPercentile窗口的桶数量 默认6。

  hystrix.command.[commandkey].metrics.rollingPercentile.bucketSize 此属性设置每个桶保存的执行时间的最大值 默认100。如果bucket size=100,window=10s,若这10s里有500次执行,只有最后100次执行会被统计到bucket里去。增加该值会增加内存开销以及排序的开销。

  hystrix.command.[commandkey].metrics.healthSnapshot.intervalInMilliseconds 记录health 快照(用来统计成功和错误绿)的间隔,默认500ms

  hystrix.command.[commandkey].requestCache.enabled 设置是否缓存请求,request-scope内缓存 默认值true

  hystrix.command.[commandkey].requestLog.enabled 设置HystrixCommand执行和事件是否打印到HystrixRequestLog中 默认值true

  hystrix.command.[commandkey].threadPoolKeyOverride   命令的线程池key,决定该命令使用哪个线程池。

HystrixThreadPoolProperties线程池相关配置:

  hystrix.threadpool.[threadkey].coreSize 线程池核心线程数 默认值10;

  hystrix.threadpool.[threadkey].maximumSize  线程池最大线程数 默认值10;

  hystrix.threadpool.[threadkey].allowMaximumSizeToDivergeFromCoreSize   当线程数大于核心线程数时,是否需要回收。与keepAliveTimeMinutes配合使用。

  hystrix.threadpool.[threadkey].keepAliveTimeMinutes  当实际线程数超过核心线程数时,线程存活时间 默认值1min

  hystrix.threadpool.[threadkey].maxQueueSize  最大等待队列数 默认不开启使用SynchronousQueue 不可动态调整

  hystrix.threadpool.[threadkey].queueSizeRejectionThreshold   允许在队列中的等待的任务数量 默认值5

  hystrix.threadpool.[threadkey].metrics.rollingStats.timeInMilliseconds 设置统计滚动窗口的长度,以毫秒为单位 默认值10000。

  hystrix.threadpool.[threadkey].metrics.rollingStats.numBuckets 设置统计窗口的桶数量 默认10

HystrixCollapserProperties批处理相关配置:

  hystrix.collapser.[collapserKey].maxRequestsInBatch 单次批处理的最大请求数,达到该数量触发批处理,默认Integer.MAX_VALUE

  hystrix.collapser.[collapserKey].timerDelayInMilliseconds  触发批处理的延迟,也可以为创建批处理的时间+该值,默认值10

  hystrix.collapser.[collapserKey].requestCache.enabled 默认值true

  hystrix.collapser.[collapserKey].metrics.rollingStats.timeInMilliseconds  默认值10000

  hystrix.collapser.[collapserKey].metrics.rollingStats.numBuckets 默认值10

  hystrix.collapser.[collapserKey].metrics.rollingPercentile.enabled 默认值true

  hystrix.collapser.[collapserKey].metrics.rollingPercentile.timeInMilliseconds 默认值60000

  hystrix.collapser.[collapserKey].metrics.rollingPercentile.numBuckets 默认值6

  hystrix.collapser.[collapserKey].metrics.rollingPercentile.bucketSize 默认值100

hystrix元素详解的更多相关文章

  1. 史上最全web.xml配置文件元素详解

    一.web.xml配置文件常用元素及其意义预览 <web-app> <!--定义了WEB应用的名字--> <display-name></display-na ...

  2. HTML5有语义的内联元素详解

    HTML5有语义的内联元素详解 time标签 time 元素表示一个时间值,比如 5:35 P.M., EST, April 23, 2007.例如: Example Source Code:< ...

  3. Jmeter 测试计划元素详解

    Jmeter 测试计划元素详解 by:授客 QQ:1033553122 由于篇幅问题,采用链接分享的形式,烦请复制以下网址,黏贴到浏览器中打开,下载 http://pan.baidu.com/s/1n ...

  4. web.xml配置文件元素详解

    一.web.xml配置文件常用元素及其意义 1 <web-app> 2 3 <!--定义了WEB应用的名字--> 4 <display-name></disp ...

  5. JS DOM对象控制HTML元素详解

    JS DOM对象控制HTML元素详解 方法: getElementsByName()  获取name getElementsByTagName()  获取元素 getAttribute()  获取元素 ...

  6. html5中output元素详解

    html5中output元素详解 一.总结 一句话总结: output元素是HTML5新增的元素,用来设置不同数据的输出,没什么大用,了解即可 <form action="L3_01. ...

  7. html5的datalist元素详解

    html5的datalist元素详解 一.总结 一句话总结: datalist元素配合input元素可以出现有提示选择作用的选框效果,还是相对比较简便好用的 1.optgroup元素是干嘛的? opt ...

  8. html5中section元素详解

    html5中section元素详解 一.总结 一句话总结: section元素 用来定义文章中的章节(通常应该有标题和段落内容) section元素的作用就是给内容分段,给页面分区 1.section ...

  9. html5中time元素详解

    html5中time元素详解 一.总结 一句话总结: time的使用的话主要是将时间放在datetime属性里面:<time datetime="2015-10-22"> ...

随机推荐

  1. Docker 学习笔记(一)

    Docker 入门 Docker 学习 概述 安装 命令 镜像命令 容器命令 操作命令 Docker 镜像 容器数据卷 DockerFile Docker网络原理 IDEA 整合Docker 单机版D ...

  2. GitBook 常用插件

    目录 必看说明 插件说明 page-treeview 目录 code 代码 pageview-count 阅读量计数 popup 图片点击查看 tbfed-pagefooter 页面添加页脚(简单版) ...

  3. 90%的开发都没搞懂的CI和CD!

    据IDC统计,2017年,DevOps的全球软件市场已达到约39亿美元的水平,预计到2022年市场将达到80亿美元左右! 在敏捷软件开发环境中,工作模型和操作需要对公司不断变化的需求具有超级灵活的能力 ...

  4. React_TODOList 数据增删改查

    ①.功能代码实现: 添加数据 查询数据,展示 删除数据 修改数据 ②.数据持久化操作 localStorage.setItem('key',value) var value = localStorag ...

  5. 【java学习笔记】LongAdder

    目录 1.背景 2.LongAdder 3.Striped64内部结构 4.LongAdder的add方法解析 5.Striped64的longAccumulate方法解析 6.总结 LongAdde ...

  6. ZEQP仓储管理系统( WMS)开源

    ZEQP仓储管理系统 代码框架是用的前后台分离的方式 后台使用的是Asp.Net Core平台,开发所有业务,向前台提供Rest API接口. 使用的认证方式是JWT 前端有两个项目,一个是Web端, ...

  7. 关于windou环境下使用http或者ftp搭建网络hu共享

    第一步 右键此电脑进入控制面 第二步:进入程序点击启用或关闭windous功能 第三步进入服务开启界面 点击让windows更新为你下载文件,并保存更改完,然后关闭 四:邮件我的电脑进入管理 四右键添 ...

  8. ajax请求默认都是异步请求,怎么变为同步请求

    Ajax请求默认的都是异步的 如果想同步 async设置为false就可以(默认是true) var html = $.ajax({   url: "some.PHP",   as ...

  9. Istio安全-授权(实操三)

    Istio安全-授权 目录 Istio安全-授权 授权HTTP流量 为使用HTTP流量的负载配置访问控制 卸载 授权TCP流量 部署 配置TCP负载的访问控制 卸载 使用JWT进行授权 部署 使用有效 ...

  10. 利用Python爬虫刷新某网站访问量

    前言:前一段时间看到有博友写了爬虫去刷新博客访问量一篇文章,当时还觉得蛮有意思的,就保存了一下,但是当我昨天准备复现的时候居然发现文章404了.所以本篇文章仅供学习交流,严禁用于商业用途 很多人学习p ...