我正在使用spring-cloud brixton.m5做一些工作,并尝试让AsyncRestTemplate异步地将多个微服务调用组合到一个协调服务响应中。我找到了spencer gibb的MyFeed github项目,该项目让AsyncRestTemplate在https://github.com/spencergibb/myfeed/blob/master/myfeed-core/src/main/java/myfeed/core中使用ribbon,但是当我有一个用@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]如果我把@HystrixCommand注释从getCustomerOrders中去掉,调用就能正常工作,但当然我会失去hystrix的功能。
发布于 2016-03-12 08:32:22
我最终从使用CompletableFuture切换到了rx.Observable,这是Hystrix/javanica支持的。
https://stackoverflow.com/questions/35806735
复制相似问题