Spring Boot中使用斷路器


           斷路器本身是電路上的一種過載保護裝置,當線路中有電器發生短路時,它能夠及時的切斷故障電路以防止嚴重后果發生。通過服務熔斷(也可以稱為斷路)、降級、限流(隔離)、異步RPC等手段控制依賴服務的延遲與失敗,防止整個服務雪崩。一個斷路器可以裝飾並且檢測了一個受保護的功能調用。根據當前的狀態決定調用時被執行還是回退。通常情況下,一個斷路器實現三種類型的狀態:open、half-open以及closed:

  1. closed狀態的調用被執行,事務度量被存儲,這些度量是實現一個健康策略所必備的。
  2. 倘若系統健康狀況變差,斷路器就處在open狀態。此種狀態下,所有調用會被立即回退並且不會產生新的調用。open狀態的目的是給服務器端回復和處理問題的時間。
  3. 一旦斷路器進入一個open狀態,超時計時器開始計時。如果計時器超時,斷路器切換到half-open狀態。在half-open狀態調用間歇性執行以確定問題是否已解決。如果解決,狀態切換回closed狀態。

         斷路器背后的基本思想非常簡單。將受保護的函數調用包裝在斷路器對象中,該對象監視故障。一旦故障達到某個閾值,斷路器就會跳閘,並且所有對斷路器的進一步調用都會返回錯誤,而根本不會進行受保護的呼叫。通常,如果斷路器跳閘,您還需要某種監控器警報。

 如何快速使用Hystrix呢?下面跟着我1234……

1、加入@EnableCircuitBreaker注解

@EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClientspublic class DroolsAppApplication {
    public static void main(String[] args) {
        SpringApplication.run(DroolsAppApplication.class, args);
    }
}

Hystrix整體執行過程,首先,Command會調用run方法,如果run方法超時或者拋出異常,且啟用了降級處理,則調用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、設置超時時間

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

     執行結果如下:

    正常應該返回:

你不要喜歡jinpingmei,要喜歡有意思的書;這樣使用好舒服啊,@EnableCircuitBreaker這個注解就這么強大嗎?

HystrixCommandAspect 通過AOP攔截所有的@HystrixCommand注解的方法,從而使得@HystrixCommand能夠集成到Spring boot中,

HystrixCommandAspect的關鍵代碼如下:

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實現的

@Configuration
public class HystrixCircuitBreakerConfiguration {

@Bean
public HystrixCommandAspect hystrixCommandAspect() {
return new HystrixCommandAspect();
}

那么誰來觸發HystrixCircuitBreakerConfiguration執行初始化

先看spring-cloud-netflix-core**.jar包的spring.factories里有這段配置,是由注解EnableCircuitBreaker觸發

org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\
org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration


那么@EnableCircuitBreaker如何觸發HystrixCircuitBreakerConfiguration
通過源碼查看,此類通過@Import初始化EnableCircuitBreakerImportSelector類

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(EnableCircuitBreakerImportSelector.class)
public @interface EnableCircuitBreaker {
}

          EnableCircuitBreakerImportSelector是SpringFactoryImportSelector子類。此類在初始化后,會執行selectImports(AnnotationMetadata metadata)的方法。此方法會根據注解啟動的注解(這里指@EnableCircuitBreaker)從spring.factories文件中獲取其配置需要初始化@Configuration類(這里是org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration),從而最終初始化HystrixCommandAspect 類,從而實現攔截HystrixCommand的功能

          以上就是通過@EnableCircuitBreake可以開啟Hystrix的原理。Hystrix用到了觀察者模式AbstractCommand.executeCommandAndObserve()模式,下次我們來深入說一下觀察者模式。歡迎拍磚!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM