无法在 Hystrix 中应用 "sleepWindowInMilliseconds" 属性
Unable to apply "sleepWindowInMilliseconds" property in Hystrix
您好,我正在尝试在示例程序中使用 Hystrix 模式。
使用以下版本 com.netflix.hystrix:hystrix-core:1.4.21
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 分钟),我认为一旦它收到很少的错误,它就会打开电路,并且一直 return FALLBACK ,它甚至不会尝试调用 invokeService,因此在 60 秒内不会打印 "Inside invokeService"。
有人能解释一下吗,为什么电路没有打开?
还有一个默认值为 20 的参数 circuitBreaker.requestVolumeThreshold
- 这意味着您在统计 windows 中总共需要至少 20 个请求。 window 的大小由 metrics.rollingStats.timeInMilliseconds
配置。
如果您在 window 中没有达到 20 个请求,断路器将不会跳闸。
调查此情况时,您可能还想记录来自 HystrixCommandMetrics
的信息。
您好,我正在尝试在示例程序中使用 Hystrix 模式。 使用以下版本 com.netflix.hystrix:hystrix-core:1.4.21
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 分钟),我认为一旦它收到很少的错误,它就会打开电路,并且一直 return FALLBACK ,它甚至不会尝试调用 invokeService,因此在 60 秒内不会打印 "Inside invokeService"。 有人能解释一下吗,为什么电路没有打开?
还有一个默认值为 20 的参数 circuitBreaker.requestVolumeThreshold
- 这意味着您在统计 windows 中总共需要至少 20 个请求。 window 的大小由 metrics.rollingStats.timeInMilliseconds
配置。
如果您在 window 中没有达到 20 个请求,断路器将不会跳闸。
调查此情况时,您可能还想记录来自 HystrixCommandMetrics
的信息。