SpringCloud学习笔记(14)----Spring Cloud Netflix之Hystrix对Feign的支持
1. Hystrix对Feign的支持
添加Feign中IUserBiz的实现类HystrixFallBack:
package com.wangx.cloud.springcloud02consumer.configure; import com.wangx.cloud.springcloud02consumer.api.UserApi;
import org.springframework.stereotype.Component; @Component
public class HystrixFallBack implements UserApi {
@Override
public String getUser(Integer id) {
System.out.println("连接超时.....");
return "system error";
} }
使用@component注解注册成组件。在@FeignClient注解里面添加fallback属性即可。
如下:
package com.wangx.cloud.springcloud02consumer.api; import com.wangx.cloud.springcloud02consumer.configure.FooConfiguration;
import com.wangx.cloud.springcloud02consumer.configure.HystrixFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "spring-cloud-provider", configuration = FooConfiguration.class, fallback = HystrixFallBack.class)
public interface UserApi { @RequestMapping(value = "/user/getUser")
String getUser(@RequestParam(value = "id") Integer id); }
当出现请求错误或超时时,就会执行实现类中的方法。
熔断设置
#默认为false,如果想用断路由,要打开这个设置
feign.hystrix.enabled=true #断路器线程池超时时间,这个值一定要比ribbon超时时间长,毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=16000
单个应用禁用Hystrix
在FooConfiguration中配置一个bean
package com.wangx.cloud.springcloud02consumer.configure; import feign.Feign;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; @Configuration
public class FooConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}
配置隔离级别
# 在feign和Ribbon里面配置隔离策略(全局配置)
#hystrix.command.default.execution.isolation.strategy=SEMAPHORE
# 配置单个 ystrixCommandKey在Ribbon下面默认为方法名,在feign下面默认为类名#方法名(参数类型)
#hystrix.command.HystrixCommandKey.execution.isolation.strategy=SEMAPHORE
说明:HystrixCommandKey在Ribbon下面默认为方法名,在feign下面默认为类名#方法名(参数类型)
Feign下的HystrixCommandKey类的配置
package com.wangx.cloud.springcloud02consumer.configure; import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommand.Setter;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey; import feign.Feign;
import feign.Target;
import feign.hystrix.HystrixFeign;
import feign.hystrix.SetterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope; import java.lang.reflect.Method; @Configuration
public class FooConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
class RcSetterFactory implements SetterFactory {
@Override
public Setter create(Target<?> target, Method method) {
String groupKey = target.name();
String commandKey = method.getName();
return HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
}
}
return HystrixFeign.builder().setterFactory(new RcSetterFactory());
//return Feign.builder();
}
}
SpringCloud学习笔记(14)----Spring Cloud Netflix之Hystrix对Feign的支持的更多相关文章
- Spring Cloud Netflix多语言/非java语言支持之Spring Cloud Sidecar
Spring Cloud Netflix多语言/非java语言支持之Spring Cloud Sidecar 前言 公司有一个调研要做,调研如何将Python语言提供的服务纳入到Spring Clou ...
- SpringCloud学习笔记(11)----Spring Cloud Netflix之Hystrix断路器的使用
为什么会有断路器? 在微服务架构中,系 是拆分成 一个的服务单元各间通过注册与发现 的方式互相依 赖.每个单元都在不同的进程中运行, 都是通过远程调用的方式进行信 ,这样就有可能因为网络原或 是依赖服 ...
- springCloud学习-消息总线(Spring Cloud Bus)
1.简介 Spring Cloud Bus 将分布式的节点用轻量的消息代理连接起来.它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控.本文要讲述的是用Spring Cloud Bus实现 ...
- SpringCloud学习笔记(16)----Spring Cloud Netflix之Hystrix Dashboard+Turbine集群监控
前言: 上一节中,我们使用Hystrix Dashboard,只能看到单个应用内的服务信息.在生产环境中,我们经常是集群状态,所以我们需要用到Turbine这一应用. 作用:汇总系统内的多个服务的数据 ...
- SpringCloud学习笔记(15)----Spring Cloud Netflix之Hystrix Dashboard的使用
1. 引入依赖 在前面几节中的消费者中添加pom依赖. <dependency> <groupId>org.springframework.cloud</groupId& ...
- SpringCloud学习笔记(13)----Spring Cloud Netflix之Hystrix断路器的隔离策略
说明 : 1.Hystrix通过舱壁模式来隔离限制依赖的并发量和阻塞扩散 2. Hystrix提供了两种隔离策略:线程池(THREAD)和信号量隔离SEMAPHORE). 1. 线程池隔离(默认策略模 ...
- SpringCloud学习笔记(12)----Spring Cloud Netflix之Hystrix断路器的流程和原理
工作流程(参考:https://github.com/Netflix/Hystrix/wiki/How-it-Works) 1. 创建一个HystrixCommand或HystrixObservabl ...
- SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性
1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...
- SpringCloud学习笔记(2)----Spring Cloud Netflix之Eureka的使用
1. Spring Cloud Netflix Spring Cloud Netflix 是Spring Cloud 的核心子项目,是对Netflix公司一系列开源产品的封装.它为Spring Bo ...
随机推荐
- js判断传入时间和当前时间大小
//判断时间是否过期 function judgeTime(time){ var strtime = time.replace("/-/g", "/");//时 ...
- Android TabLayout+ViewPager禁止滑动
1.重写ViewPager并重写覆盖ViewPager的onInterceptTouchEvent(MotionEvent arg0)方法和onTouchEvent(MotionEvent arg0) ...
- 多帧图片转gif
示例 工具photosh cc2017 1: 文件--> 脚本--> 将文件载入堆栈--> 选择文件-->勾选窗口的时间轴-->底部 从图层建立帧--> 设置时间延 ...
- SQL--left join ,inner join, right jion, Limit
SQL Limit 语句 用于返回规定的数量记录.当数据库中的数据量十分庞大时,可以使用,返回指定的数量记录. 语句如:select * from grade limit 5.返回grade表中的前面 ...
- [OS][ linux ] 建立新帳號, 變更密碼, 加入 sudoer
新增 linux , 設定預設 password, 新增 user 到 sudoers 1. 新增 User sudo useradd aa97 2. 設定 User password sudo pa ...
- POJ 1328 Radar Installation 【贪心 区间选点】
解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的 ...
- 人人都是产品经理?关于PM你不知道的还有很多
产品经理的职称最早出现在P&G宝洁公司,因效果非常显著,许多企业纷纷仿而效尤.硅谷知名的产品管理大师Marty Cagan在<Inspired: How To Create Produc ...
- 中国人自己的技术!百度开源自研底层区块链XuperChain
中国人自己的技术!百度开源自研底层区块链XuperChain 近年来,我国各行各业迅猛发展,但不少尖端行业和产品仍然依赖进口,关键领域受制于人,火热的区块链领域也是如此. 在中国国际大数据产业博览会现 ...
- Pyhton二级操作题练习
# 1.编写一个python程序,输入两个数,比较它们的大小并输出其中较大者. num1 = input('请输入数字X:') num2 = input('请输入数字Y:') if num1.isde ...
- BZOJ 2314 士兵的放置(支配集)
显然是\(DP\). 设\(dp[i][0/1/2]\)代表以i为根且\(i上有士兵放置/i被控制但i上没有士兵/i没有被控制\)的最小代价. \(g[i][0/1/2]\)代表对应的方案数. 然后运 ...