Hystrix
系统容错、限流工具
功能
降级
当一个服务调用一个后台服务失败(异常、服务不存在、超时:Hystrix的超时时间默认为1s),可以执行当前服务中的一段"降级代码",
来返回降级结果,如:“系统异常”
添加降级功能:
1、添加Hystrix依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2、启动类添加注解:@EnableCircuitBreaker
3、添加降级代码:
在远程调用的代码上,添加:@HystrixCommoand(fallbackMethod=“降级方法”)
更改hystrix的默认超时时间
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMillisecond: 1000
熔断
服务过热,直接把后台服务断开,限制服务的请求流量
在特定条件下,会自动触发熔断(参数可调):
1)10秒20次请求(必须满足,才会判断第二个条件)
2)50%失败,执行了降级代码
熔断后,所有请求直接执行降级代码,返回降级结果。
半开状态:
断路器打开几秒后,会进入半开状态,向后台服务发送一次用户请求,如果成功,则关闭断路器,回复正常。
如果失败,继续保持断开状态几秒,循环执行。
actuator
SpringBoot提供的项目监控信息工具,提供项目的各种监控日志数据
hystrix利用actuator曝露自己的监控日志,日志端点:hystrix.stream
添加actuator
1、添加actuator依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、曝露监控端点
management:
endpoints:
web:
exposure:
include: "*"
Hystrix Dashboard
1、添加Hystrix Dashboard依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
2、yml配置
配置允许抓取日志
hystrix:
dashboard:
proxy-stream-allow-list: localhost
3、启动类添加@EnableHystrixDashboard注解