Feign使用Hystrix开发步骤

1、导入依赖spring-cloud-starter-hystrix

2、消费启动类开启@EnableCircuitBreaker

3、配置yml文件feign.hystrix.enabled=true

4、实现FeignClient接口或FallbackFactory接口
4.1、实现FeignClient接口
4.2、实现FallbackFactory接口

5、@FeignClient注解配置fallback参数

1、导入依赖spring-cloud-starter-hystrix

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

2、消费启动类开启@EnableCircuitBreaker

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix //开启Hystrix支持
public class RibbonConsumerApplication { public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
} }

3、配置yml文件feign.hystrix.enabled=true

server:
port: 8808
feign:
hystrix:
#开启feign的hystrix支持,默认是false
enabled: true

4、实现FeignClient接口或FallbackFactory接口
4.1、实现FeignClient接口
TestClient1.java

//fallback 指定一个回退类,这个类必须实现@FeignClient声明的接口,并且在spring context中
@FeignClient(value="cloud-producer",
configuration=FeignClientsConfiguration.class,
fallback=TestClient1FallBack.class)
public interface TestClient1 { @GetMapping(value = "/get/{id}")
public String get(@PathVariable("id") String id); @RequestMapping(value = "/getuser/{id}")
public User getUser(@PathVariable("id") String id); @RequestMapping(value = "/getuser2", method = RequestMethod.GET)
public User getUser2(User user); @RequestMapping(value = "/postuser")
public User postUser(@RequestBody User user); @RequestMapping(value = "/postuser")
public User postUser2(User user); @RequestMapping(value = "/postuser", method = RequestMethod.GET)
public User postUser3(User user); @GetMapping(value = "/listAll")
List<User> listAll(); }

TestClient1FallBack.java

@Component
public class TestClient1FallBack implements TestClient1 {
@Override
public String get(String id) {
return "feign hystrix fallback for get method";
} @Override
public User getUser(String id) {
User errorUser = new User();
errorUser.setId("getUser.errorId");
errorUser.setName("getUser.errorName");
return errorUser;
} @Override
public User getUser2(User user) {
User errorUser = new User();
errorUser.setId("getUser2.errorId");
errorUser.setName("getUser2.errorName");
return errorUser;
} @Override
public User postUser(User user) {
User errorUser = new User();
errorUser.setId("postUser.errorId");
errorUser.setName("postUser.errorName");
return errorUser;
} @Override
public User postUser2(User user) {
User errorUser = new User();
errorUser.setId("postUser2.errorId");
errorUser.setName("postUser2.errorName");
return errorUser;
} @Override
public User postUser3(User user) {
User errorUser = new User();
errorUser.setId("postUser3.errorId");
errorUser.setName("postUser3.errorName");
return errorUser;
} @Override
public List<User> listAll() {
User errorUser = new User();
errorUser.setId("listAll.errorId");
errorUser.setName("listAll.errorName");
ArrayList<User> list = new ArrayList<User>();
list.add(errorUser);
return list;
}
}

Test1Controller.java

@RestController
public class Test1Controller { @Autowired
private TestClient1 testClient1; @GetMapping("/feign1/get/{id}")
public String get(@PathVariable String id) {
String result = testClient1.get(id);
return result;
} @GetMapping("/feign1/getuser/{id}")
public User getUser(@PathVariable String id) {
User result = testClient1.getUser(id);
return result;
} @GetMapping("/feign1/getuser2")
public User getUser2(User user) {
User result = testClient1.getUser2(new User());
return result;
} @GetMapping("/feign1/listAll")
public List<User> listAll() {
return testClient1.listAll();
} @PostMapping("/feign1/postuser")
public User postUser(@RequestBody User user) {
User result = testClient1.postUser(user);
return result;
} @PostMapping("/feign1/postuser2")
public User postUser2(@RequestBody User user) {
User result = testClient1.postUser2(user);
return result;
} @PostMapping("/feign1/postuser3")
public User postUser3(@RequestBody User user) {
User result = testClient1.postUser3(user);
return result;
} }

4.2、实现FallbackFactory接口
TestClient3.java

//fallbackFactory 指定一个fallback工厂,与指定fallback不同, 此工厂可以用来获取到触发断路器的异常信息,TestClientFallbackFactory需要实现FallbackFactory类
@FeignClient(value="mima-cloud-producer",configuration=FeignClientsConfiguration.class,fallbackFactory=TestClien3tFallbackFactory.class)
public interface TestClient3 { @RequestMapping(value="/get/{id}",method=RequestMethod.GET)
public String get(@PathVariable("id") String id); @RequestMapping(value = "/getuser/{id}")
public User getUser(@PathVariable("id") String id); }

TestClien3tFallbackFactory.java——优点是可以获取到异常信息

//FallbackFactory的优点是可以获取到异常信息
@Component
public class TestClien3tFallbackFactory implements FallbackFactory<TestClient3> { @Override
public TestClient3 create(Throwable cause) {
return new TestClient3() {
@Override
public String get(String id) {
return "get trigger hystrix open! reason:"+cause.getMessage();
} @Override
public User getUser(String id) {
User errorUser = new User();
errorUser.setId("getUser.errorId");
errorUser.setName("getUser.errorName");
return errorUser;
}
};
} }

Test3Controller.java

@RestController
public class Test3Controller { @Autowired
private TestClient3 testClient3; @GetMapping("/feign3/get/{id}")
public String get(@PathVariable String id) {
String result = testClient3.get(id);
return result;
} @GetMapping("/feign3/getuser/{id}")
public User getuser(@PathVariable String id) {
User result = testClient3.getUser(id);
return result;
} }

5、@FeignClient注解配置fallback参数

Feign使用Hystrix的更多相关文章

  1. 在dropwizard中使用feign,使用hystrix

    前言 用惯了spring全家桶之后,试试dropwizard的Hello World也别有一帆风味.为了增强对外访问API的能力,需要引入open feign.这里简单在dropwizard中使用fe ...

  2. SpringCloud Feign对Hystrix(断路由)的支持

    第一步:首先开启Feign对Hystrix的支持,在properties文件中添加以下配置: feign.hystrix.enabled=true. 第二步:在上一篇Feign的基础上添加Hystri ...

  3. Feign 与 Hystrix

    Feign 与 Hystrix Feign是一个声明式的web服务客户端,它使得web服务调用非常的简单,当我们使用Feign时,Spring Cloud 整合了Ribbon和Eureka,从而为我们 ...

  4. spring cloud: Hystrix(四):feign类似于hystrix的断容器功能:fallback

    spring cloud: Hystrix(四):feign使用hystrix @FeignClient支持回退的概念:fallback方法,这里有点类似于:@HystrixCommand(fallb ...

  5. Spring Cloud(Dalston.SR5)--Feign 与 Hystrix 断路器整合

    创建项目 要使 Feign 与 Hystrix 进行整合,我们需要增加 Feign 和 Hystrix 的依赖,修改 POM.xml 中增加以下依赖项如下: <?xmlversion=" ...

  6. springcloud微服务实战:Eureka+Zuul+Feign/Ribbon+Hystrix Turbine+SpringConfig+sleuth+zipkin

    相信现在已经有很多小伙伴已经或者准备使用springcloud微服务了,接下来为大家搭建一个微服务框架,后期可以自己进行扩展.会提供一个小案例: 服务提供者和服务消费者 ,消费者会调用提供者的服务,新 ...

  7. springcloud(九)-Feign使用Hystrix

    前言 上一篇我们使用注解@HystrixCommond的fallbackMethod属性实现回退.然而,Feign是以接口形式工作的,它没有方法体,上一篇讲解的方式显然不适用于Feign. 那么Fei ...

  8. 004声明式服务调用Feign & 断路器Hystrix

    1.POM配置 和普通Spring Boot工程相比,添加了Eureka Client.Feign.Hystrix依赖和Spring Cloud依赖管理 <dependencies> &l ...

  9. SpringCloud系列十六:Feign使用Hystrix

    1. 回顾 上文讲解了使用注解@HystrixCommand的fallbackMethod属性实现回退.然而,Feign是以接口形式工作的, 它没有方法体,前文讲解的方式显然不适用与Feign. 事实 ...

随机推荐

  1. 使用GO开发ChainCode

    本来不会GO,最近突击学了些GO的基础,就开始搞chaincode了. 首先给大家推荐一个非常好的Hyperldeger Fabric项目 marble:https://github.com/ibm- ...

  2. 【java】:Junit

    创建单元测试文件 点击创建测试文件的目录,比如,我要在control目录下添加一个测试类,点击control文件夹 右键->new->other->junit test case 下 ...

  3. SimpleDateFormat的parse(String str)方法的用法

    SimpleDateFormate 中的parse 方法可以将string类型的字符串转换成特定的date的特定类型.

  4. virtual和abstract的区别

    virtual和abstract都是用来修饰父类的,前面不能用private私有,要不然就会出现编译错误:虚拟方法或抽象方法是不能私有的.  毕竟加上virtual或abstract就是让子类重新定义 ...

  5. Struts2学习第四天——全局结果,动态结果及异常映射

    1.异常映射的配置 当Action方法出错时Struts会返回异常错误信息页面,这种页面方式是不友好的,可以使用try-catch捕捉异常,然后在catch块中返回对应的错误页面.这种为单个<a ...

  6. Python简介及环境安装

    Python 官网传送门 Python是一种面向对象的解释性计算机程序设计语言. Python 2.7将于2020年1月1日终止支持,本笔记基于Python3. pip pip 是一个现代的,通用的 ...

  7. 卷积在图像处理中的应用(转自https://medium.com/@irhumshafkat/intuitively-understanding-convolutions-for-deep-learning-1f6f42faee1)

    直观理解深度学习的卷积 探索使他们工作的强大视觉层次   近年来强大且多功能的深度学习框架的出现使得可以将卷积层应用到深度学习模型中,这是一项非常简单的任务,通常可以在一行代码中实现. 然而,理解卷积 ...

  8. uart通讯协议

    本次设计的源码在http://download.csdn.net/detail/noticeable/9912383 下载 实验目的:通过uart通讯协议的编写,了解FPGA的通讯协议编写的方法. 实 ...

  9. Linux/unix 查看端口占用

    有的时候我们想找到某个端口被那个程序.程序占用,然后 kill 掉他,所以今天就来探讨一下. 1.netstat -apn|grep port | 关键字(java/kafka/nginx) 图中所示 ...

  10. SQL SERVER占用CPU过高优化

    操作系统是Windows2008R2 ,数据库是SQL2014 64位. 近阶段服务器出现过几次死机,管理员反馈机器内存使用率100%导致机器卡死.于是做了个监测服务器的软件实时记录CPU数据,几日观 ...