spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix
spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix
首先application.yml / applicatoin.propreties的配置项:feign.hystrix.enabled=true是针对全局的。
feign.hystrix.enabled=true 或者
feign:
hystrix:
enabled: true
那么怎么配置禁止单个的FeignClient使用hystrix呢。
在自定义的configuration.java的配置文件里加入:
//关闭feign的hystrix
@Bean
@Scope("property")
public Feign.Builder feignBuilder()
{
return Feign.builder(); }
例子:复写Feign的默认配置(fooConfiguration.java)
看例子:

1.入口文件,开启eureka, feign配置
@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class FeignApp { public static void main(String[] args) {
SpringApplication.run(FeignApp.class, args);
}
}
2.复写Feign的默认配置(fooConfiguration.java, fooConfiguration2.java,这两个文件一个是关闭hystrix的,一个没有关闭)
a.返回默认的feign配置
@Configuration
public class FooConfiguration { @Bean
public Contract feignContract()
{
return new feign.Contract.Default();
} }
b.返回默认的feign配置,以及关闭Hystrix
@Configuration
public class FooConfiguration2 { /**
* 配置Url用户和密码,当eureka启用用户名和密码时
* @return
*/
/*@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor()
{
return new BasicAuthRequestInterceptor("root", "root123"); }*/ //默认配置
@Bean
public Contract getContract()
{
return new feign.Contract.Default(); } //feign日志配置
@Bean
Logger.Level feignLoggerLevel()
{
return Logger.Level.FULL;
} //关闭feign的hystrix
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder()
{
return Feign.builder(); } }
3.FeignClient的代码
a.调取spring-boot-user服务器
@FeignClient(name="spring-boot-user", configuration=FooConfiguration.class, fallback=HystrixClientFallback.class)
public interface UserFeignClient { /**
* 代用feign原生模式
* @param id
* @return
*/
@RequestLine("GET /simple/{id}")
public User findById(@Param("id") Long id);
}
b.调取eureka的apps/serviceName(要填入具体项目名称)服务
/**
* 当@FeignClient有name和url还有configuration时,取值为url的地址,name只是为一个名称而已(无意义)
* 当@FeignClient只有name和configuration时,name的取值为eureka中的application项目的名称即虚拟地址
* @author Administrator
*
*/
@FeignClient(name="boot-user", url="http://localhost:8761", configuration=FooConfiguration2.class, fallback=HystrixClientFallback2.class)
public interface UserFeignClient2 { @RequestLine("GET /eureka/apps/{serviceName}")
public String findServiceInfoFromEurekaByServiceName(@Param("serviceName") String serviceName); //@RequestLine("GET /simple/{id}")
//public User findById(@Param("id") Long id);
}
4.fallback方法,hystrix的断容器代码
a.
@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;
} }
b.直接返回字符
@Component
public class HystrixClientFallback2 implements UserFeignClient2 { @Override
public String findServiceInfoFromEurekaByServiceName(String serviceName) {
// TODO Auto-generated method stub
return "hahah";
} }
5.controller代码
@RestController
public class UserController { @Autowired
private UserFeignClient userFeignClient; @Autowired
private UserFeignClient2 userFeignClient2; @GetMapping("/simple/{id}")
public User findById(@PathVariable Long id) {
return this.userFeignClient.findById(id);
} @GetMapping("/eureka/apps/{serviceName}")
public String findEurekaInfo(@PathVariable String serviceName) {
return this.userFeignClient2.findServiceInfoFromEurekaByServiceName(serviceName);
} }
6测试
a.eureka服务,spring-boot-user服务未关闭的情况

b.eureka服务器、spring-boot-user服务器关闭
spring cloud: Hystrix(五):如禁止单个FeignClient使用hystrix的更多相关文章
- Spring Cloud(五):Hystrix 监控面板【Finchley 版】
Spring Cloud(五):Hystrix 监控面板[Finchley 版] 发表于 2018-04-16 | 更新于 2018-05-10 | 在上一篇 Hystrix 的介绍中,我们提到 ...
- Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】
Spring Cloud(四):服务容错保护 Hystrix[Finchley 版] 发表于 2018-04-15 | 更新于 2018-05-07 | 分布式系统中经常会出现某个基础服务不可用 ...
- Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)
Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...
- Spring Cloud第五篇 | 服务熔断Hystrix
本文是Spring Cloud专栏的第五篇文章,了解前四篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...
- Spring Cloud(五)断路器监控(Hystrix Dashboard)
在上两篇文章中讲了,服务提供者 Eureka + 服务消费者 Feign,服务提供者 Eureka + 服务消费者(rest + Ribbon),本篇文章结合,上两篇文章中代码进行修改加入 断路器监控 ...
- spring cloud学习(五)断路器 Hystrix
断路器 Hystrix 断路器模式 (云计算设计模式) 断路器模式源于Martin Fowler的Circuit Breaker一文. 在分布式环境中,其中的应用程序执行访问远程资源和服务的操作,有可 ...
- 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)
在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...
- [Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard
在之前的18次文章中,我们实现了广告系统的广告投放,广告检索业务功能,中间使用到了 服务发现Eureka,服务调用Feign,网关路由Zuul以及错误熔断Hystrix等Spring Cloud组件. ...
- 【spring cloud】子模块启动报错com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect
spring cloud子模块启动报错 Caused by: java.lang.ClassNotFoundException: com.netflix.hystrix.contrib.javanic ...
随机推荐
- java开发注意点之String使用equals和==的区别
"=="操作符的作用 1.用于基本数据类型的比较 2.判断引用是否指向堆内存的同一块地址. equals所在位置: 在Object类当中,而Object是所有类的父类,包含在jdk ...
- ./configure: error: C compiler cc is not found
没有安装gcc 在安装nginx之前先安装依赖软件 yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget htt ...
- 数据库 --- 4 多表查询 ,Navicat工具 , pymysql模块
一.多表查询 1.笛卡儿积 查询 2.连接 语法: ①inner 显示可构成连接的数据 mysql> select employee.id,employee.name,department ...
- MVC 自定义特性(验证用户登录session是否已经过期)
新建一个类 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] publ ...
- SP10707 COT2 - Count on a tree II 莫队
链接 https://vjudge.net/problem/SPOJ-COT2 https://www.luogu.org/problemnew/show/SP10707 思路 dfs欧拉序转化为普通 ...
- newcoder F石头剪刀布(DFS + 思维)题解
题意:wzms 今年举办了一场剪刀石头布大赛,bleaves 被选为负责人. 比赛共有 2n 个人参加, 分为 n 轮, 在每轮中,第 1 位选手和第 2 位选手对战,胜者作为新的第 1 位选手, 第 ...
- lamp服务器被人恶意绑定域名的解决办法
还没开始就被别人绑定了域名 事情的起因与发现 刚买了个服务器搭建了一个dz,想着域名还没备案,就先搭建了起来,然后在做DDOS测试时偶然发现服务器被别人恶意把域名绑定了 最初的解决方案 没管..... ...
- 今天就整一个bug了
BeanPostProcessor加载次序及其对Bean造成的影响分析 SSM整合出现not found for dependency: expected at least 1 bean which ...
- x=x+1,x+=1,及x++的效率哪个最高,为什么?
x=x+1: 1. 读取右X的地址 2. X+1 3. 读取左X的地址 4. 将右值传给左边的X x+=1: 1. 读取右边的x的地址 2. X+1 3 ...
- facebook ads api
api测试 https://developers.facebook.com/tools/explorer/517735271920003?method=GET&path=act_1107316 ...