欧美成人午夜免费全部完,亚洲午夜福利精品久久,а√最新版在线天堂,另类亚洲综合区图片小说区,亚洲欧美日韩精品色xxx

扣丁學(xué)堂Java開發(fā)培訓(xùn)SpringBoot中如何使用斷路器詳解

2018-08-27 10:08:38 1214瀏覽

在現(xiàn)如今,隨著互聯(lián)網(wǎng)技術(shù)飛速的發(fā)展和進(jìn)步,越來越來多程序員使用Spring進(jìn)行開發(fā),Spring也慢慢從一個單一簡潔的小框架變成一個大而全的開源軟件,今天扣丁學(xué)堂Java培訓(xùn)老師給大家介紹一下關(guān)于在Spring Boot中如何使用斷路器的詳細(xì)介紹及源碼。



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

closed狀態(tài)的調(diào)用被執(zhí)行,事務(wù)度量被存儲,這些度量是實現(xiàn)一個健康策略所必備的。

倘若系統(tǒng)健康狀況變差,斷路器就處在open狀態(tài)。此種狀態(tài)下,所有調(diào)用會被立即回退并且不會產(chǎn)生新的調(diào)用。open狀態(tài)的目的是給服務(wù)器端回復(fù)和處理問題的時間。

一旦斷路器進(jìn)入一個open狀態(tài),超時計時器開始計時。如果計時器超時,斷路器切換到half-open狀態(tài)。在half-open狀態(tài)調(diào)用間歇性執(zhí)行以確定問題是否已解決。如果解決,狀態(tài)切換回closed狀態(tài)。



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

如何快速使用Hystrix呢?

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方法進(jìn)行降級;

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é)果如下:



正常應(yīng)該返回:



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

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

HystrixCommandAspect的關(guān)鍵代碼如下:

1.方法hystrixCommandAnnotationPointcut()定義攔截注解HystrixCommand

2.方法hystrixCollapserAnnotationPointcut()定義攔截注解HystrixCollapser

3.方法methodsAnnotatedWithHystrixCommand(…)通過@Around(…)攔截所有HystrixCommand和HystrixCollapser注解的方法。詳細(xì)見方法注解

@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;
 }

那么誰來觸發(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(AnnotationMetadatametadata)的方法。此方法會根據(jù)注解啟動的注解(這里指@EnableCircuitBreaker)從spring.factories文件中獲取其配置需要初始化@Configuration類(這里是org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration),從而最終初始化HystrixCommandAspect類,從而實現(xiàn)攔截HystrixCommand的功能

以上就是通過@EnableCircuitBreake可以開啟Hystrix的原理。Hystrix用到了觀察者模式AbstractCommand.executeCommandAndObserve()模式,扣丁學(xué)堂有專業(yè)老師制定的java學(xué)習(xí)路線圖輔助學(xué)員學(xué)習(xí),此外還有與時俱進(jìn)的java課程體系和大量的java視頻教程供學(xué)員觀看學(xué)習(xí),想要學(xué)好java開發(fā)技術(shù)的小伙伴快快行動吧。扣丁學(xué)堂Java技術(shù)交流群:670348138。


扣丁學(xué)堂微信公眾號


【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】



查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>


標(biāo)簽: Java培訓(xùn) Java視頻教程 Java多線程 Java面試題 Java學(xué)習(xí)視頻 Java開發(fā)

熱門專區(qū)

暫無熱門資訊

課程推薦

微信
微博
15311698296

全國免費(fèi)咨詢熱線

郵箱:codingke@1000phone.com

官方群:148715490

北京千鋒互聯(lián)科技有限公司版權(quán)所有   北京市海淀區(qū)寶盛北里西區(qū)28號中關(guān)村智誠科創(chuàng)大廈4層
京ICP備2021002079號-2   Copyright ? 2017 - 2022
返回頂部 返回頂部