欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

七.Hystrix Timeout机制

发布时间:2025/3/15 编程问答 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 七.Hystrix Timeout机制 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

因为在一个复杂的系统里,可能你的依赖接口的性能很不稳定,有时候2ms,200ms,2s,如果你不对各种依赖接口的调用做超时的控制来给你的服务提供安全保护措施,那么很可能你的服务就被依赖服务的性能给拖死了,大量的接口调用很慢,大量线程就卡死了。

(1)execution.isolation.thread.timeoutInMilliseconds

  手动设置timeout时长,一个command运行超出这个时间,就被认为是timeout,然后将hystrix command标识为timeout,同时执行fallback降级逻辑,默认是1000,也就是1000毫秒。

HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(int value)

 

(2)execution.timeout.enabled

  控制是否要打开timeout机制,默认是true

HystrixCommandProperties.Setter().withExecutionTimeoutEnabled(boolean value)

 

/*** 获取商品信息* @author 张三丰**/ public class GetProductInfoCommand extends HystrixCommand<ProductInfo> {private Long productId;public GetProductInfoCommand(Long productId) {super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ProductInfoService")).andCommandKey(HystrixCommandKey.Factory.asKey("GetProductInfoCommand")).andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("GetProductInfoPool")).andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(10).withMaxQueueSize(12).withQueueSizeRejectionThreshold(15)) .andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withCircuitBreakerRequestVolumeThreshold(30).withCircuitBreakerErrorThresholdPercentage(40).withCircuitBreakerSleepWindowInMilliseconds(3000).withExecutionTimeoutInMilliseconds(500)//超时时间500毫秒.withFallbackIsolationSemaphoreMaxConcurrentRequests(30)) ); this.productId = productId;}@Overrideprotected ProductInfo run() throws Exception {System.out.println("调用接口,查询商品数据,productId=" + productId); if(productId.equals(-2L)) {Thread.sleep(3000); }return JSONObject.parseObject("数据", ProductInfo.class); }@Overrideprotected ProductInfo getFallback() {ProductInfo productInfo = new ProductInfo();productInfo.setName("降级商品"); return productInfo;}}

 

转载于:https://www.cnblogs.com/z-3FENG/p/9696477.html

总结

以上是生活随笔为你收集整理的七.Hystrix Timeout机制的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。