- 文档地址
- 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
[java] 深入源码剖析Hystrix降级策略和调整
最新推荐文章于 2023-04-19 15:08:07 发布