SpringBoot使用斷路器的方法

本篇文章給大家分享的是有關(guān)Spring Boot使用斷路器的方法,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

公司主營(yíng)業(yè)務(wù):成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出青島免費(fèi)做網(wǎng)站回饋大家。

前言

Netflix創(chuàng)建了一個(gè)名為hystrix的庫(kù)來(lái)實(shí)現(xiàn)斷路器模式。在微服務(wù)體系結(jié)構(gòu)中,有多層服務(wù)調(diào)用是常見(jiàn)的。

在微服務(wù)架構(gòu)中,根據(jù)業(yè)務(wù)來(lái)拆分成一個(gè)個(gè)的服務(wù),服務(wù)與服務(wù)之間可以相互調(diào)用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來(lái)調(diào)用。為了保證其高可用,單個(gè)服務(wù)通常會(huì)集群部署。由于網(wǎng)絡(luò)原因或者自身的原因,服務(wù)并不能保證100%可用,如果單個(gè)服務(wù)出現(xiàn)問(wèn)題,調(diào)用這個(gè)服務(wù)就會(huì)出現(xiàn)線程阻塞,此時(shí)若有大量的請(qǐng)求涌入,Servlet容器的線程資源會(huì)被消耗完畢,導(dǎo)致服務(wù)癱瘓。服務(wù)與服務(wù)之間的依賴(lài)性,故障會(huì)傳播,會(huì)對(duì)整個(gè)微服務(wù)系統(tǒng)造成災(zāi)難性的嚴(yán)重后果,這就是服務(wù)故障的“雪崩”效應(yīng)。為了解決這個(gè)問(wèn)題,業(yè)界提出了斷路器模型。

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

  • closed狀態(tài)的調(diào)用被執(zhí)行,事務(wù)度量被存儲(chǔ),這些度量是實(shí)現(xiàn)一個(gè)健康策略所必備的。
  • 倘若系統(tǒng)健康狀況變差,斷路器就處在open狀態(tài)。此種狀態(tài)下,所有調(diào)用會(huì)被立即回退并且不會(huì)產(chǎn)生新的調(diào)用。open狀態(tài)的目的是給服務(wù)器端回復(fù)和處理問(wèn)題的時(shí)間。
  • 一旦斷路器進(jìn)入一個(gè)open狀態(tài),超時(shí)計(jì)時(shí)器開(kāi)始計(jì)時(shí)。如果計(jì)時(shí)器超時(shí),斷路器切換到half-open狀態(tài)。在half-open狀態(tài)調(diào)用間歇性執(zhí)行以確定問(wèn)題是否已解決。如果解決,狀態(tài)切換回closed狀態(tài)。

Spring Boot使用斷路器的方法

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

Spring Boot使用斷路器的方法

如何快速使用Hystrix呢?

1、加入@EnableCircuitBreaker注解

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

Hystrix整體執(zhí)行過(guò)程,首先,Command會(huì)調(diào)用run方法,如果run方法超時(shí)或者拋出異常,且啟用了降級(jí)處理,則調(diào)用getFallback方法進(jìn)行降級(jí);

Spring Boot使用斷路器的方法 

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è)置超時(shí)時(shí)間

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

執(zhí)行結(jié)果如下:

Spring Boot使用斷路器的方法 

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

Spring Boot使用斷路器的方法 

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

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

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

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

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

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

@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() &#63;
    metaHolder.getCollapserExecutionType() : metaHolder.getExecutionType();
  Object result;
  try {
   result = CommandExecutor.execute(invokable, executionType, metaHolder);
  } catch (HystrixBadRequestException e) {
   throw e.getCause();
  }
  return result;
 }

那么HystrixCommandAspect是如何初始化,是通過(guò)HystrixCircuitBreakerConfiguration實(shí)現(xiàn)的

@Configuration
public class HystrixCircuitBreakerConfiguration {

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

那么誰(shuí)來(lái)觸發(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

通過(guò)源碼查看,此類(lèi)通過(guò)@Import初始化EnableCircuitBreakerImportSelector類(lèi)

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

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

以上就是Spring Boot使用斷路器的方法,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

名稱(chēng)欄目:SpringBoot使用斷路器的方法
本文路徑:http://muchs.cn/article0/ieheio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器網(wǎng)站改版、電子商務(wù)軟件開(kāi)發(fā)、網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)