目录
总系列目录地址
soul-bootstrap-hystrix 配置
依赖 soul-spring-boot-starter-plugin-hystrix
<!-- soul hystrix plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-hystrix</artifactId>
<version>${project.version}</version>
</dependency>
<!-- soul hystrix plugin end-->
soul-admin 修改
打开 hystrix 插件开关
手动增加 hystrix selector 和 rule
soul-examples-http 压测
使用 superbenchmarker
sb -u http://localhost:8188/order/findById?id=121 -c 20 -N 20
压测
sb -u http://localhost:9195/http/order/findById?id=121 -c 20 -N 20
以下两个图,插件 hystrix 已经生效, 由于配置的 MaxConcurrentRequests 比较小,所有会过滤掉部分请求。
在压测期间手动请求会出现报错,{“code”:-103,“message”:“Service invocation exception, or no result is returned!”,“data”:null}
hystrix 也属于插件链里面的一个插件,所以执行方式与前几章的插件一样,只是处理逻辑不一样
hystrix 是什么
soul 使用的是 neflix hystrix 的熔断开源实现。 官网有介绍工作原理
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>${hystrix-core.version}</version>
</dependency>
- hystrix 有两种底层实现: 信号量和线程池
- 结果缓存: 保存同一个key 的结果
- 合并请求: 相邻的请求会被合并一起,再去发送,减少网络消耗
- 其他方式,更详细文章可以看这篇
总结
hystrix 是奈飞公司开源的熔断框架,底层有很多实现机制,并优化到网络层。
soul 的 hystrix 插件实现比较简单易懂,通过页面直接设置参数就可以实现熔断功能。
减少了使用者的接入成本,体会到开源社区的强大,众人拾柴火焰高,也增长了各个方面的知识。