Hystrix多种请求超时原因分析及演示

hystrix是springcloud中扮演断路器的组件,主要是为微服务提供熔断、限流、降级等功能。

本文主要通过代码演示关于hystrix各种超时相关的配置。

演示代码
通过postman调用order服务queryOrderTimeout接口方法,传入一个time参数,用来模拟服务响应时间,使用restTemplate调用logistical服务,睡眠time时间。

order服务接口

@RequestMapping("/queryOrderTimeout")
public String queryOrderTimeout(@RequestParam("time") int time) {
    return orderService.queryOrderTimeout(time);
}

restTemplate调用logistical服务

@HystrixCommand(fallbackMethod = "queryOrderTimeoutFallback",
            commandKey = "queryOrderTimeout")
public String queryOrderTimeout(int time) {
	log.info("queryOrderTimeout request wait time: " + time);
	String url = "http://logistical/logistical/noticeWarehouseTimeout?time=" + time;
	String str = restTemplate.getForObject(url, String.class);
	return str;
}

public String queryOrderTimeoutFallback(int time) {
    log.error("hystrix queryOrderTimeoutFallback...");
    return "queryOrderTimeoutFallback fail: " + time;
}

logistical服务接口

@RequestMapping("/noticeWarehouseTimeout")
public String noticeWarehouseTimeout(@RequestParam("time") int time) throws InterruptedException {
    Thread.sleep(time);
    return "sleep time: " + time;
}

1、同时开启了ribbon超时与hystrix超时设置

//开启ribbon超时管理
ribbon.http.client.enabled=true
//请求超时时间
ribbon.ReadTimeout=2000
//连接超时时间
ribbon.ConnectTimeout=2000
//开启hystrix超时管理
hystrix.command.default.execution.timeout.enabled=true
//hystrix超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

time参数传入1000,即服务响应需要1秒,能够正常响应。
在这里插入图片描述

2020-05-27 20:37:41.162  INFO 21124 --- [erHystrixPool-4] com.wyl.server.service.OrderService      : queryOrderTimeout request wait time: 1000

time参数传入3000,虽然没有达到hystrix设置的5秒,但还是熔断了,因为超过了ribbon的2秒。
在这里插入图片描述

2020-05-27 20:52:58.854  INFO 22864 --- [-OrderService-2] com.wyl.server.service.OrderService      : queryOrderTimeout request wait time: 3000
2020-05-27 20:53:03.855 ERROR 22864 --- [ HystrixTimer-1] com.wyl.server.service.OrderService      : hystrix queryOrderTimeoutFallback...

现在修改如下两个参数,ribbon时间大于hystrix时间,其他不变。

ribbon.ReadTimeout=5000
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000

time参数传入3000,得到的结果与上一个测试一样。
在这里插入图片描述

2020-05-27 20:57:33.142  INFO 7088 --- [-OrderService-2] com.wyl.server.service.OrderService      : queryOrderTimeout request wait time: 3000
2020-05-27 20:57:35.146 ERROR 7088 --- [ HystrixTimer-1] com.wyl.server.service.OrderService      : hystrix queryOrderTimeoutFallback...

time参数传入1000,则正常响应,这里就不再截图了。

到此得出验证,如果ribbon和hystrix同时开启,则以配置最小的为准。

2、同时开启了ribbon超时,关闭hystrix超时设置

ribbon.ReadTimeout=5000
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000
hystrix.command.default.execution.timeout.enabled=false

先是time参数传入1000,能够正常响应,没问题。
在这里插入图片描述

2020-05-27 21:11:38.667  INFO 7088 --- [-OrderService-4] com.wyl.server.service.OrderService      : queryOrderTimeout request wait time: 1000

time参数传入3000,超过了hystrix设置的2000,但是hystrix此时被关闭了,所以也可以正常响应。
在这里插入图片描述

2020-05-27 21:26:48.390  INFO 2340 --- [-OrderService-2] com.wyl.server.service.OrderService      : queryOrderTimeout request wait time: 3000

time参数传入6000,超过了ribbon设置的5000,所以响应失败。
在这里插入图片描述

2020-05-27 21:28:29.040  INFO 2340 --- [-OrderService-3] com.wyl.server.service.OrderService      : queryOrderTimeout request wait time: 6000
2020-05-27 21:28:34.048 ERROR 2340 --- [-OrderService-3] com.wyl.server.service.OrderService      : hystrix queryOrderTimeoutFallback...

到此得出验证,如果关闭了hystrix的超时时间,则请求的超时只依据ribbon中配置的时间。

如果ribbon中配置了重试机制,那么建议hystrix的超时时间为
(1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout,避免在重试完成前被hystrix熔断了。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码拉松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值