Hystrix超时时间设置

  我现在用的环境是springboot:2.3.1、springCloud:Hoxtom.SR6,然后引入的是hystrix依赖是spring-cloud-starter-netflix-hystrix

  1. 方法上加上@HystrixCommand
    @RequestMapping("/getToOrderMemberAll")
        @HystrixCommand(commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000")})
        public List<String> getToOrderMemberAll() throws Exception {
            System.out.println("memberFegin 开始调用member工程。。。" + count++);
            return memberFeign.getToOrderMemberAll();
        }
  2. 全局配置yml文件
    hystrix:
      command:
        default: #也可以针对多个服务
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 4000 # 设置hystrix的超时时间为4000ms

     

  3. 全部配置config
        @Bean
    //    @Scope("prototype")
    //    @ConditionalOnMissingBean
    //    @ConditionalOnProperty(name = "feign.hystrix.enabled")
        public Feign.Builder feignHystrixBuilder() {
            return HystrixFeign.builder().setterFactory((target, 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))
    //                    .andCommandKey(HystrixCommandKey.Factory.asKey(groupKey))
    //                    .andCommandKey(HystrixCommandKey.Factory.asKey(target.type().getSimpleName()))
                        .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                .withExecutionTimeoutInMilliseconds(4000)
                .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD));
            });
        }
    
    
    
    //.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
    //默认的接口方式
    
    //.andCommandKey(HystrixCommandKey.Factory.asKey(groupKey))
    //service-id方式
    
    //.andCommandKey(HystrixCommandKey.Factory.asKey(target.type().getSimpleName()));
    //Feign Client Name方式
    //不知道用什么的 统一用第一个就好

     

最后,可能是我引入的openfeign的依赖,提供者那边接口中sleep了2s,这边的三种方式都不行,后来在官方文档中看到hystrix中关于ribbon的配置,然后就在yml文件中加入了下面的配置,至此,熔断超时设置全部ok!

service-member://提供者服务名
  ribbon:
#    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
#    listOfServers: https://example1.com,http://example2.com
    ConnectTimeout: 2000
    ReadTimeout: 4000
    MaxTotalHttpConnections: 500
    MaxConnectionsPerHost: 100
 
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值