SpringBoot熔斷機制之CircuitBreaker詳解
Circuit Breaker
熔斷機制在微服務(wù)中必不可少,比如故障發(fā)生時怎么處理
熔斷:半熔斷、熔斷打開、熔斷關(guān)閉
- 熔斷關(guān)閉: 熔斷關(guān)閉不會對服務(wù)進行熔斷,當請求服務(wù)失敗次數(shù)符合設(shè)定的規(guī)則則進入熔斷機制
- 半熔斷: 部分請求根據(jù)規(guī)則調(diào)用當前服務(wù),如果請求成功且符合規(guī)則則認為當前服務(wù)恢復(fù)正常,關(guān)閉熔斷;
- 熔斷打開:請求不再進行調(diào)用當前服務(wù),內(nèi)部設(shè)置時鐘一般為(MTTR:平均故障處理時間),當打開時長達到所設(shè)時鐘則進入半熔斷狀態(tài)。
- 基于服務(wù)策略觸發(fā)
服務(wù)降級
提到熔斷機制還得提下服務(wù)降級服務(wù)降級往往因為服務(wù)壓力過大,比如京東雙促之類的服務(wù)降級方式比較多
簡單舉個列子:在各大電商大促銷的時候往往詳情頁有時候是不會看到推薦之類的信息。
熔斷與服務(wù)降級關(guān)系
- 都是為實現(xiàn)服務(wù)高可用
- 最終的表現(xiàn)方式類似
基于Feign實現(xiàn)
- Feign 本身支持Hystrix,不再需要引入相關(guān)的jar
- Feign實現(xiàn)只支持類的方式,不支持方法
- 如果啟用 Hytrix則設(shè)置 enabled = true
feign:
hystrix:
enabled: true基于上次寫的FeignServer module來測試此功能
fallback
簡單的fallback應(yīng)用,在FeignClient中加入 fallback
@FeignClient(value = "ribbonserver" , fallback = FeignServerImpl.class )
public interface FeignServer {
@RequestMapping(value ="/testRealRibbon",method= RequestMethod.GET)
String testRealRibbon(@RequestParam("content") String content);
}創(chuàng)建 FeignServerImpl 實現(xiàn)類,實現(xiàn)FeignClient的 FeignServer
@Component
public class FeignServerImpl implements FeignServer {
public String testRealRibbon(@RequestParam("content") String content) {
return content + ", it's fallback with feign";
}
}測試驗證
- 啟動 discovery 、configserver、apigateway、feignserver
- 因為feign調(diào)用的是ribbonserver的服務(wù),所以ribbonserver不用啟動
測試結(jié)果為: Hello World, it’s fallback with feign
啟動ribbonserver
測試結(jié)果為: Hello World, for Spring Boot
fallbackFactory
如果需要觸發(fā)來進行熔斷,則需要用 fallbackFactory
在FeignClient中加入 fallbackFactory
@FeignClient(value = "ribbonserver" , fallbackFactory = FeignServerFactoryImpl.class )
public interface FeignServer {
@RequestMapping(value ="/testRealRibbon",method= RequestMethod.GET)
String testRealRibbon(@RequestParam("content") String content);
}創(chuàng)建 FeignServerFactoryImpl 實現(xiàn)類,實現(xiàn)FeignClient的 FeignServer
@Component
public class FeignServerFactoryImpl implements FallbackFactory<FeignServer> {
/**
* Returns an instance of the fallback appropriate for the given cause
*
* @param cause corresponds to {@link AbstractCommand#getFailedExecutionException()}
* often, but not always an instance of {@link FeignException}.
*/
public FeignServer create(Throwable cause) {
return new FeignServer() {
public String testRealRibbon(String content) {
return content + ", it's fallback Factory with feign";
}
};
}
}測試驗證
- 啟動 discovery 、configserver、apigateway、feignserver
- 因為feign調(diào)用的是ribbonserver的服務(wù),所以ribbonserver不用啟動
測試結(jié)果為: Hello World, it’s fallback Factory with feign
啟動ribbonserver
測試結(jié)果為: Hello World, for Spring Boot
基于Ribbon實現(xiàn)
- 大致與Feign差不多,但需要引入 Hystrix,spring-cloud-starter-hystrix
- Feign 因為本身支持 hystrix,所以不需要引入
- @HystrixCommand 指定 fallback的方法
@Controller
public class RibbonController {
@Autowired
RestTemplate restTemplate;
private final static String serverURI = "http://ribbonserver/";
@RequestMapping("/test")
@HystrixCommand(fallbackMethod = "testError")
public String testRibbon(String content) {
System.out.println(content);
restTemplate.getForEntity(serverURI+"testRealRibbon?content="+content,String.class);
return "index";
}
public String testError() {
return "404";
}
}到此這篇關(guān)于SpringBoot熔斷機制之CircuitBreaker詳解的文章就介紹到這了,更多相關(guān)SpringBoot熔斷機制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決spring-cloud-config 多服務(wù)共享公共配置的問題
這篇文章主要介紹了解決spring-cloud-config 多服務(wù)共享公共配置的問題,本文通過多種方法給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
MyBatis中Collection和Association的底層實現(xiàn)原理分析
本文詳細介紹了MyBatis中`<collection>`和`<association>`標簽的底層實現(xiàn)原理,探討了它們?nèi)绾瓮ㄟ^緩存和對象創(chuàng)建機制將數(shù)據(jù)庫結(jié)果集高效地映射為Java對象,文章通過實際的數(shù)據(jù)庫示例和偽代碼示例,展示了這兩個標簽在一對多和一對一關(guān)系中的具體工作方式2025-11-11
大廠禁止SpringBoot在項目使用Tomcat容器原理解析
這篇文章主要為大家介紹了大廠禁止SpringBoot在項目使用Tomcat原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
SpringBoot+WebMagic實現(xiàn)網(wǎng)頁爬蟲的示例代碼
本文是對spring?boot+WebMagic+MyBatis做了整合,使用WebMagic爬取數(shù)據(jù),然后通過MyBatis持久化爬取的數(shù)據(jù)到mysql數(shù)據(jù)庫,具有一定的參考價值,感興趣的可以了解一下2023-10-10
Spring的RedisTemplate的json反序列泛型丟失問題解決
本文主要介紹了Spring RedisTemplate中使用JSON序列化時泛型信息丟失的問題及其提出三種解決方案,可以根據(jù)性能需求選擇方案,下面就來具體了解一下2025-07-07

