Feign对Hystrix的支持,我们知道Feign Client注解它是支持fallback属性,这个我们详细探讨一下Feign Hystrix Supporthttps://cloud.spring.io/spring-cloud-openfeign/reference/html/如果Hystrix在classpath下面,默认情况下所有的方法都会被断路器包装,你返回com.netflix.hystrix.HystrixCommand这个类也是可以的,这个类我们可能很陌生https://github.com/Netflix/Hystrix/wiki/How-To-Usepublic class CommandHelloFailure extends HystrixCommand<String> {private final String name;public CommandHelloFailure(String name) {super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));this.name = name;}@Overrideprotected String run() {throw new RuntimeException("this command always fails");}@Overrideprotected String getFallback() {return "Hello Failure " + name + "!";}
}响应式的模式,rx.java,rx.android,异步的调用使用queue,响应式编程的东西,如果你想让Feign不支持,If Hystrix is on the classpath and feign.hystrix.enabled=true, Feign will wrap all methods with a circuit breaker. Returning a com.netflix.hystrix.HystrixCommand is also available. This lets you use reactive patterns (with a call to .toObservable() or .observe() or asynchronous use (with a call to .queue()).如果你想单独的禁用一个Feign Client的话,你可以这么玩@Configuration
public class FooConfiguration {@Bean@Scope("prototype")public Feign.Builder feignBuilder() {return Feign.builder();}
}Hystrix它支持一种回退的概念,他说当断路器打开,或者发现错误的时候,他就会执行一个默认的代码路径,我们之前Ribbon的做法一样的吗,无论是请求不成功或者断路器打开的时候,就会走fallback方法,你可以使用@FeignClient注解,并为他设置fallback的属性,然后这个类实现FeinClient接口Hystrix supports the notion of a fallback: a default code path that is executed when they circuit is open or there is an error. To enable fallbacks for a given @FeignClient set the fallback attribute to the class name that implements the fallback. You also need to declare your implementation as a Spring bean.就是这个样子的@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
protected interface HystrixClient {@RequestMapping(method = RequestMethod.GET, value = "/hello")Hello iFailSometimes();
}static class HystrixClientFallback implements HystrixClient {@Overridepublic Hello iFailSometimes() {return new Hello("fallback");}
}使用fallback属性,to the class name,他要实现HystrixClient接口microservice-consumer-movie-feign-hystrixlocalhost:8010/movie/1Feign使用Hystrix无效原因及解决方法https://www.cnblogs.com/devzxd/p/feign-hystrix-problem.html<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Brixton.RELEASE</version><type>pom</type><scope>import</scope>
</dependency>为什么要默认关闭hystrix呢?请看这里:https://github.com/spring-cloud/spring-cloud-netflix/issues/1277解决方案
如果是yml文件,请在文件中加入:feign:hystrix:enabled: true
如果是properties文件,请在文件中加入:feign.hystrix.enabled=truelocalhost:8010/healthlocalhost:8010/hystrix.streamhttps://cloud.spring.io/spring-cloud-openfeign/reference/html/#spring-cloud-feign-hystrixFeign Hystrix SupportIf Hystrix is on the classpath and feign.hystrix.enabled=true, Feign will wrap all methods with a circuit breaker. @Configuration
public class FooConfiguration {@Bean@Scope("prototype")public Feign.Builder feignBuilder() {return Feign.builder();}
}
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version><packaging>pom</packaging><name>microcloud02</name><url>http://maven.apache.org</url><properties><jdk.version>1.8</jdk.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencyManagement><dependencies><dependency>    <groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency><dependency>    <!-- SpringCloud离不开SpringBoot,所以必须要配置此依赖包 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>1.5.12.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><!-- <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> --></project>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><artifactId>microservice-consumer-movie-feign-hystrix</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>microservice-simple-consumer-movie</name><description>Demo project for Spring Boot</description><parent><groupId>cn.learn</groupId><artifactId>microcloud02</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
#debug=true
server.port=8010eureka.client.serviceUrl.defaultZone=http://admin:1234@10.40.8.152:8761/eurekaspring.application.name=microservice-consumer-movie-feign-hystrix
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
eureka.client.healthcheck.enabled=true
spring.redis.host=10.40.8.152
spring.redis.password=1234
spring.redis.port=6379#stores.ribbon.listOfServers=10.40.8.144:7900feign.hystrix.enabled=true
package com.learn.cloud.feign;import org.springframework.stereotype.Component;import com.learn.cloud.entity.User;@Component
public class HystrixClientFallback implements UserFeignClient {@Overridepublic User findById(Long id) {User user = new User();user.setId(0L);return user;}}
package com.learn.cloud.feign;import org.springframework.stereotype.Component;import com.learn.cloud.entity.User;import feign.hystrix.FallbackFactory;@Component
public class FeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {@Overridepublic UserFeignClient create(Throwable throwable) {return new UserFeignClient() {@Overridepublic User findById(Long id) {System.out.println("fallback; reason was :"+ throwable);User user = new User();user.setId(-1L);user.setName("默认用户");return user;}};}
}
package com.learn.cloud.feign;import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import com.learn.cloud.entity.User;//@FeignClient(name = "microservice-simple-provider-user", fallback = HystrixClientFallback.class)
@FeignClient(name = "microservice-simple-provider-user", fallbackFactory = FeignClientFallbackFactory.class)
public interface UserFeignClient {@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)public User findById(@PathVariable("id") Long id);
}
package com.learn.cloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import com.learn.cloud.entity.User;
import com.learn.cloud.feign.UserFeignClient;@RestController
public class MovieController {@Autowiredprivate UserFeignClient userFeignClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id) {return this.userFeignClient.findById(id);}}
package com.learn.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerMovieFeignHystrixApplication {public static void main(String[] args) {SpringApplication.run(ConsumerMovieFeignHystrixApplication.class, args);}
}

如何禁用单个FegionClient的Hystrix的支持相关推荐

  1. 第七章:SpringCloud Feign对hystrix的支持

    方法一:设置fallback属性 Feign Hystrix Fallbacks 官网解释 Hystrix supports the notion of a fallback: a default c ...

  2. Spring Cloud对Hystrix的支持

    Spring Cloud项目为Netflix OSS Hystrix库提供了全面的支持. 之前我已经写过有关如何使用原始Hystrix库包装远程调用的文章. 在这里,我将探讨如何将Hystrix与Sp ...

  3. Element DatePicker 日期选择器禁用单个指定日期

    需求:禁用某个时间范围 例如['2022-11-11', '2022-11-29'] 效果图: 代码: // html<el-date-pickerv-model="formInfo. ...

  4. feign 回退原因_0503-Hystrix保护应用-feign的hystrix支持

    一.概述 1.1.基础[示例一] 如果Hystrix在类路径上并且feign.hystrix.enabled = true,Feign将用断路器包装所有方法.还可以返回com.netflix.hyst ...

  5. java-大数据-精品课程目录(超级全)

    网上看到的一个目录,可以通过对应视频名称网上找找资源 第一章:java精品课程目录大全 1.亿级流量电商详情页系统的大型高并发与高可用缓存架构实战 1课程介绍以及高并发高可用复杂系统中的缓存架构有哪些 ...

  6. Java架构师,大数据架构师,高并发设计模式,机器学习知识点分享

    第一章:java精品课程目录大全 1.亿级流量电商详情页系统的大型高并发与高可用缓存架构实战 1课程介绍以及高并发高可用复杂系统中的缓存架构有哪些东西?32分钟 2基于大型电商网站中的商品详情页系统贯 ...

  7. 2018最新Spring Cloud 系列学习附课件全套

    Spring Cloud 系列学习视频教程附课件全套 按照官方的话说:Spring Cloud 为开发者提供了在分布式系统(如配置管理.服务发现.断路器.智能路由.微代理.控制总线.一次性 Token ...

  8. 创智播客微服务_周立-Spring Cloud微服务实战视频课程

    『课程目录』:" |0 O6 f0 d! b% }$ Z; T- g 1 公开课 2 1.1 微服务架构概述 3. 2.1 开始使用Spring Cloud实战微服务 4 3.1 服务提供者 ...

  9. Hystrix-超时机制和断路器模式

    一 超时机制.断路器模式简介 1.1 背景 假设服务提供者响应非常缓慢,那么消费者对提供者的请求线程就会被等待,知道服务返回,在高并发高负载的场景下,如果不做任何处理,这种问题很有可能造成所有处理用户 ...

最新文章

  1. 免费公开课 | 基于定制数据流技术的AI计算加速
  2. sqlserver 分页存储过程
  3. 怎样快速提高新站权重收录
  4. 职业中专计算机高考英语卷子,职业高中高考计算机专业试卷3.doc
  5. 组件三层_Java三层架构原理与作用小结
  6. AR 第一大单,微软 219 亿美元为美军打造高科技头盔
  7. HP台式计算机不能启动,惠普电脑不能启动怎么处理
  8. 笔记本双显卡Ubantu16.04 Nvidia驱动安装指导
  9. 洪磊口述:番茄花园如何捆绑流氓软件月入十万
  10. 数据库里的数据模型是什么及三要素
  11. Java程序员必读精选书籍分享,强烈推荐
  12. allergro音乐术语什么意思_常见音乐术语(速度术语)
  13. c语言表示静态存储类别的关键字是,在C语言中,表示静态存储类别的关键字是:...
  14. Java学习第十二天----方法参数类型以及返回值类型问题、链式编程、package包、权限修饰符、内部类、成员内部类
  15. HOS Develop Notes-开启ssh服务
  16. 汽车ABS控制器设计及车身稳定算法研究笔记(二)——ABS控制器硬件设计及调试
  17. 1822 - Failed to add the foreign key constraint. Missing index for constraint ‘fk_tno_course‘ in the
  18. Class类是什么?
  19. Metal 框架之渲染到多个纹理切片
  20. linux读内存的命令devmem,嵌入式Linux调试_命令devmem_直接读写内存

热门文章

  1. Android—常用组件练习
  2. 《30天自制操作系统》笔记(04)——显示器256色
  3. iOS隐藏键盘的几种方式
  4. [asp.net] 获取网页访问来路的几种方法
  5. EnterpriseLibrary数据访问(4)使用数据访问器接收数据
  6. C#中string类型赋值问题
  7. 【Spring学习】spring动态配置多数据源
  8. Innodb事务和锁
  9. JavaScript强化教程——JavaScript Math(算数) 对象
  10. 总结运行SSIS包的几种方式