Soul源码阅读 - Hystrix插件使用指南

目标

Soul网关如何使用Hystrix插件集成Hystrix工具

介绍

背景

熔断机制是网关的必备功能之一,所以Soul网关提供了Hystrix插件与Hystrix集成。

Hystrix是一个库,可通过添加延迟和容错逻辑来帮助您控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点,停止服务之间的级联故障并提供后备选项来实现此目的,所有这些都可以提高系统的整体弹性。

Hystrix插件的使用

Soul控制台配置:

在“系统管理” --> "插件管理“ --> "Hystrix" 设置开启

16125042859532.jpg

进入”插件列表“ --> "hystrix"页面

  • 添加”选择器“:
16125044059731.jpg
  • 添加”规则“

规则配置说明:

  • 跳闸最小请求数量 :最小的请求量,至少要达到这个量才会触发熔断
  • 错误百分比阀值 : 这段时间内,发生异常的百分比
  • 最大并发量 : 最大的并发量
  • 跳闸休眠时间(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压测工具,可以进行大并发请求:

 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触发了熔断机制。有些部分请求被拒绝了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容