Hystrix隔离策略

Hystrix隔离方案

   Hystrix提供了两种隔离的解决方案:线程隔离和信号量隔离,默认为线程隔离。

private static final ExecutionIsolationStrategy default_executionIsolationStrategy = ExecutionIsolationStrategy.THREAD;
  public static enum ExecutionIsolationStrategy {
        THREAD, SEMAPHORE
    }

这里写图片描述

线程隔离

  思路:请求被传入单独的线程池进行处理;
  优点:存在超时机制;
  缺点:线程切换和调度存在一定开销;

示例代码

package com.cainiao.test.test.hystrix;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;

public class CommandHelloWorld extends HystrixCommand<Void> {

    public CommandHelloWorld() {
        super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("g1"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("k1"))
                // 设置线程池属性
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("ThreadPool"))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(5)
                        .withKeepAliveTimeMinutes(60).withMaxQueueSize(5).withQueueSizeRejectionThreshold(2))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        // 开启熔断机制
                        .withCircuitBreakerEnabled(true)
                        // 熔断器开启3s后关闭
                        .withCircuitBreakerSleepWindowInMilliseconds(3000)
                        // 设置隔离策略
                        .withExecutionIsolationStrategy(ExecutionIsolationStrategy.THREAD)));
    }

    @Override
    protected Void run() {
        System.out.println("Hello world!");
        return null;
    }

}

信号量隔离

  思路:请求被调用前,首先获取信号量,只有获得信号量的请求才会被处理;
  优点:没有线程切换的开销;
  缺点:没有超时机制;

package com.cainiao.test.test.hystrix;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy;

public class CommandHelloWorld extends HystrixCommand<Void> {

    public CommandHelloWorld() {
        super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("g1"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("k1"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                        // 开启熔断机制
                        .withCircuitBreakerEnabled(true)
                        // 熔断器开启3s后关闭
                        .withCircuitBreakerSleepWindowInMilliseconds(3000)
                        // 设置隔离策略
                        .withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE)));
    }

    @Override
    protected Void run() {
        System.out.println("Hello world!");
        return null;
    }

}

The advantage of the thread pool approach is that requests that are passed to C can be timed out, something that is not possible when using semaphores.

参考:

  1. https://stackoverflow.com/questions/30391809/what-is-bulkhead-pattern-used-by-hystrix
  2. https://github.com/Netflix/Hystrix/wiki/How-it-Works#Isolation

猜你喜欢

转载自blog.csdn.net/yangguosb/article/details/80803147
今日推荐