目標
Soul網關如何使用Hystrix插件集成Hystrix工具
介紹
背景
熔斷機制是網關的必備功能之一迄靠,所以Soul網關提供了Hystrix插件與Hystrix集成憨攒。
Hystrix是一個庫缆巧,可通過添加延遲和容錯邏輯來幫助您控制這些分布式服務之間的交互。Hystrix通過隔離服務之間的訪問點狂鞋,停止服務之間的級聯(lián)故障并提供后備選項來實現(xiàn)此目的,所有這些都可以提高系統(tǒng)的整體彈性却盘。
Hystrix插件的使用
Soul控制臺配置:
在“系統(tǒng)管理” --> "插件管理“ --> "Hystrix" 設置開啟
16125042859532.jpg
進入”插件列表“ --> "hystrix"頁面
- 添加”選擇器“:
16125044059731.jpg
- 添加”規(guī)則“
規(guī)則配置說明:
- 跳閘最小請求數量 :最小的請求量,至少要達到這個量才會觸發(fā)熔斷
- 錯誤百分比閥值 : 這段時間內媳拴,發(fā)生異常的百分比
- 最大并發(fā)量 : 最大的并發(fā)量
- 跳閘休眠時間(ms) :熔斷以后恢復的時間
- 分組Key: 一般設置為:contextPath
- 命令Key: 一般設置為具體的 路徑接口
Soul網關配置
pom.xml中添加如下代碼:
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-hystrix</artifactId>
<version>${project.version}</version>
</dependency>
hystrix測試
- 啟動Soul網關
- 啟動下游服務(soul-examples-http)
我們用wrk壓測工具谷炸,可以進行大并發(fā)請求:
wrk -c100 -t10 -d10s http://127.0.0.1:9195/http/order/findById\?id\=1
然后觀察網關的日志輸出情況:
2021-02-05 16:14:06.184 INFO 19234 --- [work-threads-19] o.d.soul.plugin.base.AbstractSoulPlugin : hystrix selector success match , selector name :http_hystrix
2021-02-05 16:14:06.184 ERROR 19234 --- [-work-threads-3] o.d.soul.plugin.hystrix.HystrixPlugin : hystrix execute have circuitBreaker is Open! groupKey:/http,commandKey:/order/findById
2021-02-05 16:14:06.184 INFO 19234 --- [-work-threads-5] o.d.soul.plugin.base.AbstractSoulPlugin : hystrix selector success match , selector name :http_hystrix
2021-02-05 16:14:06.184 ERROR 19234 --- [work-threads-32] o.d.soul.plugin.hystrix.HystrixPlugin : hystrix execute have circuitBreaker is Open! groupKey:/http,commandKey:/order/findById
2021-02-05 16:14:06.184 INFO 19234 --- [work-threads-19] o.d.soul.plugin.base.AbstractSoulPlugin : hystrix rule success match , rule name :http_hystrix_rule
2021-02-05 16:14:06.184 INFO 19234 --- [-work-threads-5] o.d.soul.plugin.base.AbstractSoulPlugin : hystrix rule success match , rule name :http_hystrix_rule
2021-02-05 16:14:06.184 INFO 19234 --- [work-threads-15] o.d.soul.plugin.base.AbstractSoulPlugin : hystrix selector success match , selector name :http_hystrix
2021-02-05 16:14:06.184 ERROR 19234 --- [work-threads-23] o.d.soul.plugin.hystrix.HystrixPlugin : hystrix execute have circuitBreaker is Open! groupKey:/http,commandKey:/order/findById
2021-02-05 16:14:06.184 ERROR 19234 --- [work-threads-21] o.d.soul.plugin.hystrix.HystrixPlugin : hystrix execute have circuitBreaker is Open! groupKey:/http,commandKey:/order/findById
2021-02-05 16:14:06.184 ERROR 19234 --- [-work-threads-9] o.d.soul.plugin.hystrix.HystrixPlugin : hystrix execute have circuitBreaker is Open! groupKey:/http,commandKey:/order/findById
通過輸出ERROR的日志看到hystrix觸發(fā)了熔斷機制。有些部分請求被拒絕了禀挫。