【问题标题】:Is Hystrix able to open a circuit depending on method parameters?Hystrix 是否能够根据方法参数打开电路?
【发布时间】:2016-03-24 03:43:18
【问题描述】:

如果我有以下 Hystrix 命令:

public class TimeoutDependingOnParam extends HystrixCommand<String> {

    private final String name;

    public TimeoutDependingOnParam (String name) {
        super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
        this.name = name;
    }

    @Override
    protected String run() {

        if (name.equals("Looong")) {
           waitABillionYears();
        }

        return "Hello " + name + "!";
    }
}

调用者:

// no timeout for "Quick"
String s1 = new TimeoutDependingOnParam("Quick").execute();

// timeout for "Looong"
String s2 = new TimeoutDependingOnParam("Looong").execute();

如果 Hystrix 因“Looong”调用超时而打开电路,这是否意味着“Quick”调用将被打开

【问题讨论】:

    标签: java hystrix circuit-breaker


    【解决方案1】:

    只要两者都具有与您的示例中相同的命令键,基本上可以。但要让断路器分闸还有更多条件as stated in the documentation about the Circuit Breaker

    您可以实现两个不同的命令,也可以根据参数在构造函数中设置 CommandKey。 This is an extract from the documentation:

    public CommandHelloWorld(String name) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
                .andCommandKey(HystrixCommandKey.Factory.asKey("HelloWorld")));
        this.name = name;
    }
    

    【讨论】:

    • 谢谢@ahus,所以我想我可以像publicTimeoutDependingOnParam (String name) {super(HystrixCommandGroupKey.Factory.asKey("TDPGroup")).andCommandKey(HystrixCommandKey.Factory.asKey("TDP" + name)));this.name = name;}那样做一些思考
    猜你喜欢
    • 2020-10-29
    • 2017-05-29
    • 1970-01-01
    • 2020-04-13
    • 2015-05-23
    • 2012-09-02
    • 2018-01-14
    • 1970-01-01
    • 2016-12-19
    相关资源
    最近更新 更多