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. 变参标准函数的重新封装,如printf

    方法一: #include <stdio.h> #include <stdarg.h> void my_trace(const char *cmd, ...) { printf ...

  2. Qt重绘机制

    一.引发重绘的事件 1.调用repaint() 2.调用uodate() 二.控件hide或者show 三.其他 ps: repaint函数是立即重绘,没有优化 update会优化,异步重绘,所以如果 ...

  3. ol3对地图上某些特定的经纬度进行标注

    最终效果需要类似于这种 1.首先我们需要一个最基本的地图,这一步骤可以浏览该分类下的上一篇随笔. 2.ol3支持的文件格式有.geojson,我们需要将坐标制作成符合这种格式的样子才能被ol3识别并显 ...

  4. 第一次参加acm区域赛

    什么,这周天就要去参加acm焦作赛,简直不敢相信.从大一暑假七月份中旬到今天十一月23日,加入acm将近四个多月的时间,如今到了检验自己的时候了.aaaaaaaaaa.乌拉,必胜.打印个模板,在跑个步 ...

  5. POJ 2226 Muddy Fields(最小点覆盖)题解

    题意:一片r*c的地,有些地方是泥地,需要铺地板.这些地板宽1,长无限,但只能铺在泥地上不能压到其他地方,问你铺满所有泥地最少几块 思路:我们把一行中连续的泥地看成整体,并把所有横的整体里的点编成一个 ...

  6. oracle单行函数 之 通用函数

    NVL()函数,处理null. Decode()函数,:多数值判断 Decode(数值 \ 列,判断值1,显示值1,判断值2,显示值2)若是判断值不包含的,则显示为空 Decode()函数非常类似程序 ...

  7. POJ3580 SuperMemo

    Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to pl ...

  8. (zhuan) How to Train Neural Networks With Backpropagation

    this blog from: http://blog.demofox.org/2017/03/09/how-to-train-neural-networks-with-backpropagation ...

  9. 用RAR将多个文件夹一次性压缩为多个对应zip文件

    选中要压缩的所有文件夹.右键,选“添加到压缩文件...”,弹出的菜单如下图: 点击菜单栏“文件”.在“把每个文件都单独压缩文件中”选中,才可以单独创建压缩.如下图

  10. 剥开比原看代码03:比原是如何监听p2p端口的

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...