0

您好我正在尝试在我的示例程序中使用 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 分钟),我认为一旦收到少量错误,它将打开电路并始终返回 FALLBACK,它甚至不会尝试调用 invokeService因此在 60 秒内不会打印“Inside invokeService”。有人可以对此有所了解,为什么电路没有打开?

4

1 回答 1

4

还有circuitBreaker.requestVolumeThreshold一个默认值为 20 的参数 - 这意味着您在统计窗口中总共需要至少 20 个请求。窗口的大小由 配置metrics.rollingStats.timeInMilliseconds

如果您在窗口中没有达到 20 个请求,断路器将不会跳闸。

HystrixCommandMetrics在调查此场景时,您可能还想记录信息。

于 2015-12-17T21:24:04.547 回答