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 ...
随机推荐
- UWP 开发APP准备工作
每新建一个UWP之后,都要进行一番相同的处理步骤,才能使Mobile的使用体验更好,故总结如下: 1.订阅Mobile后退导航事件 在App.xaml.cs文件中OnLaunched方法中添加 Sys ...
- Android 的永久登陆 与注销登陆
一.永久登陆 sharedprefrence 存储 userID 以及 password private String FILE = "saveUserNamePwd";//用于 ...
- 大数据量.csv文件导入SQLServer数据库
前几天拿到了一个400多M的.csv文件,在电脑上打开要好长时间,打开后里面的数据都是乱码.因此,做了一个先转码再导入数据库的程序.100多万条的数据转码+导入在本地电脑上花了4分钟,感觉效率还可以. ...
- python字符串、列表、元组
字符串的常用方法: name.count('h')统计h在name中出现的次数 name.find('h')查找h的索引 '?'.join(name)使用问好拼接 name.encode('gb231 ...
- "啃下"插入排序
插入排序法基本原理 插入排序法较冒泡排序法和选择排序法更贴近生活,应该来说理解起来更快.如果你现在能够得到一副麻将,请把里面的“一万”到“六万”拿出来,打乱顺序,再重新排好,就像打麻将开始那样.是否需 ...
- 使用iframe标签时如何通过jquery隐藏滚动条
通过mouseover和mouseout事件来控制iframe的滚动条 代码如下:
- 紫书 例题8-1 UVa 120(构造法)
#include<cstdio> #include<iostream> #include<sstream> #include<algorithm> #d ...
- 利用 ST-LINK Utility软件下载程序
先在电脑上安装STM32 ST-LINK Utility,软件安装一路Next就可以了,安装好软件之后界面如下: 下载程序只需要使用3个图标就可以了 第一个图标Connect to the ta ...
- phpexcel乱码问题
php导出Excel乱码,只需在header函数前加入ob_end_clean();//清除缓冲区,避免乱码
- Android,iOS打开手机QQ与指定用户聊天界面
在浏览器中能够通过JS代码打开QQ并弹出聊天界面.一般作为客服QQ使用. 而在移动端腾讯貌似没有发布提供相似API,可是却能够使用schema模式来启动手机QQ. 下面为详细代码: Android: ...