Hystrix 异步中的巨坑

研究 Hystrix 的异步执行,每次调用 100% 进入断路器,debug后发现有一个异常直接被 Hystrix 处理了而没有输出到控制台,真的是巨坑啊...该异常内容:

return type of 'queryAll' method should be com.netflix.hystrix.contrib.javanica.command.AsyncResult.

仔细检查代码,我返回的是 AsyncResult 啊....再仔细看,我擦,不是同一个 AsyncResult, 我用的是 org.springframework.scheduling.annotation.AsyncResult
网上的教程里的实例代码都把 import 省了,也不具体说明是哪个 AsyncResult,导入包的时候也没仔细看,想着既然用的spring 的 @Async 注解,就顺手选了 spring 的 AsyncResult, 最后实测,没必要使用 @Async 注解,下面附上个例子(使用spring cloud, 客户端使用 rest+ribbon, 使用 Hystrix 作为断路器):

import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.command.AsyncResult;

@Service
public class TestAsync {

    @Autowired
    private RestTemplate restTemplate;
    
    @HystrixCommand(fallbackMethod = "testAsyncError")
    public Future<String> testAsync(){
    return new AsyncResult<List<MongoDBTest>>() {
            @Override
            public List<MongoDBTest> invoke() {
                return restTemplate.postForObject("http://test-service/testAsync", null, String.class);
            }
        };
    }
    
    public String testAsyncError(String abc){
    System.out.println("=================进入断路器了==================");
    return null;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 七天假期就这么的结束了,加了两天班,浑浑噩噩过了剩下的五天。想到明天要工作,身体就自动产生了一种抗体,使我浑身不适...
    Domo2T阅读 402评论 0 1
  • 在昨天,就在昨天,复联3正式在中国上映了。 虽然我还没有去看️️️️️️️️️️️️️️️️️️️️,但是,没有...
    好呀红书阅读 287评论 0 1
  • 原题目:怪文章的生存方式 也不怕什么了,最奇怪的就是一个奇怪的开头,一个奇怪的结尾,造就的是一张张茫然的脸和...
    Ruby安若阅读 358评论 2 5
  • 蜗牛 2.3李明冬 今天是周六的早上,我抓了一只小蜗牛。 我发现蜗牛的眼睛是长在触角上的,它的嘴巴歪歪的,四根触角...
    储能阅读 298评论 0 2