hystrix元素详解

转自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

 

 

posted @ 2019-11-08 17:12  heroinss  阅读(573)  评论(0编辑  收藏  举报