feign默认集成了hystrix,那么问题来了,如何像hystrix command那样设置每个方法的hystrix属性呢。

实例

  1. @FeignClient("product")
  2. public interface RemoteProductService {
  3. @RequestMapping(method = RequestMethod.GET,value = "/product/{productId}")
  4. public Product getProduct(@PathVariable(value = "productId") int productId);
  5. }

FeignClientsConfiguration

spring-cloud-netflix-core-1.2.6.RELEASE-sources.jar!/org/springframework/cloud/netflix/feign/FeignClientsConfiguration.java

  1. @Configuration
  2. @ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
  3. protected static class HystrixFeignConfiguration {
  4. @Bean
  5. @Scope("prototype")
  6. @ConditionalOnMissingBean
  7. @ConditionalOnProperty(name = "feign.hystrix.enabled", matchIfMissing = true)
  8. public Feign.Builder feignHystrixBuilder() {
  9. return HystrixFeign.builder();
  10. }
  11. }

HystrixFeign

feign-hystrix-9.3.1-sources.jar!/feign/hystrix/HystrixFeign.java

  1. private SetterFactory setterFactory = new SetterFactory.Default();

SetterFactory

feign-hystrix-9.3.1-sources.jar!/feign/hystrix/SetterFactory.java

  1. public interface SetterFactory {
  2. /**
  3. * Returns a hystrix setter appropriate for the given target and method
  4. */
  5. HystrixCommand.Setter create(Target<?> target, Method method);
  6. /**
  7. * Default behavior is to derive the group key from {@link Target#name()} and the command key from
  8. * {@link Feign#configKey(Class, Method)}.
  9. */
  10. final class Default implements SetterFactory {
  11. @Override
  12. public HystrixCommand.Setter create(Target<?> target, Method method) {
  13. String groupKey = target.name();
  14. String commandKey = Feign.configKey(target.type(), method);
  15. return HystrixCommand.Setter
  16. .withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
  17. .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
  18. }
  19. }
  20. }

groupKey,这里为product,即@FeignClient("product")中的值。

Feign.configKey

feign-core-9.3.1-sources.jar!/feign/Feign.java

  1. public static String configKey(Class targetType, Method method) {
  2. StringBuilder builder = new StringBuilder();
  3. builder.append(targetType.getSimpleName());
  4. builder.append('#').append(method.getName()).append('(');
  5. for (Type param : method.getGenericParameterTypes()) {
  6. param = Types.resolve(targetType, targetType, param);
  7. builder.append(Types.getRawType(param).getSimpleName()).append(',');
  8. }
  9. if (method.getParameterTypes().length > 0) {
  10. builder.deleteCharAt(builder.length() - 1);
  11. }
  12. return builder.append(')').toString();
  13. }

commandKey的构造,这里组装了类、方法名、参数,比如本文的实例,commandKey=RemoteProductService#getProduct(int)

配置文件指定

  1. hystrix:
  2. command:
  3. "RemoteProductService#getProduct(int)":
  4. execution:
  5. isolation:
  6. thread:
  7. timeoutInMilliseconds: 500

Java中指定

  1. @Bean
  2. public Feign.Builder feignHystrixBuilder() {
  3. return HystrixFeign.builder().setterFactory(new SetterFactory() {
  4. @Override
  5. public HystrixCommand.Setter create(Target<?> target, Method method) {
  6. return HystrixCommand.Setter
  7. .withGroupKey(HystrixCommandGroupKey.Factory.asKey(RemoteProductService.class.getSimpleName()))// 控制 RemoteProductService 下,所有方法的Hystrix Configuration
  8. .andCommandPropertiesDefaults(
  9. HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(10000) // 超时配置
  10. );
  11. }
  12. });
  13. }

小结

论灵活程度,还是配置文件灵活一点,唯一的工作量就是根据规则构造commandKey,然后就可以进行相关配置了。

  1. import com.netflix.hystrix.HystrixCommand;
  1. HystrixCommand.Setter
  1.  

feignclient设置hystrix参数的更多相关文章

  1. hystrix参数使用方法

    hystrix+feign+ribbon,但是可能很多人都知道hystrix还有线程隔离,信号量隔离,等等各种参数配置,在这几就记录下hystrix的参数, 一.hystrix参数使用方法 通过注解@ ...

  2. Hystrix参数配置

    1.Hystrix参数配置文档  2.Hystrix参数配置示例 import org.springframework.beans.factory.annotation.Autowired; impo ...

  3. tomcat,zookeeper,activeMQ,Kafka设置jvm参数

    1,tomcat设置jvm参数 设置方法:   在tomcat bin 目录增加配置:setenv.sh   #add tomcat pid CATALINA_PID="$CATALINA_ ...

  4. Scala命令设置JVM参数的规则

    Scala下设置JVM参数简单分析 Scala 启动shell脚本,简化后的scala REPL 启动命令大致如下所示: java -Xmx256M -Xms32M \-Xbootclasspath/ ...

  5. 【Unity】13.2 通过Lighting Window设置相关参数

    分类:Unity.C#.VS2015 创建日期:2016-05-19 一.简介 Unity 5.3.4的Lighting Window有3个选项卡:Object.Scene.Lightmaps. 二. ...

  6. 设置JVM参数,查看堆大小

    1.在eclipse设置JVM参数     打开eclipse-窗口-首选项-Java-已安装的JRE(对在当前开发环境中运行的java程序皆生效,也就是在eclipse中运行的java程序)编辑当前 ...

  7. Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致

    Controller里写自己需要的Action,参数的名字必须和路由设置的参数名一致,如果参数不一致,传过去为null

  8. Delphi HTTPRIO控件怎么设置超时参数

    HTTPRIO控件怎么设置超时参数 //HTTPRIO1: THTTPRIO  设置5分钟超时 HTTPRIO1.HTTPWebNode.ConnectTimeout := 5000; Connect ...

  9. javascript:设置URL参数的方法,适合多条件查询

    适用场景:多条件查询情况,如下图所示: 通过设置URL参数,再结合数据源控件设置的RUL参数,就能进行简单的多条件查询了. javascript函数: <mce:script type=&quo ...

随机推荐

  1. vue 通信方式汇总

    1.10种通信方式 10种:https://juejin.im/post/5bd18c72e51d455e3f6e4334 2.除此之外,还有children和ref. 需要注意 $children  ...

  2. HDU 5402 Travelling Salesman Problem(棋盘染色 构造 多校啊)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5402 Problem Description Teacher Mai is in a maze wit ...

  3. 26、线性表(List)

    1.List List接口是Collection的子接口,List是一个可重复集合 2.ArrayList和LinkedList ArrayList和LinkedList是List接口最常见的两个实现 ...

  4. office-word去掉效验红色的波浪线

         工作中,总是能发现不足.能再次学习到知识和经验!

  5. iOS任何界面全屏炫酷倒计时,一句代码就够了

    概述 iOS全屏炫酷倒计时,任何界面只需要调用一句代码就能实现,支持定制倒计时数字.倒计时结束时显示的文本.支持倒计时播放图片.开始倒计时和结束倒计时的block和delegate回调.支持定制文本颜 ...

  6. HTTP1.1协议请求方面参数

    请求信息 GET / HTTP/1.1                                              ->请求行 Accept: */* Accept-Languag ...

  7. HDUOJ----(4788)Hard Disk Drive

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  8. Eclipse 去掉JavaScript Validator

    http://www.cnblogs.com/wucg/archive/2012/08/06/2625458.html eclipse有些时候,javascript文件总是通不过eclipse的编译校 ...

  9. java程序员学习路线图 java程序员进阶路线

  10. Eclipse编译快捷键

    从C#转学Java,总结了一些Eclipse跟Microsoft Visual Studio 向对应的快捷键: 1. Run Ctrl+F11    (VS对应的是F5) 2. Debug F11 ( ...