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ì)介紹及源碼。
一旦斷路器進(jìn)入一個open狀態(tài),超時計時器開始計時。如果計時器超時,斷路器切換到half-open狀態(tài)。在half-open狀態(tài)調(diào)用間歇性執(zhí)行以確定問題是否已解決。如果解決,狀態(tài)切換回closed狀態(tài)。
@EnableCircuitBreaker @SpringBootApplication @EnableEurekaClient @EnableFeignClientspublic class DroolsAppApplication { public static void main(String[] args) { SpringApplication.run(DroolsAppApplication.class, args); } }
@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"; }
compile("org.springframework.cloud:spring-cloud-starter-hystrix") compile('org.springframework.cloud:spring-cloud-starter-turbine')
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
執(zhí)行結(jié)果如下:
正常應(yīng)該返回:
@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; }
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\ org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @Import(EnableCircuitBreakerImportSelector.class) public @interface EnableCircuitBreaker { }
以上就是通過@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。
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Java開發(fā)資訊”的相關(guān)文章>>