ITPub博客

首页 > 数据库 > 数据库开发技术 > 实践bug总结-Feign使用Hystrix配置

实践bug总结-Feign使用Hystrix配置

原创 数据库开发技术 作者:HelloTech技术派 时间:2017-12-16 08:00:00 0 删除 编辑
  • 在之前的文章中使用注解 @HystrixCommand 的 fallbackMethod属性实现回调的。

  1. @Slf4j

  2. @Component

  3. public class AdditionCommand {

  4.    @Autowired

  5.    private AdditionClient additionClient;

  6.    @HystrixCommand(fallbackMethod = "fallback")

  7.    public ResponseEntity<ResultData> add(Integer a, Integer b) {

  8.        // 模拟接口异常

  9.        Random random = new Random();

  10.        int r = random.nextInt(10);

  11.        if (r % 2 == 0) {

  12.            throw new RuntimeException();

  13.        }

  14.        return additionClient.add(a, b);

  15.    }

  16.    public ResponseEntity<ResultData> fallback(Integer a, Integer b) {

  17.        ResultData resultData = new ResultData();

  18.        Message message = Message.builder().code(10000).message("xxxx").build();

  19.        resultData.setMessage(message);

  20.        return ResponseEntity.ok(resultData);

  21.    }

  22. }

  • 然后,Feign是以接口的形式进行工作的,本身没有方法体。那么Feign怎么与Hystrix整合呢?


  • 事实上,SpringCloud默认已经为Feign整合了Hystrix,只要添加Hystrix依赖,Feign默认就会用断路器包裹所有方法。

    Feign接口定义:


  1. import com.gemantic.commons.ResultData;

  2. import org.springframework.cloud.netflix.feign.FeignClient;

  3. import org.springframework.http.ResponseEntity;

  4. import org.springframework.web.bind.annotation.GetMapping;

  5. import org.springframework.web.bind.annotation.RequestParam;

  6. /**

  7. * @author Yezhiwei

  8. * @date 17/12/14

  9. */

  10. @FeignClient(name = "springcloud-subtraction-service", fallback = SubtractionClientFallBack.class)

  11. public interface SubtractionClient {

  12.    /**

  13.     * 减法接口

  14.     * @param a

  15.     * @param b

  16.     * @return

  17.     */

  18.    @GetMapping(value = "/sub")

  19.    ResponseEntity<ResultData> sub(@RequestParam("a") Integer a, @RequestParam("b") Integer b);

  20. }

回调实现 SubtractionClient接口:

  1. import com.gemantic.commons.ResultData;

  2. import org.springframework.http.ResponseEntity;

  3. import org.springframework.stereotype.Component;

  4. import org.springframework.web.bind.annotation.RequestParam;

  5. /**

  6. * @author Yezhiwei

  7. * @date 17/12/14

  8. */

  9. @Component

  10. public class SubtractionClientFallBack implements SubtractionClient {

  11.    @Override

  12.    public ResponseEntity<ResultData> sub(@RequestParam("a") Integer a, @RequestParam("b") Integer b) {

  13.        // 这里是实现的回调逻辑!

  14.        return null;

  15.    }

  16. }

  • 好了,开始测试。but but but 问题来了,访问了几次接口,断路器Hystrix一直不起作用, HystrixDashboard也没有收到任何统计信息。感觉Feign中并没有启用Hystrix,通过查看文档发现了Hystrix在Feign中的开关配置, 从Disable HystrixCommands For FeignClients By Default  (https://github.com/spring-cloud/spring-cloud-netflix/issues/1277) 中知道了产生错误的原因,以及为什么要默认关闭Hystrix。


  • 解决方案,在properties文件中增加 feign.hystrix.enabled=true



原文作者:HelloTech技术派;如有侵权,请联系管理员删除。

下一篇: GitAndGitLab介绍
请登录后发表评论 登录
全部评论

注册时间:2017-12-05

  • 博文量
    181
  • 访问量
    73730