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的更多相关文章

  1. Spring Cloud(五):Hystrix 监控面板【Finchley 版】

    Spring Cloud(五):Hystrix 监控面板[Finchley 版]  发表于 2018-04-16 |  更新于 2018-05-10 |  在上一篇 Hystrix 的介绍中,我们提到 ...

  2. Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】

    Spring Cloud(四):服务容错保护 Hystrix[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  分布式系统中经常会出现某个基础服务不可用 ...

  3. Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)

    Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...

  4. Spring Cloud第五篇 | 服务熔断Hystrix

    ​ 本文是Spring Cloud专栏的第五篇文章,了解前四篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...

  5. Spring Cloud(五)断路器监控(Hystrix Dashboard)

    在上两篇文章中讲了,服务提供者 Eureka + 服务消费者 Feign,服务提供者 Eureka + 服务消费者(rest + Ribbon),本篇文章结合,上两篇文章中代码进行修改加入 断路器监控 ...

  6. spring cloud学习(五)断路器 Hystrix

    断路器 Hystrix 断路器模式 (云计算设计模式) 断路器模式源于Martin Fowler的Circuit Breaker一文. 在分布式环境中,其中的应用程序执行访问远程资源和服务的操作,有可 ...

  7. 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)

    在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...

  8. [Spring cloud 一步步实现广告系统] 19. 监控Hystrix Dashboard

    在之前的18次文章中,我们实现了广告系统的广告投放,广告检索业务功能,中间使用到了 服务发现Eureka,服务调用Feign,网关路由Zuul以及错误熔断Hystrix等Spring Cloud组件. ...

  9. 【spring cloud】子模块启动报错com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect

    spring cloud子模块启动报错 Caused by: java.lang.ClassNotFoundException: com.netflix.hystrix.contrib.javanic ...

随机推荐

  1. 解决c1xx fatal error C1083 Cannot open source file

    在项目开发过程中,遇到一个问题,一个工程B导入另外一个工程A的生产代码,出现这个错误,最后查阅资料发现是文件路径太深,导致文件路径字符超过了217字符. 写了一个测试Demo来验证: 一.新建Win3 ...

  2. linux基础之vim编辑器

    vi : Visual Interface vim : VI Improved : VI的基础加上一些有用的插件 vim编辑器: 文本编辑器, 字处理器, 全屏编辑器, 模式化编辑器 vim的模式有三 ...

  3. tp剩余未验证内容-4

    关于pop-up被blocked的问题 首先 这个pop-up的功能叫 popup blocker , 它是浏览器(包括ff, chrome等) 自身 所内置 的一个功能, 不是 安装的外部 插件/或 ...

  4. js动画(速度)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...

  5. 使用digitalocean进行项目开发

    使用digitalocean进行项目开发 命令记录 搭建SS 1 apt-get update 2 apt-get install python-pip 3 pip install --upgrade ...

  6. (zhuan) Variational Autoencoder: Intuition and Implementation

    Agustinus Kristiadi's Blog TECH BLOG TRAVEL BLOG PORTFOLIO CONTACT ABOUT Variational Autoencoder: In ...

  7. c# 重试机制

    protected async Task<T> TryOperation<T>(int maxRetryCount,Func<Task<T>> func ...

  8. 测试常用的Linux命令总结

    列出常用的命令和最常用的用法,排名不分先后:) 1. find在/home目录下查找以.txt结尾的文件名find /home -name "*.txt"同上,但忽略大小写find ...

  9. HDU 2612 Find a way(找条路)

    HDU 2612 Find a way(找条路) 00 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)   Problem  ...

  10. 2、Python程序控制结构(0530)

    条件测试: 1.if 条件测试表达式 python的比较操作 1.所有的python对象都支持比较操作 可用于测试相等性.相对大小等: 如果是符合对象,python会检查其所有部分,包括自动遍历各级嵌 ...