美文网首页
Spring Cloud全解析:熔断之Hystrix配置参数

Spring Cloud全解析:熔断之Hystrix配置参数

作者: 墨线宝 | 来源:发表于2024-09-04 10:11 被阅读0次

    Hystrix配置参数

    @HystrixCommand(groupKey="UserGroup", commandKey = "GetUserByIdCommand",
                    commandProperties = {
                        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "100"),//指定多久超时,单位毫秒。超时进fallback
                        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),//判断熔断的最少请求数,默认是10;只有在一个统计窗口内处理的请求数量达到这个阈值,才会进行熔断与否的判断
                        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "10"),//判断熔断的阈值,默认值50,表示在一个统计窗口内有50%的请求处理失败,会触发熔断
                    },
                    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")
            })
    
    • groupKey group标识,一个group使用一个线程池
    • commandKey command标识,当前执行方法名
    • threadPoolKey
    • fallbackMethod fallback方法名,两者需要返回值和参数列表相同
    • ignoreExceptions
    • observableExecutionMode
    • raiseHystrixExceptions
    • defaultFallback

    commandProperties参数

    HystrixCommandProperties

    • circuitBreaker.enabled 是否开启断路器,默认true
    • circuitBreaker.requestVolumeThreshold 一个滑动窗口下的最小的请求次数(请求是否达到开启的阈值),默认20,只有在一个统计窗口内处理的请求数量达到这个阈值,才会进行熔断与否的判断
    • circuitBreaker.sleepWindowInMilliseconds 触发短路的时间值,触发circuitBreaker后该时间段内会拒绝请求(熔断多久之后开始进入半开),默认5000ms
    • circuitBreaker.errorThresholdPercentage 失败率达到多少后跳闸,该时间段内对应请求次数失败率达到多少后进行跳闸,默认50%
    • execution.isolation.strategy 设置隔离策略,THREAD表示线程 SEMAPHORE表示信号量,默认THREAD,有几种情况可以使用SEMAPHORE模式:只想控制并发度 外部的方法已经做了线程隔离 调用的是本地方法或者可靠度非常高、耗时特别小的方法
    • execution.isolation.semaphore.maxConcurrentRequests 如果选用SEMAPHORE,用来设置信号量池的大小,最大并发数,默认10
    • execution.isolation.thread.timeoutInMilliseconds 配置超时时间,默认1000ms
    • execution.timeout.enabled 是否启用超时时间,默认true
    • execution.isolation.thread.interruptOnTimeout 执行超时是否中断,默认true,THREAD模式下有效
    • execution.isolation.thread.interruptOnFutureCancel 执行被取消时是否中断,默认false
    • fallback.isolation.semaphore.maxConcurrentRequests 允许回调方法执行的最大并发数,默认10,SEMAPHORE模式下有效
    • fallback.enabled 服务降级是否启用,默认true
    • circuitBreaker.forceOpen 是否强制开启熔断
    • circuitBreaker.forceClosed 是否强制关闭熔断
    • metrics.rollingStats.timeInMilliseconds 设置统计滚动窗口的长度,单位毫秒
    • metrics.rollingStats.numBuckets 设置统计窗口的桶数量,滚动窗口会被分隔成桶,并且进行滚动,该值设置必须能被metrics.rollingStats.timeInMilliseconds 整除,表示metrics.rollingStats.timeInMilliseconds若值为10000,metrics.rollingStats.numBuckets 值为10,则表示每个桶是1000ms,也就是1s
    • metrics.rollingPercentile.enabled 设置执行时间是否被跟踪,并且计算各个百分比50%、90%等
    • metrics.rollingPercentile.timeInMilliseconds 设置执行时间在滚动窗口中保留时间,用来计算百分比
    • metrics.rollingPercentile.numBuckets 设置rollingPercentile窗口的桶数量,该值必须能被metrics.rollingPercentile.timeInMilliseconds整除
    • metrics.rollingPercentile.bucketSize 设置每个桶保存的执行时间的最大值,如果设置为100,但是有500次请求,则只会计算最近的100次
    • metrics.healthSnapshot.intervalInMilliseconds 采光间隔时间
    • requestCache.enabled 设置是否缓存请求
    • requestLog.enabled 设置HystrixCommand执行事件是否大隐刀HystrixRequestLog中

    如果配置中同时包含Ribbon和Hystrix的超时配置,需要确保Hystrix超时配置长于Ribbon的超时配置

    threadPoolProperties配置

    线程池配置

    HystrixThreadPoolProperties

    • coreSize 线程池的核心线程数,最大并发执行数量

    • allowMaximumSizeToDivergeFromCoreSize 允许扩升到maximumSize

    • maximumSize 只有allowMaximumSizeToDivergeFromCoreSize设置为true才会生效

    • maxQueueSize 最大队列长度,如果为正数,将从队列由SynchronousQueue改为LinkedBlockingQueue

      public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) {
          
          if (maxQueueSize <= 0) {
              return new SynchronousQueue<Runnable>();
          } else {
              return new LinkedBlockingQueue<Runnable>(maxQueueSize);
          }
      }
      
    • keepAliveTimeMinutes keep-live时间

    • queueSizeRejectionThreshold 设置拒绝请求的临界值

    • metrics.rollingStats.timeInMilliseconds

    • metrics.rollingStats.numBuckets

    https://zhhll.icu/2021/框架/微服务/springcloud/熔断/Hystrix断路器/3.Hystrix配置参数/

    相关文章

      网友评论

          本文标题:Spring Cloud全解析:熔断之Hystrix配置参数

          本文链接:https://www.haomeiwen.com/subject/yimlljtx.html