當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringCloud熔断器介绍
生活随笔
收集整理的這篇文章主要介紹了
SpringCloud熔断器介绍
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Hystrix概念
Hystrix?是一個(gè)供分布式系統(tǒng)使用,提供延遲和容錯(cuò)功能,保證復(fù)雜的分布系統(tǒng)在面臨不可避免的失敗時(shí),仍能有其彈性。
比如系統(tǒng)中有很多服務(wù),當(dāng)某些服務(wù)不穩(wěn)定的時(shí)候,使用這些服務(wù)的用戶線程將會(huì)阻塞,如果沒有隔離機(jī)制,系統(tǒng)隨時(shí)就有可能會(huì)掛掉,從而帶來很大的風(fēng)險(xiǎn)。SpringCloud使用Hystrix組件提供斷路器、資源隔離與自我修復(fù)功能。下圖表示服務(wù)B觸發(fā)了斷路器,阻止了級(jí)聯(lián)失敗
feign結(jié)合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><!--服務(wù)注冊(cè)--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--服務(wù)調(diào)用--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>在配置文件中添加hystrix配置
#開啟熔斷機(jī)制 feign.hystrix.enabled=true # 設(shè)置hystrix超時(shí)時(shí)間,默認(rèn)1000ms hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=6000在service-edu的client包里面創(chuàng)建熔斷器的實(shí)現(xiàn)類
@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); }?
總結(jié)
以上是生活随笔為你收集整理的SpringCloud熔断器介绍的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务调用Feign
- 下一篇: SpringCloud(Gateway网