【发布时间】:2016-06-18 19:39:16
【问题描述】:
我正在使用 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 功能。
【问题讨论】:
-
我不确定
@HystrixCommand是否支持返回CompletableFuture。请参阅github.com/Netflix/Hystrix/tree/master/hystrix-contrib/… 了解有效的执行模式。 -
我的 myfeed 项目有一段时间没有更新了,所以以后的版本可能会出现问题。
标签: spring-cloud hystrix