fallbackFactory(类似于断容器)与fallback方法

feign的注解@FeignClient:fallbackFactory与fallback方法不能同时使用,这个两个方法其实都类似于Hystrix的功能,当网络不通时返回默认的配置数据.

fallback方法的使用:

在入口文件开启feign注解功能。

@EnableFeignClients

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeignApp {
public static void main(String[] args) {
SpringApplication.run(FeignApp.class, args);
}
}

  

2.写一个访问spring-boot-user服务的接口,同时在@FeignClient注解中使用fallback默认返回方法(断容器)

fallback=HystrixClientFallback.class

@FeignClient(name="spring-boot-user", fallback=HystrixClientFallback.class)
public interface UserFeignClient { // 两个坑:1. @GetMapping不支持 2. @PathVariable得设置value
@RequestMapping(value="/simple/{id}", method=RequestMethod.GET)
public User findById(@PathVariable("id") Long id); }

  

3.写HystrixClientFallback类,并继承UserFeignClient类,当网络不通或者访问失败时,返回固定/默认内容

@Component
public class HystrixClientFallback implements UserFeignClient{ @Override
public User findById(Long id) {
// TODO Auto-generated method stub
User user = new User();
user.setId(0L);
return user;
}
}

  

4.controller调用spring-boot-user服务的接口

@RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable("id") Long id) {
return this.userFeignClient.findById(id);
} }

  

fallbackFactory方法的使用

1.入口文件引入feign注解

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeignApp { public static void main(String[] args) {
SpringApplication.run(FeignApp.class, args);
}
}

  

2.写feignClient客户端j,使用feignClient注解的fallbackFactory方法

@FeignClient(name="spring-boot-user", fallbackFactory=HystrixClientFallbackFactory.class)
public interface UserFeignClient { // 两个坑:1. @GetMapping不支持 2. @PathVariable得设置value
@RequestMapping(value="/simple/{id}", method=RequestMethod.GET)
public User findById(@PathVariable("id") Long id); }

  

3.写HystrixClientFallbackFactory类,和HystrixClientWithFallbackFactory类

HystrixClientWithFallbackFactory类继承UserFeignClient类

public interface HystrixClientWithFallbackFactory extends UserFeignClient {

}

  

HystrixClientFallbackFactory实现FallbackFactory类,并使用内部匿名方法类,继续UserFeignClient

@Component
public class HystrixClientFallbackFactory implements FallbackFactory<UserFeignClient> { @Override
public UserFeignClient create(Throwable arg0) {
// TODO Auto-generated method stub
return new HystrixClientWithFallbackFactory() { @Override
public User findById(Long id) {
// TODO Auto-generated method stub
User user = new User();
user.setId(-1L);
return user;
} };
} }

  

4controller调用UserFeignClient接口

@RestController
public class MovieController { @Autowired
private UserFeignClient userFeignClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable("id") Long id) {
return this.userFeignClient.findById(id);
}
}

  

5调用

当开启spring-boot-user方法,返回数据

当关闭spring-boot-user服务时

spring cloud: Hystrix(六):feign的注解@FeignClient:fallbackFactory(类似于断容器)与fallback方法的更多相关文章

  1. Spring Cloud(六):Hystrix 监控数据聚合 Turbine【Finchley 版】

    Spring Cloud(六):Hystrix 监控数据聚合 Turbine[Finchley 版]  发表于 2018-04-17 |  更新于 2018-05-07 |  上一篇我们介绍了使用 H ...

  2. spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix

    spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix 首先application.yml / applicatoin.propreties的配置项:fe ...

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

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

  4. Spring Cloud 微服务笔记(六)Spring Cloud Hystrix

    Spring Cloud Hystrix Hystrix是一个延迟和容错库,旨在隔离远程系统.服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力. 一.快速入门 1)依赖: <dep ...

  5. Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端

    Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...

  6. Spring Cloud 微服务四:熔断器Spring cloud hystrix

    前言:在微服务架构中,一般都是进程间通信,有可能调用链都比较长,当有底层某服务出现问题时,比如宕机,会导致调用方的服务失败,这样就会发生一连串的反映,造成系统资源被阻塞,最终可能造成雪崩.在sprin ...

  7. Spring Cloud Hystrix理解与实践(一):搭建简单监控集群

    前言 在分布式架构中,所谓的断路器模式是指当某个服务发生故障之后,通过断路器的故障监控,向调用方返回一个错误响应,这样就不会使得线程因调用故障服务被长时间占用不释放,避免故障的继续蔓延.Spring ...

  8. 一起来学Spring Cloud | 第六章:服务网关 ( Zuul)

    本章节,我们讲解springcloud重要组件:微服务网关Zuul.如果有同学从第一章看到本章的,会发现我们已经讲解了大部分微服务常用的基本组件. 已经讲解过的: 一起来学Spring Cloud | ...

  9. 7、Spring Cloud Hystrix

    1.Spring Cloud Hystrix简介 (1).分布式问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败. 多个微服务之间调用的时候,假设微服务A调 ...

随机推荐

  1. update与select关联执行效率问题

    UPDATE fl_user_space u SET u.`course_count` = (SELECT COUNT(*) FROM fl_course c WHERE c.uid = u.uid) ...

  2. android uboot中的mmc命令

    一:mmc的命令如下: 1:对mmc读操作 mmc read addr blk# cnt 2:对mmc写操作 mmc write addr blk# cnt 3:对mmc擦除操作 mmc erase ...

  3. day 26 元类

    一.isinstance issubclass class Person: passclass Student(Person): passstu1=Student()#判断是不是实例print(isi ...

  4. \n和\r区别

    转载:https://www.cnblogs.com/hq233/p/6389234.html 符号    ASCII码      意义\n        10        换行NL\r       ...

  5. topcoder srm 679 div1

    problem1 link $f[u][0],f[u][1]$表示$u$节点表示的子树去掉和不去掉节点$u$的最大权值. problem2 link 首先预处理计算任意三个蓝点组成的三角形中的蓝点个数 ...

  6. 配置Jenkins 实现自动发布maven项目至weblogic(svn+maven+weblogic12c)

    Jenkins安装完成之后,需要我们对其配置,然后才可以实现自动部署项目. 前提 防火墙开放weblogic的7001端口 Linux(CentOS):firewall-cmd --zone=publ ...

  7. Docker 编排工具Rancher 1.6.18

    使用docker获取rancher [root@localhost /]# docker pull rancher/server:stable [root@localhost /]# docker i ...

  8. Configuring Logstash

    Configuring Logstash To configure Logstash, you create a config file that specifies which plugins yo ...

  9. FastQC结果详解

    REF https://www.plob.org/article/5987.html 解压后,查看html格式的结果报告.结果分为如下几项: 结果分为绿色的"PASS",黄色的&q ...

  10. Arch 安装后,一些基本设置(1)

    1.安装成功后新建普通用户不能使用useradd进行一步添加,应该下载adduser交互式添加新用户,否则用户无法登陆. 2.安装openkeeper之前需要安装ppp和net-tools (需要里面 ...