Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 每个命令的Hystrix类?_Java_Hystrix - Fatal编程技术网

Java 每个命令的Hystrix类?

Java 每个命令的Hystrix类?,java,hystrix,Java,Hystrix,我的应用程序有一组可配置的运行时外部端点可供调用: /foo /bar 和用于使用apache HttpClient进行http调用 我想在这里使用Hystrix包装HttpClient,但我想为每个端点分离断路器,这样,如果一个端点出现故障,就不会影响另一个端点 我可以通过上两门课来做到这一点: FooCommand extends HystrixCommand<...> { } BarCommand extends HystrixCommand<...> { }

我的应用程序有一组可配置的运行时外部端点可供调用:

/foo
/bar
和用于使用apache HttpClient进行http调用

我想在这里使用Hystrix包装HttpClient,但我想为每个端点分离断路器,这样,如果一个端点出现故障,就不会影响另一个端点

我可以通过上两门课来做到这一点:

FooCommand extends HystrixCommand<...> {
}

BarCommand extends HystrixCommand<...> {
}

如果1失败,2仍将被单独执行和处理

您可以在构造函数中设置命令键。像这样的

public class HttpCommand extends HystrixCommand<HttpResponse> {

  private final HttpClient httpClient;
  private final HttpRequestBase request;

  public HttpCommand(String commandKey, HttpClient httpClient, HttpRequestBase request) {
    super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HttpGroup"))
        .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)));
    this.httpClient = httpClient;
    this.request = request;
  }

  @Override
  protected HttpResponse run() throws Exception {
    return httpClient.execute(request);
  }

}
啊,你可能是对的。给出了一个解决类似问题的示例。我会重新测试,然后再给你回复
new MyHystrixHttpClient("/foo").execute(); // (1)
new MyHystrixHttpClient("/bar").execute(); // (2)
public class HttpCommand extends HystrixCommand<HttpResponse> {

  private final HttpClient httpClient;
  private final HttpRequestBase request;

  public HttpCommand(String commandKey, HttpClient httpClient, HttpRequestBase request) {
    super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("HttpGroup"))
        .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)));
    this.httpClient = httpClient;
    this.request = request;
  }

  @Override
  protected HttpResponse run() throws Exception {
    return httpClient.execute(request);
  }

}