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開關會打開