【问题标题】:spring-cloud hystrix CompletableFuturespring-cloud hystrix CompletableFuture
【发布时间】: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 功能。

【问题讨论】:

标签: spring-cloud hystrix


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-03-31
    • 2015-05-31
    • 2018-07-21
    • 2016-12-08
    • 2018-07-18
    • 2018-10-05
    • 2018-07-09
    • 2019-01-21
    • 2017-08-22
    相关资源
    最近更新 更多