SpringBoot熔斷機制之CircuitBreaker詳解
Circuit Breaker
熔斷機制在微服務中必不可少,比如故障發(fā)生時怎么處理
熔斷:半熔斷、熔斷打開、熔斷關閉
- 熔斷關閉: 熔斷關閉不會對服務進行熔斷,當請求服務失敗次數(shù)符合設定的規(guī)則則進入熔斷機制
- 半熔斷: 部分請求根據(jù)規(guī)則調用當前服務,如果請求成功且符合規(guī)則則認為當前服務恢復正常,關閉熔斷;
- 熔斷打開:請求不再進行調用當前服務,內部設置時鐘一般為(MTTR:平均故障處理時間),當打開時長達到所設時鐘則進入半熔斷狀態(tài)。
- 基于服務策略觸發(fā)
服務降級
提到熔斷機制還得提下服務降級服務降級往往因為服務壓力過大,比如京東雙促之類的服務降級方式比較多
簡單舉個列子:在各大電商大促銷的時候往往詳情頁有時候是不會看到推薦之類的信息。
熔斷與服務降級關系
- 都是為實現(xiàn)服務高可用
- 最終的表現(xiàn)方式類似
基于Feign實現(xiàn)
- Feign 本身支持Hystrix,不再需要引入相關的jar
- Feign實現(xiàn)只支持類的方式,不支持方法
- 如果啟用 Hytrix則設置 enabled = true
feign: hystrix: enabled: true
基于上次寫的FeignServer module來測試此功能
fallback
簡單的fallback應用,在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調用的是ribbonserver的服務,所以ribbonserver不用啟動
測試結果為: Hello World, it’s fallback with feign
啟動ribbonserver
測試結果為: 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調用的是ribbonserver的服務,所以ribbonserver不用啟動
測試結果為: Hello World, it’s fallback Factory with feign
啟動ribbonserver
測試結果為: 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"; } }
到此這篇關于SpringBoot熔斷機制之CircuitBreaker詳解的文章就介紹到這了,更多相關SpringBoot熔斷機制內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot整合WxJava開啟消息推送的實現(xiàn)
本文主要介紹了SpringBoot整合WxJava開啟消息推送,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-03-03詳解Java弱引用(WeakReference)的理解與使用
這篇文章主要介紹了Java弱引用(WeakReference)的理解與使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04