1 package com.cookie.test; 2 3 import com.netflix.hystrix.HystrixCommand; 4 5 import com.netflix.hystrix.HystrixCommandGroupKey; 6 7 import java.util.Random; 8 9 /** 10 11 * author : cxq 12 13 * Date : 2019/6/28 14 15 * 16 17 * Hystrix超时测试 18 19 */ 20 21 public class HystrixCommandTest extends HystrixCommand<String> { 22 23 public String name ; 24 25 public HystrixCommandTest( String name) { 26 27 // 设置超时时间100ms 28 29 super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),100); 30 31 this.name = name; 32 33 } 34 35 @Override 36 37 protected String run() throws InterruptedException { 38 39 int excution = new Random().nextInt(200); 40 41 System.out.println(" 执行时间 :"+excution + "ms"); 42 43 Thread.sleep(excution); 44 45 return "Hello "+name ; 46 47 } 48 49 @Override 50 51 protected String getFallback() { 52 53 return "error ! 降级处理 "; 54 55 } 56 57 public static void main(String[] args) { 58 59 HystrixCommandTest test = new HystrixCommandTest("Ketty"); 60 61 System.out.println(test.execute()); 62 63 } 64 65 }
输出结果展示:
1、 执行时间 :177ms
error ! 降级处理
2、执行时间 :5ms
Hello Ketty