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;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值