您好我想在我的示例程序中使用Hystrix模式。 使用以下版本com.netflix.hystrix:椎核:1.4.21無法在Hystrix中應用「sleepWindowInMilliseconds」屬性
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandProperties;
import java.util.GregorianCalendar;
import java.util.Map;
public class ServiceInvoker extends HystrixCommand<String> {
Map<String, String> serviceParams;
public String invokeService(Map<String, String> serviceParams){
System.out.println("Inside invokeService");
//Induce processing delay START
long currentTime = GregorianCalendar.getInstance().getTimeInMillis();
long timeNow = 0;
long bound = 3000;
while(timeNow < (currentTime+bound)){
timeNow = GregorianCalendar.getInstance().getTimeInMillis();
}
//Induce processing delay END
return "Service Invoked";
}
public ServiceInvoker(Map<String, String> params){
super(Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey("MYKEY"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withCircuitBreakerSleepWindowInMilliseconds(60000)
.withExecutionTimeoutInMilliseconds(2000)
.withCircuitBreakerErrorThresholdPercentage(5))
);
this.serviceParams=params;
}
@Override
protected String run() throws Exception {
return invokeService(serviceParams);
}
@Override
protected String getFallback() {
System.out.println("Inside FallBack");
return "FALLBACK";
}
public static void main(String args[]) throws InterruptedException {
while(true) {
ServiceInvoker si = new ServiceInvoker(null);
String op = si.execute();
System.out.println("output="+op);
Thread.sleep(100);
}
}
}
當我運行上面的代碼我多次得到以下不停。
Inside invokeService
Inside FallBack
output=FALLBACK
我想,既然我已經設置了withCircuitBreakerErrorThresholdPercentage爲5%和withCircuitBreakerSleepWindowInMilliseconds在60000(1分鐘),我認爲,一旦收到幾個錯誤,它會打開電路,就回到回退的所有時間,它甚至不會嘗試調用invokeService,因此不會打印「Inside invokeService」60秒。 有人可以請說一說,爲什麼電路沒有打開?