自动降级的实现:Hystrix
Hystrix概述
Hystrix是什么
Hystrix可以做什么
使用Hystrix实现自动降级
public class EchoService {
public String echoTime(){
//模拟抛出异常:(1/5的概率抛出异常)
if(new Random().nextInt(10) > 7){
System.out.println("exception:::failure processing echo time");
throw new RuntimeException();
}
//模拟网络耗时(1/2的概率100,1/2的概率1200)
try {
long elapsed = new long[]{100, 1200}[new Random().nextInt(2)];
System.out.println("elapsed:::" + elapsed);
Thread.sleep(elapsed);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "[normal]time:::" + System.currentTimeMillis();
}
}