1、熔断例子
import java.util.concurrent.TimeUnit;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;
/**
* 测试4-熔断测试
*
*/
public class CommandHelloWorld5 extends HystrixCommand<String>{
private Integer id;
public CommandHelloWorld5(Integer id) {
super(setter());
this.id = id;
}
private static Setter setter() {
return ApiSetter.setter("getNum");
}
@Override
protected String run() throws Exception {
System.out.print("id:" + id);
if (id % 2 == 0 && id <= 10) { //让小于等于10的偶数返回
return "running run():" + id;
} else { //让奇数或大于10的数进入fallback
TimeUnit.MILLISECONDS.sleep(200);
return " XXX ";
}
}
@Override
protected String getFallback() {
return " ====== CircuitBreaker fallback" + id + " ,是否进入熔断:" + super.isCircuitBreakerOpen();
}
public static void main(String[] args) throws Exception {
for(int i = 0; i < 30; i++) {
System.out.println(new CommandHelloWorld5(i).execute());
// /*Future<String> future = new CommandHelloWorld5(i).queue();
// System.out.println(future.get());*/
}
}
private static class ApiSetter {
/**
*
* @param commandKeyName 服务标识名称
* @param threadPoolKeyName 线程程名称
* @return
*/
public static Setter setter(