當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringCloud熔断器介绍
生活随笔
收集整理的這篇文章主要介紹了
SpringCloud熔断器介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Hystrix概念
Hystrix?是一個供分布式系統使用,提供延遲和容錯功能,保證復雜的分布系統在面臨不可避免的失敗時,仍能有其彈性。
比如系統中有很多服務,當某些服務不穩定的時候,使用這些服務的用戶線程將會阻塞,如果沒有隔離機制,系統隨時就有可能會掛掉,從而帶來很大的風險。SpringCloud使用Hystrix組件提供斷路器、資源隔離與自我修復功能。下圖表示服務B觸發了斷路器,阻止了級聯失敗
feign結合Hystrix使用
在service的pom中添加依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency><!--hystrix依賴,主要是用 @HystrixCommand --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!--服務注冊--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--服務調用--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>在配置文件中添加hystrix配置
#開啟熔斷機制 feign.hystrix.enabled=true # 設置hystrix超時時間,默認1000ms hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000在service-edu的client包里面創建熔斷器的實現類
@Component public class VodFileDegradeFeignClient implements VodClient {@Overridepublic R removeVideo(String videoId) {return R.error().message("time out");}@Overridepublic R removeVideoList(List videoIdList) {return R.error().message("time out");} }修改VodClient接口的注解
@FeignClient(name = "service-vod", fallback = VodFileDegradeFeignClient.class) @Component public interface VodClient {@DeleteMapping(value = "/eduvod/vod/{videoId}")public R removeVideo(@PathVariable("videoId") String videoId);@DeleteMapping(value = "/eduvod/vod/delete-batch")public R removeVideoList(@RequestParam("videoIdList") List videoIdList); }?
總結
以上是生活随笔為你收集整理的SpringCloud熔断器介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务调用Feign
- 下一篇: SpringCloud(Gateway网