1. 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2. 启动类增加注解:@EnableCircuitBreaker
注解越来越多可以用注解@SpringCloudApplication代替一些注解
3. Controller层控制:
a. 在需要熔断的方法上加@HystrixCommand(fallbackMethod = "saveOrderFail")注解
b. 新建一个saveOrderFail方法,入参和原方法一模一样
4. 内层方法调用控制:
a. 新建一个类实现Feign的接口,这个方法里返回熔断信息
b. Feign接口增加这个类名(@FeignClient(name="product-service",fallback = ProductClientFallBack.class))
c. 配置文件开启Feign
feign:
hystrix:
enabled: true
5. 增加报警机制,可以吧预警信息放redis,加过期时间,过期时间内就不再发送
6. hystrix超时时间默认:1s
配置文件设置
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1000
7. Dashboard监控仪表盘使用
a. 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
b. 启动类加注解@EnableHystrixDashboard
c. 配置文件增加
managment:
endpoints:
web:
exposure:
include: "*"
d. 访问:http://localhost:8781/hystrix
e. 在页面上输入网址:http://localhost:8781/actuator/hystrix.stream
f. 报错:Failed opening connection to http://localhost:8781/actuator/hystrix.stream : 404
原因:访问路径默认是不开启的,增加c的配置,暴露全部监控信息即可
g. 当请求速率达到2/s时,circuit开关会打开