首页 > 其他分享 >乌龙!Hystrix命令执行超时!

乌龙!Hystrix命令执行超时!

时间:2024-06-21 20:44:34浏览次数:12  
标签:TestCommand Hystrix hystrix -- 乌龙 protected 超时 com Setter

我在使用hystrix编写一个模拟命令执行超时的demo。

 1 import com.netflix.hystrix.*;
 2 import lombok.extern.slf4j.Slf4j;
 3 
 4 @Slf4j
 5 public class HystrixTimeoutDemo {
 6 
 7     static class TestCommand extends HystrixCommand<String> {
 8         String param;
 9 
10         protected TestCommand(String param) {
11             super(HystrixCommand.Setter
12                             .withGroupKey(HystrixCommandGroupKey.Factory.asKey("TestCommand"))
13                             .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
14                                     .withExecutionTimeoutInMilliseconds(5000)
15                             )
16                             .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
17                                       .withCircuitBreakerEnabled(false)
18 //                                    .withCircuitBreakerRequestVolumeThreshold(100)
19 //                                    .withCircuitBreakerErrorThresholdPercentage(100)
20 //                                    .withCircuitBreakerSleepWindowInMilliseconds(3000)
21                             )
22             );
23             this.param = param;
24         }
25 
26         @Override
27         protected String run() throws Exception {
28             log.info("{}--RUN start", param);
29             TimeUnit.MILLISECONDS.sleep(2000);
30             log.info("{}--RUN end-------------", param);
31             return param + "--正常返回 ";
32         }
33 
34         @Override
35         protected String getFallback() {
36             return param + "--fallback(降级)";
37         }
38     }
39 
40     public static void main(String[] args) throws IOException {
41         log.info("{}", new TestCommand("command" + 1).execute());
42     }
43 }
View Code

运行代码时,发现一个奇怪的现象。就是,我设置的 命令执行超时时间是 5s。 可是在命令的run方法里sleep了 2s,却提示执行超时了。

10:07:41.961 [hystrix-TestCommand-1] INFO com.serviceshare.HystrixTimeoutDemo - command1--RUN start
10:07:42.970 [main] INFO com.serviceshare.HystrixTimeoutDemo - command1--fallback(降级)

这不禁让我有些疑惑呢。

于是,在我翻查console控制台的log时,我发现了下面hystrix的属性初始化,它把commandExecutionTimeout设置成了默认的1s,这并不是我所期望的————应该是 5s 才对呀!

10:07:41.910 [main] DEBUG com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty - Flipping property: 
hystrix.command.TestCommand.execution.isolation.thread.timeoutInMilliseconds to use NEXT property: hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 1000

 

而后,再分析我的代码,我才意识到是我上面程序的失误。原来,是hystrix构造器的Setter里重复调用了 andCommandPropertiesDefaults,然后后者把前者覆盖了。改正代码后,一切运行正常。

...
10:17:17.301 [main] DEBUG com.netflix.hystrix.strategy.properties.HystrixPropertiesChainedProperty - Flipping property: hystrix.command.TestCommand.execution.isolation.thread.timeoutInMilliseconds to use its current value: 5000
...
10:17:17.350 [hystrix-TestCommand-1] INFO com.serviceshare.HystrixTimeoutDemo - command1--RUN start
10:17:19.355 [hystrix-TestCommand-1] INFO com.serviceshare.HystrixTimeoutDemo - command1--RUN end-------------
10:17:19.360 [main] INFO com.serviceshare.HystrixTimeoutDemo - command1--正常返回

 


关于com.netflix.hystrix.HystrixCommand.Setter

HystrixCommand构造器需要groupKey(命令组键)、commandKey(命令键)、threadPoolKey(线程池键)、commandPropertiesDefaults(命令的属性默认设置)、threadPoolPropertiesDefaults(线程池的属性默认设置)等一系列参数,这在我们使用上会有些不便。而hystrix团队在HystrixCommand里定义了一个Setter内部类,它提供了一组基于链式调用方式的setter方法,可以让开发者更灵活、更流畅地为HystrixCommand构造器赋值。

贴出来它的源码,大家欣赏一下。

/**
 * Fluent interface for arguments to the {@link HystrixCommand} constructor.
 * <p>
 * The required arguments are set via the 'with' factory method and optional arguments via the 'and' chained methods.
 * <p>
 * Example:
 * <pre> {@code
 *  Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("GroupName"))
            .andCommandKey(HystrixCommandKey.Factory.asKey("CommandName"));
 * } </pre>
 * 
 * @NotThreadSafe
 */
final public static class Setter {
    protected final HystrixCommandGroupKey groupKey;
    protected HystrixCommandKey commandKey;
    protected HystrixThreadPoolKey threadPoolKey;
    protected HystrixCommandProperties.Setter commandPropertiesDefaults;
    protected HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults;

    protected Setter(HystrixCommandGroupKey groupKey) {
        this.groupKey = groupKey;
    }

    public static Setter withGroupKey(HystrixCommandGroupKey groupKey) {
        return new Setter(groupKey);
    }

    public Setter andCommandKey(HystrixCommandKey commandKey) {
        this.commandKey = commandKey;
        return this;
    }

    public Setter andThreadPoolKey(HystrixThreadPoolKey threadPoolKey) {
        this.threadPoolKey = threadPoolKey;
        return this;
    }

    public Setter andCommandPropertiesDefaults(HystrixCommandProperties.Setter commandPropertiesDefaults) {
        this.commandPropertiesDefaults = commandPropertiesDefaults;
        return this;
    }

    public Setter andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter threadPoolPropertiesDefaults) {
        this.threadPoolPropertiesDefaults = threadPoolPropertiesDefaults;
        return this;
    }
}

 

标签:TestCommand,Hystrix,hystrix,--,乌龙,protected,超时,com,Setter
From: https://www.cnblogs.com/buguge/p/18261298

相关文章

  • [转帖]深入理解JDBC的超时设置
     https://www.cnblogs.com/Chary/articles/14958848.html 这是最近读到的讲关于JDBC的超时问题最透彻的文章,原文是http://www.cubrid.org/blog/understanding-jdbc-internals-and-timeout-configuration ,网上现有的翻译感觉磕磕绊绊的,很多上下文信息丢失了,这里用我......
  • 异步任务取消、超时
    一、定义异步任务//定义异步任务publicclassAsyncClass{publicstaticasyncTaskTaskAsync(CancellationTokentoken){token.Register(()=>{Console.WriteLine("TaskAsync被取消");});for(inti=0;i<10;i++){......
  • Nginx下载大文件超时配置和请求超时配置等
    https://blog.csdn.net/weixin_42949219/article/details/139354348 Nginx下载大文件超时配置和请求超时配置等location/download{......proxy_bufferingoff; proxy_connect_timeout180s; proxy_send_timeout180s; proxy_read_timeout180s; proxy_se......
  • SpringCloud入门之设置OpenFeign 压缩 超时时间 重试等
    文章目录前言一、为什么要配置二、配置属性1.代码2.yml配置2.1开启Feign日志2.2读取超时和连接超时2.3gzip压缩2.4变更httpclient客户端3.日志输出说明前言通过yml中设置一些属性,就可以让OpenFeign的功能更加强大,它不仅限于服务间的调用,还有请求重试、压缩......
  • python - pip安装三方库超时、安装慢解决办法
    前言:WARNING:Retrying(Retry(total=4,connect=None,read=None,redirect=None,status=None))afterconnectionbrokenby'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org',port=443):今天在一台新的电脑上时候pip下载requests库,报错这个链接超时的错误。我们直......
  • Mariadb版本的JDBC驱动,连接云上Mysql出现连接超时
    记录一下一个小问题的解决,Mariadb驱动连接云上Mysql的时候,如果频繁连接偶尔出现读取超时的现象。通过分析报错的堆栈发现,在 org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol#postConnectionQueries这个方法里面有一个判断,usePipelineAuth默认为true,导致设置sock......
  • 为何超时
    为何超时代码如下defhw(n):sun=0sun=n%10*100+n//10%10*10+n//10//10%10ifsun==n:returnTruereturnFalsedefss(n):foriinrange(2,n+1):w=Trueforjinrange(2,i):ifi%j==0:w=Fa......
  • SRE 排障利器,接口请求超时试试 httpstat
    夜莺资深用户群有人推荐的一个工具,看了一下真挺好的,也推荐给大家。需求场景A服务调用B服务的HTTP接口,发现B服务返回超时,不确定是网络的问题还是B服务的问题,需要排查。工具简介就类似curl,httpstat也可以请求某个后端,而且可以把各个阶段的耗时都展示出来,包括DNS解......
  • 深入解析:MySQL连接超时问题排查与优化策略
    引言​在现代企业应用中,数据库的稳定性和响应速度是保证业务连续性的关键。MySQL作为广泛使用的数据库系统,其连接超时问题可能成为性能瓶颈,影响用户体验和业务效率。本文将深入探讨MySQL连接超时的原因、影响以及优化策略。超时配置详解​查看当前设置​要了解MySQL......
  • 执行 flux bootstrap 时 ansible 超时
    我正在使用HyperledgerBevel管理HyperledgerFabric2.2网络。到目前为止,它一直运行正常,但我在执行游戏本时没有尝试执行一些维护操作。ansible-playbookplatforms/shared/configuration/site.yaml-e"@./build/network-iprd-qa.yaml";这个方法曾经有效,但现在当fl......