Hystrix隔离策略

匿名 (未验证) 提交于 2019-12-03 00:34:01

   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
文章来源: Hystrix隔离策略
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!