1、常用參數說明
hystrix參數的詳細配置可參照 https://github.com/Netflix/Hystrix/wiki/Configuration
下面是一些常用的配置:
配置項 | 默認值 | 默認屬性 | 實例屬性 |
隔離策略,HystrixCommandKey,如果不配置,默認為方法名 | THREAD | hystrix.command.default.execution.isolation.strategy | hystrix.command.HystrixCommandKey.execution.isolation.strategy |
超時時間,hystrixCommand命令執行超時時間,單位:毫秒 | 1000 | hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds | hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds |
hystrixCommand命令執行是否開啟超時 | true | hystrix.command.default.execution.timeout.enabled | hystrix.command.HystrixCommandKey.execution.timeout.enabled |
超時的時候,是否中斷執行操作 | true | hystrix.command.default.execution.isolation.thread.interruptOnTimeout | hystrix.command.HystrixCommandKey.execution.isolation.thread.interruptOnTimeout |
信號量請求數,當設置為信號量隔離策略時,設置最大允許的請求數 | 10 | hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests | hystrix.command.HystrixCommandKey.execution.isolation.semaphore.maxConcurrentRequests |
CircuitBreaker設置打開fallback並啟動fallback邏輯的錯誤比例 | 50 | hystrix.command.default.circuitBreaker.errorThresholdPercentage | hystrix.command.HystrixCommandKey.circuitBreaker.errorThresholdPercentage |
強制打開斷路器,拒絕所有請求 | false | hystrix.command.default.circuitBreaker.forceOpen | hystrix.command.HystrixCommandKey.circuitBreaker.forceOpen |
當為線程隔離時,核心線程池大小 | 10 | hystrix.threadpool.default.coreSize | hystrix.threadpool.HystrixThreadPoolKey.coreSize |
當隔離策略為線程池隔離模式時,最大線程池大小配置。1.5.9版本中還需要allowMaximumSizeToDivergeFromCore為true | 10 | hystrix.threadpool.default.maximumSize | hystrix.threadpool.HystrixThreadPoolKey.maximumSize |
allowMaximumSizeToDivergeFromCore,該屬性允許配置maximumSize生效 | false | hystrix.threadpool.default.allowMaximumSizeToDivergeFromCore | hystrix.threadpool.HystrixThreadPoolKey.default.allowMaximumSizeToDivergeFromCore |
在真實的項目中,一般會對超時時間、線程池大小、信號量等進行修改,具體需要根據業務,hystrix默認超時1秒,實際項目中,這個時間是肯定不夠的,一般會設置5-10秒,如果有同步上傳的業務,時間需要更長,如果配置了ribbon的時間,其超過時間也需要和ribbon的時間配合使用,一般情況下,ribbon的時間應短於hystrix的超時時間。
hystrix兩種線程隔離方式比對
性能 | 線程池隔離模式(thread) | 信號量隔離模式(semaphore) |
默認 | 是 | 否 |
線程 | 與請求線程分離 | 與請求線程共享 |
開銷 | 上下文頻繁切換,開銷較大 | 較小 |
異步 | 支持 | 不支持 |
應用並發 | 大 | 小 |
適用場景 | 外網交互 | 內網交互 |
hystrix.command.default.execution.isolation.strategy=thread/semaphore
當應用服務需要與外界交互,由於網絡開銷較大,這時選用線程隔離策略,可以保證有剩余的容器線程可用,而不會因為外部原因導致線程一直處於阻塞或者等待,可以快速失敗返回。
當我們的應用只在內網交互,並且量還挺大,這時使用信號量隔離策略就比較好,因為這類應用的響應速度非常快,由於是內網,不會占用容器線程太長時間。
2、hystrix緩存使用
常用的注解說明:
注解 | 說明 |
@CacheResult |
使用該注解后,結果會被緩存,同時它需要和 @HystrixCommand(commandKey = "xxx") 一起使用,注解參數為cacheKeyMethod |
@CacheRemove(commandKey="xxx") |
清除緩存,需要指定commandKey,注解參數為cacheKeyMethod |
@CacheKey |
指定請求命令參數,默認使用方法所有參數作為key,注解屬性為value |
一般在查詢接口上使用@CacheResult,在更新接口上使用@CacheRemove刪除緩存。
在使用hystrix緩存,注意事項:
- 需要開啟 @EnableHystrix
- 需要初始化 HystrixRequestContext
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext; /** * 初始化hystrix上下文 */ public class HystrixContextInterceptor implements HandlerInterceptor { private HystrixRequestContext context; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) { context = HystrixRequestContext.initializeContext(); return true; } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3) { context.shutdown(); } }
- 在指定了 HystrixCommand 的commandKey以后,在@CacheRemove也要指定commandKey