hystrix插件是网关用来对流量进行熔断的核心实现,今天尝试使用一下。
一、配置环境
1、soul-admin
登录soul-admin 打开 hystrix插件
2、soul-bootstrap
<!-- soul hystrix plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-hystrix</artifactId>
<version>${project.version}</version>
</dependency>
3、启动服务
soul-admin
soul-bootstrap
soul-examples-http
二、配置选择器和选择器规则
1、devide 插件配置
我这里使用的http来测试服务,所以开启了devide插件, soul-examples-http 启动服务后,devide选择器和规则会自动同步
2、hystrix插件配置
开启hystrix插件
配置 hystrix插件选择器和对应的规则,我这里配置的是覆盖所有devide转发的所有接口
隔离模式为信号量
跳闸最小请求数量 :最小的请求量,至少要达到这个量才会触发熔断
错误百分比阀值 : 这段时间内,发生异常的百分比。
最大并发量 : 最大的并发量
跳闸休眠时间(ms) :熔断以后恢复的时间。
三、测试请求
1、通过请求,hystrix selector正常匹配,hystrix 如何起到作用需要通过压测模拟
➜ ~ curl -X GET \
'http://127.0.0.1:9195/http/order/findById?id=5' \
-H 'cache-control: no-cache' \
-H 'postman-token: a16b899f-b48b-e8f1-8196-e3cef80f748a' \
-H 'test: test'
{"id":"5","name":"hello world findById"}% ➜ ~
2021-02-01 18:14:51.844 INFO 95901 --- [-work-threads-8] o.d.soul.plugin.base.AbstractSoulPlugin : hystrix selector success match , selector name :test
2021-02-01 18:14:51.846 INFO 95901 --- [-work-threads-8] o.d.soul.plugin.base.AbstractSoulPlugin : hystrix rule success match , rule name :test-rule
2021-02-01 18:14:55.101 INFO 95901 --- [-work-threads-8] o.d.soul.plugin.base.AbstractSoulPlugin : divide selector success match , selector name :/http
2021-02-01 18:14:55.101 INFO 95901 --- [-work-threads-8] o.d.soul.plugin.base.AbstractSoulPlugin : divide rule success match , rule name :/http/order/findById
2021-02-01 18:14:55.103 INFO 95901 --- [-work-threads-8] o.d.s.plugin.httpclient.WebClientPlugin : The request urlPath is http://10.7.254.31:8188/order/findById?id=5, retryTimes is 0
2021-02-01 18:14:55.120 ERROR 95901 --- [-work-threads-8] o.d.soul.plugin.hystrix.HystrixPlugin : hystrix execute have circuitBreaker is Open! groupKey:test,commandKey:test122123
2、模拟高流量,查看hystrix熔断作用
发起压测
wrk -t12 -c100 -d30s http://127.0.0.1:9195/http/order/findById\?id\=5
同时再次请求接口
➜ ~ curl -X GET \
'http://127.0.0.1:9195/http/order/findById?id=5' \
-H 'cache-control: no-cache' \
-H 'postman-token: a16b899f-b48b-e8f1-8196-e3cef80f748a' \
-H 'test: test'
{"code":500,"message":"Internal Server Error","data":"test122123 short-circuited and fallback failed."}% ➜ ~
四、总结
通过hystrix插件只是简单体验了一下熔断的作用,了解尚浅,去看源码理解起来也很困难。Hystrix是一个开源库,通过添加延迟容忍和容错逻辑,帮助控制分布式服务之间的交互。需要去详细的了解一下Hystrix,才能看懂soul中对Hystrix的使用。