继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Spring01——SpringIoC容器

慕神8447489
关注TA
已关注
手记 1134
粉丝 172
获赞 955

研究 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;@Servicepublic 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;
    }
}



作者:邪影oO
链接:https://www.jianshu.com/p/3496a3474e71


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP