七.Hystrix Timeout机制
生活随笔
收集整理的這篇文章主要介紹了
七.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机制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows系统自带WMI应用的查询使
- 下一篇: burp intruder爆破出现 Pa