SpringBoot -- 熔斷機制 Circuit Breaker

Circuit Breaker


  • 熔斷機制在微服務中必不可少,比如故障發生時怎麼處理
  • 熔斷:半熔斷、熔斷打開、熔斷關閉

  • 熔斷關閉: 熔斷關閉不會對服務進行熔斷,當請求服務失敗次數符合設定的規則則進入熔斷機制
  • 半熔斷: 部分請求根據規則調用當前服務,如果請求成功且符合規則則認爲當前服務恢復正常,關閉熔斷;
  • 熔斷打開:請求不再進行調用當前服務,內部設置時鐘一般爲(MTTR:平均故障處理時間),當打開時長達到所設時鐘則進入半熔斷狀態。
  • 基於服務策略觸發

服務降級


  • 提到熔斷機制還得提下服務降級
  • 服務降級往往因爲服務壓力過大,比如京東雙促之類的
  • 服務降級方式比較多,簡單舉個列子:在各大電商大促銷的時候往往詳情頁有時候是不會看到推薦之類的信息。
  • 熔斷與服務降級關係

  • 都是爲實現服務高可用
  • 最終的表現方式類似
  • ……

基於Feign實現

  • Feign 本身支持Hystrix,不再需要引入相關的jar
  • Feign實現只支持類的方式,不支持方法
  • 如果啓用 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);
}

創建 FeignServerImpl 實現類,實現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

如果需要觸發來進行熔斷,則需要用 fallbackFactory

在FeignClient中加入 fallbackFactory

@FeignClient(value = "ribbonserver" , fallbackFactory = FeignServerFactoryImpl.class )
public interface FeignServer {
    @RequestMapping(value ="/testRealRibbon",method= RequestMethod.GET)
    String testRealRibbon(@RequestParam("content") String content);

}

創建 FeignServerFactoryImpl 實現類,實現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實現

  • 大致與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";
    }
}

代碼

代碼請移步 Github參考地址

如有疑問請加公衆號(K171),如果覺得對您有幫助請 github start
公衆號_k171

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章