spring-cloud hystrix CompletableFuture

时间:2016-03-04 21:51:02

标签: spring-cloud hystrix

我正在使用spring-cloud brixton.m5进行一些工作,并尝试让AsyncRestTemplate工作以异步地将多个微服务调用组合成协调服务响应。我找到了spencer gibb的MyFeed github项目,让AsyncRestTemplate在这里使用功能区https://github.com/spencergibb/myfeed/blob/master/myfeed-core/src/main/java/myfeed/core,但是当我有一个使用@HystrixCommand注释的方法返回CompletableFuture时,我遇到了麻烦,就像这样:

public List<Order> getCustomerOrdersFallback(int customerId) {
    return Collections.emptyList();
}

@HystrixCommand(fallbackMethod = "getCustomerOrdersFallback")
@Override
public CompletableFuture<List<Order>> getCustomerOrders(int customerId) {
    ListenableFuture<ResponseEntity<List<Order>>> ordersFuture = restTemplate.exchange(
            "http://order-service/orders?customerId={customerId}", HttpMethod.GET, null,
            new ParameterizedTypeReference<List<Order>>() {
            }, customerId);
    return CompletableFutureUtils.convert(ordersFuture);
}

调用此方法时出现以下异常:

java.lang.ClassCastException: rx.internal.operators.BlockingOperatorToFuture$2 cannot be cast to java.util.concurrent.CompletableFuture
at com.sun.proxy.$Proxy86.getCustomerOrders(Unknown Source) ~[na:na]
at com.build.coordination.service.CustomerServiceImpl.getCustomerOrderView(CustomerServiceImpl.java:50) ~[classes/:na]
at com.build.coordination.customer.CustomerController.getCustomerOrderView(CustomerController.java:45) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72-internal]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_72-internal]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_72-internal]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_72-internal]

如果我从getCustomerOrders中取出@HystrixCommand注释,则调用工作正常,但当然我失去了hystrix功能。

1 个答案:

答案 0 :(得分:0)

我最终从使用CompletableFuture切换到rx.Observable,这是由Hystrix / javanica支持的。