package com.cookie.test; import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; import java.util.Random; /** * author : cxq * Date : 2019/6/28 * * Hystrix超时测试 */ public class HystrixCommandTest extends HystrixCommand<String> { 23 public String name ; public HystrixCommandTest( String name) { // 设置超时时间100ms super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"),100); this.name = name; } @Override protected String run() throws InterruptedException { int excution = new Random().nextInt(200); System.out.println(" 执行时间 :"+excution + "ms"); Thread.sleep(excution); return "Hello "+name ; } @Override protected String getFallback() { return "error ! 降级处理 "; } public static void main(String[] args) { HystrixCommandTest test = new HystrixCommandTest("Ketty"); System.out.println(test.execute()); } }
输出结果展示:
1、 执行时间 :177ms
error ! 降级处理
2、执行时间 :5ms
Hello Ketty