首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >spring-cloud hystrix CompletableFuture

spring-cloud hystrix CompletableFuture
EN

Stack Overflow用户
提问于 2016-03-05 05:51:02
回答 1查看 2.3K关注 0票数 2

我正在使用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的方法时,我遇到了麻烦,如下所示:

代码语言:javascript
复制
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);
}

当调用此方法时,我得到以下异常:

代码语言:javascript
复制
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的功能。

EN

回答 1

Stack Overflow用户

发布于 2016-03-12 08:32:22

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35806735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档