feignclient设置hystrix参数
序
feign默认集成了hystrix,那么问题来了,如何像hystrix command那样设置每个方法的hystrix属性呢。
实例
@FeignClient("product")
public interface RemoteProductService {
@RequestMapping(method = RequestMethod.GET,value = "/product/{productId}")
public Product getProduct(@PathVariable(value = "productId") int productId);
}
FeignClientsConfiguration
spring-cloud-netflix-core-1.2.6.RELEASE-sources.jar!/org/springframework/cloud/netflix/feign/FeignClientsConfiguration.java
@Configuration
@ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
protected static class HystrixFeignConfiguration {
@Bean
@Scope("prototype")
@ConditionalOnMissingBean
@ConditionalOnProperty(name = "feign.hystrix.enabled", matchIfMissing = true)
public Feign.Builder feignHystrixBuilder() {
return HystrixFeign.builder();
}
}
HystrixFeign
feign-hystrix-9.3.1-sources.jar!/feign/hystrix/HystrixFeign.java
private SetterFactory setterFactory = new SetterFactory.Default();
SetterFactory
feign-hystrix-9.3.1-sources.jar!/feign/hystrix/SetterFactory.java
public interface SetterFactory {
/**
* Returns a hystrix setter appropriate for the given target and method
*/
HystrixCommand.Setter create(Target<?> target, Method method);
/**
* Default behavior is to derive the group key from {@link Target#name()} and the command key from
* {@link Feign#configKey(Class, Method)}.
*/
final class Default implements SetterFactory {
@Override
public HystrixCommand.Setter create(Target<?> target, Method method) {
String groupKey = target.name();
String commandKey = Feign.configKey(target.type(), method);
return HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
}
}
}
groupKey,这里为product,即@FeignClient("product")中的值。
Feign.configKey
feign-core-9.3.1-sources.jar!/feign/Feign.java
public static String configKey(Class targetType, Method method) {
StringBuilder builder = new StringBuilder();
builder.append(targetType.getSimpleName());
builder.append('#').append(method.getName()).append('(');
for (Type param : method.getGenericParameterTypes()) {
param = Types.resolve(targetType, targetType, param);
builder.append(Types.getRawType(param).getSimpleName()).append(',');
}
if (method.getParameterTypes().length > 0) {
builder.deleteCharAt(builder.length() - 1);
}
return builder.append(')').toString();
}
commandKey的构造,这里组装了类、方法名、参数,比如本文的实例,commandKey=RemoteProductService#getProduct(int)
配置文件指定
hystrix:
command:
"RemoteProductService#getProduct(int)":
execution:
isolation:
thread:
timeoutInMilliseconds: 500
Java中指定
@Bean
public Feign.Builder feignHystrixBuilder() {
return HystrixFeign.builder().setterFactory(new SetterFactory() {
@Override
public HystrixCommand.Setter create(Target<?> target, Method method) {
return HystrixCommand.Setter
.withGroupKey(HystrixCommandGroupKey.Factory.asKey(RemoteProductService.class.getSimpleName()))// 控制 RemoteProductService 下,所有方法的Hystrix Configuration
.andCommandPropertiesDefaults(
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(10000) // 超时配置
);
}
});
}
小结
论灵活程度,还是配置文件灵活一点,唯一的工作量就是根据规则构造commandKey,然后就可以进行相关配置了。
import com.netflix.hystrix.HystrixCommand;
HystrixCommand.Setter
feignclient设置hystrix参数的更多相关文章
- hystrix参数使用方法
hystrix+feign+ribbon,但是可能很多人都知道hystrix还有线程隔离,信号量隔离,等等各种参数配置,在这几就记录下hystrix的参数, 一.hystrix参数使用方法 通过注解@ ...
- Hystrix参数配置
1.Hystrix参数配置文档 2.Hystrix参数配置示例 import org.springframework.beans.factory.annotation.Autowired; impo ...
- tomcat,zookeeper,activeMQ,Kafka设置jvm参数
1,tomcat设置jvm参数 设置方法: 在tomcat bin 目录增加配置:setenv.sh #add tomcat pid CATALINA_PID="$CATALINA_ ...
- Scala命令设置JVM参数的规则
Scala下设置JVM参数简单分析 Scala 启动shell脚本,简化后的scala REPL 启动命令大致如下所示: java -Xmx256M -Xms32M \-Xbootclasspath/ ...
- 【Unity】13.2 通过Lighting Window设置相关参数
分类:Unity.C#.VS2015 创建日期:2016-05-19 一.简介 Unity 5.3.4的Lighting Window有3个选项卡:Object.Scene.Lightmaps. 二. ...
- 设置JVM参数,查看堆大小
1.在eclipse设置JVM参数 打开eclipse-窗口-首选项-Java-已安装的JRE(对在当前开发环境中运行的java程序皆生效,也就是在eclipse中运行的java程序)编辑当前 ...
- Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致
Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致,如果参数不一致,传过去为null
- Delphi HTTPRIO控件怎么设置超时参数
HTTPRIO控件怎么设置超时参数 //HTTPRIO1: THTTPRIO 设置5分钟超时 HTTPRIO1.HTTPWebNode.ConnectTimeout := 5000; Connect ...
- javascript:设置URL参数的方法,适合多条件查询
适用场景:多条件查询情况,如下图所示: 通过设置URL参数,再结合数据源控件设置的RUL参数,就能进行简单的多条件查询了. javascript函数: <mce:script type=&quo ...
随机推荐
- 关闭xp防火墙
在控制面版关闭防火墙 禁用“Security Center”服务 禁用“WindowsFirewall/InternetConnectionSharing(ICS)”服务 删除服务:开始运行CMD,命 ...
- 【基于Android的ARM汇编语言系列】之三:ARM汇编语言程序结构
作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell [ ...
- SettingsJDK
迁移时间:2017年5月20日23:38:40 Author:Marydon 1.双击安装,更改安装路径为D:\ProgramFiles\Java\jdk1.7.0_55: 注意事项: 1.1 将 ...
- 类的专有方法(__getattr__和__setattr__、__delattr__)
# -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.360doc.com/content/15/0413/19/12067640_4629 ...
- 修改Cygwin的默认启动路径
原先启动Cygwin后,pwd显示: C:\Documents and Settings\Administrator@IBM-EBDC0EAC4B7 ~$ pwdC:\Documents and Se ...
- url-pattern / /*匹配
http://hi.baidu.com/atell/item/522112d3db45081fd90e44e1 struts2中配置为 <url-pattern>/*</url-pa ...
- word中批量修改图片大小
一,在word中按alt+f11组合键,进入VBA模式二,在左边的工程资源管理器中找到你的word文档,在其上右键/添加/模块三,把下面代码复制,粘贴进去.四,更改数值, 改一下宽度和高度数值(10) ...
- Cannot refer to the non-final local variable user defined in an enclosing scope 内部类定义在方法内,方法定义的参数(形参)无法被内部类直接访问,需要用final定义
为什么匿名内部类参数必须为final类型 1) 从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变量 ...
- python selenium +autoit实现文件上传 --实践
upload.html <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...
- Spring.net(一)----Spring.NET框架简介及模块说明
简介: Spring.NET框架包括很多功能,Spring.NET 1.0包括完整的IoC容器和AOP类库.1.1版加入Web.ORM和数据模块.Spring.NET的下载包中并不包含与其它类库 ...