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 ...
随机推荐
- phpstorm 配置点右上角浏览器图标时用浏览器打开当前页面
文件>设置 只要这一步就可以这样了
- 20145208 蔡野 《网络对抗》免考项目 MSF学习
20145208 蔡野 <网络对抗>免考项目 MSF Exploit模块开发 题目:MSF Exploit模块开发 摘要 本免考项目的目标是通过对msf模块的设计语言--ruby和expl ...
- 桌面版Ubuntu系统固定IP设置和Network-manager设置
我自己的学校是绑定MAC和IP上网的.这种方法名义上说是为了安全,不过是个搞计算机的都知道随便修改一下MAC就可以使用别人的IP了. 不提蛋疼的事情了,先来说说网络的配置吧 我用的是Ubuntu14. ...
- Firemonkey的几个特色属性(二)
3.RotationAngle 控件的旋转角度,可以通过TAnimation进行角度旋转控制. 4.RotationCenter 控件旋转的中心位置,从(0,0)到(1,1),缺省是(0.5,0.5) ...
- Python3基础 list extend 合并列表
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 从Hello World说起(Dart)到“几乎所有东西都是Widget”小部件。
import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); class MyApp extends S ...
- P2234 [HNOI2002]营业额统计(Splay树)题解
思路:Splay数查找前驱后继 代码: #include<iostream> #include<cstdio> #include<cstring> #include ...
- hihoCoder week4 Trie图
ac自动机 题目链接 https://hihocoder.com/contest/hiho4/problem/1 参考:https://blog.csdn.net/baidu_30541191/art ...
- c# 之 事务
SqlTransaction事务的简单例子 { DataTable dt = new DataTable(); System.Data.SqlClient.SqlConnection cnn = ne ...
- Asp.Net 之 js/jquery获取服务器端控件
由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,总结有以下3种方法:服务器控件代码:<asp:TextBox ID="txtUserID& ...