[java] 深入源碼剖析Hystrix降級策略和調整

- 文檔地址
	- https://github.com/Netflix/Hystrix
	- https://github.com/Netflix/Hystrix/wiki
- 源碼分析Hystrix降級策略和調整
- 查看默認講解策略: HystrixCommandProperties
	- 通過查找配置類可以按需修改默認配置文件(小技巧, 不要去背默認配置)
	- execution.isolation.strategy 隔離策略
		- THREAD 線程池隔離(默認)
		- SEMAPHORE 信號量: 信號量適用於接口併發量高的情況, 如每秒調用數千次的情況, 導致線程開銷過高, 通常只適用於非網絡調用, 執行速度快
	- execution.isolation.thread.timeoutInMilliseconds 超時時間
		- 默認: 1000毫秒
	- execution.timeout.enable 是否開啓超時限制
	- execution.isolation.semaphore.maxConcurrentRequests 隔離策略爲信號量的時候, 如果達到最大併發數時, 後續請求會被拒絕, 默認是10

- 官方文檔: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.strategy

- 調整策略:
	- 超時時間調整

- 示例代碼
	- 通過HistrixCommand註解的方式去更改策略
	@RequestMapping("/get")
    @HystrixCommand(fallbackMethod = "saveOrderfail", commandProperties = {
        @HystrixProperty(name = "", value = "")
    })
    public Object findById(@RequestParam("product_id") Integer productId,
                           @RequestParam(value = "user_id", required = false) Integer userid) {
        Map<String, Object> ret = new HashMap<>();
        Order save = orderService.save(userid, productId);
        ret.put("code", 0);
        ret.put("msg", "請求成功");
        ret.put("data", save);

        return ret;
    }

	- 通過配置文件的方式修改策略(修改關閉超時時間, 建議一定不能關閉)
	修改yml文件
	hystrix:
	    command:
	      	default:
	        	execution:
	          		timeout:
	              		enabled: false
	- 修改Hystrix默認超時時間
		hystrix:
		  command:
		    default:
		      execution:
		        isolation:
		          thread:
		            timeoutInMilliseconds: 4000
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章