目录
官网文档 https://dromara.org/zh/projects/soul/hystrix-plugin/
hystrix插件是网关用来对流量进行熔断的核心实现。
1.启动
在网关的 pom.xml 文件中添加 hystrix starter 依赖
<!-- soul hystrix plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-hystrix</artifactId>
<version>${project.version}</version>
</dependency>
把 soul-admin、soul-bootstrap、soul-examples-http 启动:
2.开启 hystrix 插件
3.添加选择器和规则
添加选择器:
添加规则:
Hystrix处理详解:
- 跳闸最小请求数量 :最小的请求量,至少要达到这个量才会触发熔断
- 错误百分比阀值 : 这段时间内,发生异常的百分比。
- 最大并发量 : 最大的并发量
- 跳闸休眠时间(ms) :熔断以后恢复的时间。
- 分组Key: 一般设置为:contextPath
- 命令Key: 一般设置为具体的 路径接口。
隔离模式有2种,一种是信号量 semaphore:
一种是线程 thread:
4.验证
我们在 OrderController 的 findById 方法内打上断点。
在信号量隔离方式下:
使用 postman 调用,可以看到3秒后,接口超时,返回 104 错误信息,很明显看到响应时间是3.01s,504 网关超时:
紧接着调用,会返回 103 错误信息,很明显可以看到响应时间是4ms,返回500 服务器内部错误,这个接口已经被熔断了。
等过去 5s 后,等熔断恢复,再次调用,还是会返回 504 网关超时错误,证明熔断已恢复,又被断点拦住了。