[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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值