降级逻辑命令嵌套
适用场景:用于fallback逻辑涉及网络访问的情况,如缓存访问。
依赖调用和降级调用使用不同的线程池做隔离,防止上层线程池跑满,影响二级降级逻辑调用
使用demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
   | package com.fuyi.hystrix.command;
  import java.util.concurrent.TimeUnit;
  import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.HystrixCommandProperties;
  public class MultiFallbackCommand extends HystrixCommand<String> {
  	protected MultiFallbackCommand() { 		super(Setter 				.withGroupKey(HystrixCommandGroupKey.Factory.asKey("userServiceGroup")) 				.andCommandPropertiesDefaults( 						// 配置调用依赖超时时间为500,超时后调用降级方法 						HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(500) 					)); 	}
  	 	 	/** 	 * 1. 从缓存获取数据 	 */ 	@Override 	protected String run() throws Exception { 		throw new RuntimeException(); 	}
  	/** 	 * 2. 请求缓存超时,请求db 	 */ 	@Override 	protected String getFallback() { 		System.out.println("fallback thread: " + Thread.currentThread().getName()); 		return new FallbackCommand().execute(); 	} 	 	/** 	 * 嵌套容错包装command 	 */ 	static class FallbackCommand extends HystrixCommand<String> {
  		protected FallbackCommand() { 			super(Setter 					.withGroupKey(HystrixCommandGroupKey.Factory.asKey("userServiceGroup")) 					.andCommandPropertiesDefaults( 							// 配置调用依赖超时时间为500,超时后调用降级方法 							HystrixCommandProperties.Setter().withExecutionIsolationThreadTimeoutInMilliseconds(500) 						)); 		}
  		/** 		 * 3. 请求db超时 		 */ 		@Override 		protected String run() throws Exception { 			throw new RuntimeException(); 		} 		 		/** 		 * 4. 返回默认值 		 */ 		@Override 		protected String getFallback() { 			return "defalut value"; 		} 	} 	 	public static void main(String[] args) { 		MultiFallbackCommand c = new MultiFallbackCommand(); 		String result = c.execute(); 		 		System.out.println(result); 	} }
 
   | 
 
执行结果
1 2
   | fallback thread: hystrix-userServiceGroup-1 defalut value
   |