Spring Boot中如何使用斷路器詳解
前言
隨著使用 Spring 進行開發(fā)的個人和企業(yè)越來越多,Spring 也慢慢從一個單一簡潔的小框架變成一個大而全的開源軟件,Spring 的邊界不斷的進行擴充,到了后來 Spring 幾乎可以做任何事情了,市面上主流的開源軟件、中間件都有 Spring 對應組件支持,人們在享用 Spring 的這種便利之后,也遇到了一些問題。
斷路器本身是電路上的一種過載保護裝置,當線路中有電器發(fā)生短路時,它能夠及時的切斷故障電路以防止嚴重后果發(fā)生。通過服務熔斷(也可以稱為斷路)、降級、限流(隔離)、異步RPC等手段控制依賴服務的延遲與失敗,防止整個服務雪崩。一個斷路器可以裝飾并且檢測了一個受保護的功能調(diào)用。根據(jù)當前的狀態(tài)決定調(diào)用時被執(zhí)行還是回退。通常情況下,一個斷路器實現(xiàn)三種類型的狀態(tài):open、half-open以及closed:
- closed狀態(tài)的調(diào)用被執(zhí)行,事務度量被存儲,這些度量是實現(xiàn)一個健康策略所必備的。
- 倘若系統(tǒng)健康狀況變差,斷路器就處在open狀態(tài)。此種狀態(tài)下,所有調(diào)用會被立即回退并且不會產(chǎn)生新的調(diào)用。open狀態(tài)的目的是給服務器端回復和處理問題的時間。
- 一旦斷路器進入一個open狀態(tài),超時計時器開始計時。如果計時器超時,斷路器切換到half-open狀態(tài)。在half-open狀態(tài)調(diào)用間歇性執(zhí)行以確定問題是否已解決。如果解決,狀態(tài)切換回closed狀態(tài)。
斷路器背后的基本思想非常簡單。將受保護的函數(shù)調(diào)用包裝在斷路器對象中,該對象監(jiān)視故障。一旦故障達到某個閾值,斷路器就會跳閘,并且所有對斷路器的進一步調(diào)用都會返回錯誤,而根本不會進行受保護的呼叫。通常,如果斷路器跳閘,您還需要某種監(jiān)控器警報。
如何快速使用Hystrix呢?下面跟著我1234……
1、加入@EnableCircuitBreaker注解
@EnableCircuitBreaker @SpringBootApplication @EnableEurekaClient @EnableFeignClientspublic class DroolsAppApplication { public static void main(String[] args) { SpringApplication.run(DroolsAppApplication.class, args); } }
Hystrix整體執(zhí)行過程,首先,Command會調(diào)用run方法,如果run方法超時或者拋出異常,且啟用了降級處理,則調(diào)用getFallback方法進行降級;
2、使用@HystrixCommand注解
@HystrixCommand(fallbackMethod = "reliable") public String readingList() { for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } return "jinpingmei"; } public String reliable() { return "you love interesting book"; }
3、添加引用
compile("org.springframework.cloud:spring-cloud-starter-hystrix") compile('org.springframework.cloud:spring-cloud-starter-turbine')
4、設(shè)置超時時間
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
執(zhí)行結(jié)果如下:
正常應該返回:
你不要喜歡jinpingmei,要喜歡有意思的書;這樣使用好舒服啊,@EnableCircuitBreaker這個注解就這么強大嗎?
HystrixCommandAspect 通過AOP攔截所有的@HystrixCommand注解的方法,從而使得@HystrixCommand能夠集成到Spring boot中,
HystrixCommandAspect的關(guān)鍵代碼如下:
1.方法 hystrixCommandAnnotationPointcut() 定義攔截注解HystrixCommand
2.方法 hystrixCollapserAnnotationPointcut()定義攔截注解HystrixCollapser
3.方法methodsAnnotatedWithHystrixCommand(…)通過@Around(…)攔截所有HystrixCommand和HystrixCollapser注解的方法。詳細見方法注解
@Aspect public class HystrixCommandAspect { private static final Map<HystrixPointcutType, MetaHolderFactory> META_HOLDER_FACTORY_MAP; static { META_HOLDER_FACTORY_MAP = ImmutableMap.<HystrixPointcutType, MetaHolderFactory>builder() .put(HystrixPointcutType.COMMAND, new CommandMetaHolderFactory()) .put(HystrixPointcutType.COLLAPSER, new CollapserMetaHolderFactory()) .build(); } @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)") public void hystrixCommandAnnotationPointcut() { } @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)") public void hystrixCollapserAnnotationPointcut() { } @Around("hystrixCommandAnnotationPointcut() || hystrixCollapserAnnotationPointcut()") public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable { Method method = getMethodFromTarget(joinPoint); Validate.notNull(method, "failed to get method from joinPoint: %s", joinPoint); if (method.isAnnotationPresent(HystrixCommand.class) && method.isAnnotationPresent(HystrixCollapser.class)) { throw new IllegalStateException("method cannot be annotated with HystrixCommand and HystrixCollapser " + "annotations at the same time"); } MetaHolderFactory metaHolderFactory = META_HOLDER_FACTORY_MAP.get(HystrixPointcutType.of(method)); MetaHolder metaHolder = metaHolderFactory.create(joinPoint); HystrixInvokable invokable = HystrixCommandFactory.getInstance().create(metaHolder); ExecutionType executionType = metaHolder.isCollapserAnnotationPresent() ? metaHolder.getCollapserExecutionType() : metaHolder.getExecutionType(); Object result; try { result = CommandExecutor.execute(invokable, executionType, metaHolder); } catch (HystrixBadRequestException e) { throw e.getCause(); } return result; }
那么HystrixCommandAspect是如何初始化,是通過HystrixCircuitBreakerConfiguration實現(xiàn)的
@Configuration public class HystrixCircuitBreakerConfiguration { @Bean public HystrixCommandAspect hystrixCommandAspect() { return new HystrixCommandAspect(); }
那么誰來觸發(fā)HystrixCircuitBreakerConfiguration執(zhí)行初始化
先看spring-cloud-netflix-core**.jar包的spring.factories里有這段配置,是由注解EnableCircuitBreaker觸發(fā)
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\ org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration
那么@EnableCircuitBreaker如何觸發(fā)HystrixCircuitBreakerConfiguration
通過源碼查看,此類通過@Import初始化EnableCircuitBreakerImportSelector類
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import(EnableCircuitBreakerImportSelector.class) public @interface EnableCircuitBreaker { }
EnableCircuitBreakerImportSelector是SpringFactoryImportSelector子類。此類在初始化后,會執(zhí)行selectImports(AnnotationMetadata metadata)的方法。此方法會根據(jù)注解啟動的注解(這里指@EnableCircuitBreaker)從spring.factories文件中獲取其配置需要初始化@Configuration類(這里是org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration),從而最終初始化HystrixCommandAspect 類,從而實現(xiàn)攔截HystrixCommand的功能
以上就是通過@EnableCircuitBreake可以開啟Hystrix的原理。Hystrix用到了觀察者模式AbstractCommand.executeCommandAndObserve()模式,下次我們來深入說一下觀察者模式。歡迎拍磚!
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
java正則表達式表單驗證類工具類(驗證郵箱、手機號碼、qq號碼等)
這篇文章主要介紹了java使用正則表達式進行表單驗證工具類,可以驗證郵箱、手機號碼、qq號碼等方法,需要的朋友可以參考下2014-04-04SpringBoot整合atomikos實現(xiàn)跨庫事務的詳細方案
這篇文章主要介紹了SpringBoot整合atomikos實現(xiàn)跨庫事務,業(yè)務主要涉及政府及企業(yè)且并發(fā)量不大,所以采用XA事務,雖然性能有所損失,但是可以保證數(shù)據(jù)的強一致性,需要的朋友可以參考下2022-06-06java線程池不同場景下使用示例經(jīng)驗總結(jié)
這篇文章主要為大家介紹了java線程池不同場景如何使用的示例源碼及經(jīng)驗總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03深入探討Druid動態(tài)數(shù)據(jù)源的實現(xiàn)方式
Druid是一個高性能的實時分析數(shù)據(jù)庫,它可以處理大規(guī)模數(shù)據(jù)集的快速查詢和聚合操作,在Druid中,動態(tài)數(shù)據(jù)源是一種可以在運行時動態(tài)添加和刪除的數(shù)據(jù)源,使用動態(tài)數(shù)據(jù)源,您可以在Druid中輕松地處理不斷變化的數(shù)據(jù)集,本文講給大家介紹一下Druid動態(tài)數(shù)據(jù)源該如何實現(xiàn)2023-08-08