0
点赞
收藏
分享

微信扫一扫

Hystrix(2)— 相关配置

认真的老去 2021-09-21 阅读 99

1. SpringBoot如何整合Hystrix

1.1 导入maven依赖

        <!--hystrix官网-->
        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-core</artifactId>
            <version>1.5.18</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-metrics-event-stream</artifactId>
            <version>1.5.18</version>
        </dependency>

        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>1.5.18</version>
        </dependency>

1.2 配置文件

在application.properties配置文件中加入如下配置:

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

将全局的默认超时时间由1s修改为3s,但是配置未起作用。

Archaius 默认支持两种方式来加载本地的配置文件:

  1. 默认情况下,Archaius 默认会加载classpath的config.properties文件。
  2. 在程序启动后,加入如下参数
    -Darchaius.configurationSource.additionalUrls=file:///apps/myapp/application.properties

1.3 如何使用

若单纯使用SpringBoot整合Hystrix,若想配置文件或者注解可以生效,必须将切面声明到Spring容器中。

    @Bean
    public HystrixCommandAspect hystrixCommandAspect(){
        return new HystrixCommandAspect();
    }

    @Bean
    public HystrixCacheAspect hystrixCacheAspect(){
        return  new HystrixCacheAspect();
    }

使用注解的方式,为方法添加hystrix断路器。

    @HystrixCommand(
            fallbackMethod = "fallConfigOfRemote"
            ,commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
            }
            )
    public String getConfigOfRemote(String id) {
        String url = "http://localhost:8001/getInfo";
        String doPost = "S";
        try {
            //调用远程服务,url,参数,socket超时时间
            doPost = HttpClientUtils.doPost(url, id, 10000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return doPost;
    }

    private String fallConfigOfRemote(String id) {
        log.info("备选方案...");
        return "F";
    }

在resources\config.properties中进行配置:

#配置默认的超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
#配置特定commandKey的超时时间(优先级最高)
hystrix.command.getConfigOfRemote.execution.isolation.thread.timeoutInMilliseconds=4000

使用Hystrix后,配置均会在com.netflix.hystrix.AbstractCommand#AbstractCommand类中组装。

注:需要注意的是,虽然在3个地方设置了 超时时间。但是只有4000ms的超时时间生效。

1. hystrix配置的优先级是怎么样的呢?
2. 如何为某个方法配置不同的hystrix策略?

2. Hystrix配置

2.1 Hystrix配置的优先级

优先级从低到高的配置:

  1. 内置全局属性默认值:写死在代码中的值;
  2. 动态全局默认属性:通过配置文件配置全局的值;
  3. 内置实例默认值:写死在代码中的实例的值;
  4. 动态配置实例属性:通过配置文件配置特定实例的值;

注:全局配置是default的配置,而实例配置为commandKey配置。

2.2 CommandKey和CommandGroup

默认情况下,Hystrix会使用类名作为CommandGroup,会使用方法名作为CommandKey。可以使用commandKey进行个性化的配置。参考【1.3 如何使用】

2.3 详细配置

2.3.1 Execution

配置 默认值
execution.isolation.strategy THREAD

表示HystrixCommand.run()的执行时的策略,有以下两种策略(色魔佛)

配置 属性
THREAD(默认值) 在单独的线程上执行,并发请求受线程池中的线程数限制
SEMAPHORE 在调用线程上执行,并发请求量受信号量计数限制
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.strategy=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.strategy=…
配置 默认值
execution.isolation.thread.timeoutInMilliseconds 1000ms
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds=…
配置 默认值
execution.timeout.enabled true
// 设置所有实例的默认值
hystrix.command.default.execution.timeout.enabled=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.timeout.enabled=…
配置 默认值
execution.isolation.thread.interruptOnTimeout true
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout=…
配置 默认值
execution.isolation.thread.interruptOnCancel false
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.thread.interruptOnCancel=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnCancel
配置 默认值
execution.isolation.semaphore.maxConcurrentRequests 10
// 设置所有实例的默认值
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests=…

2.3.2 Fallback

以下属性控制HystrixCommand.getFallback()如何执行,这些属性对隔离策略THREADSEMAPHORE都起作用。

此属性设置从调用线程允许Hystrix.getFallback()方法允许的最大并发请求数,如果达到最大的并发量,则接下来的请求都会被拒绝并且抛出异常。

配置 默认值
fallback.isolation.semaphore.maxConcurrentRequests 10
// 设置所有实例的默认值
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.fallback.isolation.semaphore.maxConcurrentRequests
配置 默认值
hystrix.command.default.fallback.enabled true
// 设置所有实例的默认值
hystrix.command.default.fallback.enabled=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.fallback.enabled=…

2.3.3 断路器

控制断路器的行为。

配置 默认值
circuitBreaker.enabled true
// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.enabled=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.enabled=…

默认值20。若是在10s(窗口时间)内,只收到19个请求且都失败了,则断路器也不会开启。

// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.requestVolumeThreshold=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.requestVolumeThreshold=…

断路器跳闸后,在此值的时间内,hystrix会拒绝新的请求,只有过了这个时间,断路器才会打开闸门。默认值5000ms

// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.sleepWindowInMilliseconds=…

设置失败百分比的阈值,如果失败比率超过这个值,则断路器跳闸并且进入fallback状态。默认值:50

// 设置所有实例的默认值
hystrix.command.default.circuitBreaker=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage=…

如果这个属性true强制断路器进入开路(跳闸)状态,它将拒绝所有请求。
此属性优先于circuitBreaker.forceClosed。默认值false

// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.forceOpen=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen=…

如果设置true,则强制使断路器进行关闭状态,此时会允许执行所有请求,无论是否失败的次数达到circuitBreaker.errorThresholdPercentage值。默认值:false。

// 设置所有实例的默认值
hystrix.command.default.circuitBreaker.forceClosed=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.circuitBreaker.forceClosed=…

2.3.4 Metrics (度量)

[麦锤死]捕获和HystrixCommand以及HystrixObservableCommand执行信息相关的配置属性。

Hystrix保留断路器使用和发布指标的时间。默认值:10000

// 设置所有实例的默认值
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.timeInMilliseconds=10000

metrics.rollingStats.timeInMilliseconds % metrics.rollingStats.numBuckets == 01
如:10000/10、10000/20是正确的配置,但是10000/7错误的。默认值:10
注:在高并发的环境里,每个桶的时间长度建议大于100ms。

// 设置所有实例的默认值
hystrix.command.default.metrics.rollingStats.numBuckets=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.metrics.rollingStats.numBuckets=…

2.3.5 Request Context

该属性控制HystrixCommand使用到的Hystrix的上下文。

默认值:true

// 设置所有实例的默认值
hystrix.command.default.requestCache.enabled=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.requestCache.enabled=…

表示是否开启日志,打印执行HystrixCommand的情况和事件。默认值true

// 设置所有实例的默认值
hystrix.command.default.requestLog.enabled=…
// 设置实例HystrixCommandKey的此属性值
hystrix.command.HystrixCommandKey.requestLog.enabled=…

2.3.6 Collapser Properties

设置请求合并请求。

默认值:Integer.MAX_VALUE

// 设置所有实例的默认值
hystrix.collapser.default.maxRequestsInBatch=…
// 设置实例HystrixCommandKey的此属性值
hystrix.collapser.HystrixCollapserKey.maxRequestsInBatch=…

默认值:10

// 设置所有实例的默认值
hystrix.collapser.default.timerDelayInMilliseconds=…
// 设置实例HystrixCommandKey的此属性值
hystrix.collapser.HystrixCollapserKey.timerDelayInMilliseconds=…

是否对HystrixCollapser.execute() 和 HystrixCollapser.queue()开启请求缓存
默认值:true

// 设置所有实例的默认值
hystrix.collapser.default.requestCache.enabled=…
// 设置实例HystrixCommandKey的此属性值
hystrix.collapser.HystrixCollapserKey.requestCache.enabled=…

2.3.7 ThreadPool配置

默认值:10

// 设置所有实例的默认值
hystrix.threadpool.default.coreSize=…
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.coreSize=…

在1.5.9中添加。此属性设置最大线程池大小。这是在不开始拒绝HystrixCommands的情况下可以支持的最大并发数量。请注意,此设置仅在您设置时生效allowMaximumSizeToDivergeFromCoreSize。在1.5.9之前,核心和最大尺寸始终相等。
默认值:10

// 设置所有实例的默认值
hystrix.threadpool.default.maximumSize=…
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.maximumSize=…

设置最大的BlockingQueue队列的值。如果设置-1,则使用SynchronousQueue队列(无限队列)。如果设置正数,则使用LinkedBlockingQueue队列。默认值:-1

// 设置所有实例的默认值
hystrix.threadpool.default.maxQueueSize=…
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.maxQueueSize=…

因为maxQueueSize值不能被动态修改,所有通过设置此值可以实现动态修改等待队列长度。即等待的队列的数量大于queueSizeRejectionThreshold时(但是没有达到maxQueueSize值),则开始拒绝后续的请求进入队列。
默认值:5
注:如果设置-1,则属性不启作用。

// 设置所有实例的默认值
hystrix.threadpool.default.queueSizeRejectionThreshold=…
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.queueSizeRejectionThreshold=…

设置线程空闲多长时间后,释放(maximumSize-coreSize )个线程。默认值1(分钟)

// 设置所有实例的默认值
hystrix.threadpool.default.keepAliveTimeMinutes=…
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.keepAliveTimeMinutes=…

设置allowMaximumSizeToDivergeFromCoreSize值为true时,maximumSize才有作用
默认值:false

// 设置所有实例的默认值
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=…
// 设置实例HystrixCommandKey的此属性值
hystrix.threadpool.HystrixThreadPoolKey.allowMaximumSizeToDivergeFromCoreSize=…

推荐阅读

官网——Hystrix的参数配置
Hystrix常用功能介绍
Hystrix源码解析--从原生的lib开始使用hystrix(一)

举报

相关推荐

0 条评论