Feign使用Hystrix

  因为feign已经依赖了hystrix,所以可以直接使用,无需添加再次添加依赖。

  1、使用@FeignClient注解中的fallback属性指定回调类

package com.daqsoft;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; /**
* @Description Created by liaoxx on 2017-6-12.
*/
@FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class)
public interface ComputerClient {
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}

  2、创建回调类ComputeClientHystrix,实现@FeignClient的接口,此时实现的方法就是对应@FeignClient接口中映射的fallback函数

package com.daqsoft;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam; /**
* @Description Created by liaoxx on 2017-6-13.
*/
@Component
public class ComputeClientHystrix implements ComputerClient {
@Override
public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
return -1;
}
}

  3、web调用

package com.daqsoft;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; /**
* @Description Created by liaoxx on 2017-6-12.
*/
@RestController
public class CustomController { @Autowired
private ComputerClient computerClient; @RequestMapping(value = "/add", method = RequestMethod.GET)
public Integer add(){
return computerClient.add(10,20); }
}

  4、启动服务,访问http://localhost:3333/add

  

  报错,无法进入回调

  5、修改配置文件,添加属性

spring.application.name=ribbon-consumer

server.port=3333
#服务注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/ #开启hystrix支持
feign.hystrix.enabled=true

  6、再次启动服务,访问http://localhost:3333/add (正常访问回调方法)

  

  

最新文章

  1. 十分钟玩转 jQuery、实例大全
  2. 详解Paint的setShader(Shader shader)
  3. DELL R710服务器做RAID5磁盘阵列图文教程
  4. UISlide属性
  5. 存储过程Oracle学习(一)
  6. Halcon学习笔记之缺陷检测(二)
  7. IE6、火狐不支持a:visited
  8. Unity屏幕射线碰撞
  9. sublime 前端开发工具
  10. 配置php网页显示错误
  11. Problem A
  12. 联想G510F1F2..功能键和FN+功能键反过来
  13. java容器-Map
  14. 🍓 移动端调试工具之vconsole的使用~ 🍓
  15. 算法之Python实现 - 003 : 换钱的方法数
  16. 「TJOI2015」概率论 解题报告
  17. 容器网络——从CNI到Calico
  18. Introduction To Machine Learning Self-Evaluation Test
  19. Vue.js基础(二)
  20. Delphi 之 菜单组件(TMainMenu)

热门文章

  1. Rancher 2.0 简单使用 重要部分截取
  2. Sublime Text3快捷键大全
  3. Django contenttypes 应用
  4. xpath&css选择器
  5. 注解@ResponseBody的作用
  6. boost asio 网络聊天 代码修改学习
  7. 创建.NET core的守护进程
  8. Knockout.js组件系统的详解之(一) - 组件的定义和注册
  9. 测试快速关闭innodb的方法
  10. ssm框架中处理json格式的数据步骤