Hystrix 旨在经过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具有拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。html
Dubbo是Alibaba开源的,目前国内最流行的java rpc框架。java
本文介绍在spring应用里,怎么把Dubbo和Hystrix结合起来使用。git
Demo地址: https://github.com/dubbo/dubbo-samples/tree/master/dubbo-samples-spring-boot-hystrixgithub
对于不熟悉dubbo 集成spring boot应用的同窗,能够在这里直接生成dubbo + spring boot的工程: http://start.dubbo.io/spring
spring boot官方提供了对hystrix的集成,直接在pom.xml里加入依赖:apache
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>1.4.4.RELEASE</version> </dependency>
而后在Application类上增长@EnableHystrix
来启用hystrix starter:缓存
@SpringBootApplication @EnableHystrix public class ProviderApplication {
在Dubbo的Provider上增长@HystrixCommand
配置,这样子调用就会通过Hystrix代理。框架
@Service(version = "1.0.0") public class HelloServiceImpl implements HelloService { @HystrixCommand(commandProperties = { @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000") }) @Override public String sayHello(String name) { // System.out.println("async provider received: " + name); // return "annotation: hello, " + name; throw new RuntimeException("Exception to show hystrix enabled."); } }
对于Consumer端,则能够增长一层method调用,并在method上配置@HystrixCommand
。当调用出错时,会走到fallbackMethod = "reliable"
的调用里。async
@Reference(version = "1.0.0") private HelloService demoService; @HystrixCommand(fallbackMethod = "reliable") public String doSayHello(String name) { return demoService.sayHello(name); } public String reliable(String name) { return "hystrix fallback value"; }
经过上面的配置,很简单地就完成了Spring Boot里Dubbo + Hystrix的集成。ide
Demo地址: https://github.com/dubbo/dubbo-samples/tree/master/dubbo-samples-spring-hystrix
传统spring annotation应用的配置其实也很简单,和spring boot应用不一样的是:
@EnableAspectJAutoProxy
@Configuration
配置HystrixCommandAspect
Bean。@Configuration @EnableDubbo(scanBasePackages = "com.alibaba.dubbo.samples.annotation.action") @PropertySource("classpath:/spring/dubbo-consumer.properties") @ComponentScan(value = {"com.alibaba.dubbo.samples.annotation.action"}) @EnableAspectJAutoProxy static public class ConsumerConfiguration { @Bean public HystrixCommandAspect hystrixCommandAspect() { return new HystrixCommandAspect(); } }
在上面的例子里能够看到,Hystrix对Spring的集成是经过Spring AOP来实现的。下面简单分析下实现。
@Aspect public class HystrixCommandAspect { @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 { if (!metaHolder.isObservable()) { result = CommandExecutor.execute(invokable, executionType, metaHolder); } else { result = executeObservable(invokable, executionType, metaHolder); } } catch (HystrixBadRequestException e) { throw e.getCause() != null ? e.getCause() : e; } catch (HystrixRuntimeException e) { throw hystrixRuntimeExceptionToThrowable(metaHolder, e); } return result; }
HystrixCommandAspect
里定义了两个注解的AspectJ Pointcut:@HystrixCommand
, @HystrixCollapser
。全部带这两个注解的spring bean都会通过AOP处理@Around
AOP处理函数里,能够看到Hystrix会建立出HystrixInvokable
,再经过CommandExecutor
来执行1.@EnableHystrix
引入了@EnableCircuitBreaker
,@EnableCircuitBreaker
引入了EnableCircuitBreakerImportSelector
@EnableCircuitBreaker public @interface EnableHystrix { } @Import(EnableCircuitBreakerImportSelector.class) public @interface EnableCircuitBreaker { }
2.EnableCircuitBreakerImportSelector
继承了SpringFactoryImportSelector<EnableCircuitBreaker>
,使spring加载META-INF/spring.factories
里的EnableCircuitBreaker
声明的配置
在META-INF/spring.factories
里能够找到下面的配置,也就是引入了HystrixCircuitBreakerConfiguration
。
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\ org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration
3.在HystrixCircuitBreakerConfiguration
里能够发现建立了HystrixCommandAspect
@Configuration public class HystrixCircuitBreakerConfiguration { @Bean public HystrixCommandAspect hystrixCommandAspect() { return new HystrixCommandAspect(); }
可见spring-cloud-starter-netflix-hystrix
实际上也是建立了HystrixCommandAspect
来集成Hystrix。
另外spring-cloud-starter-netflix-hystrix
里还有metrics, health, dashboard等集成。
@Service
是一个spring bean,直接在上面配置@HystrixCommand
便可对于dubbo consumer的@Reference
,能够经过加一层简单的spring method包装,配置@HystrixCommand
便可
Hystrix自己提供HystrixCommandAspect
来集成Spring AOP,配置了@HystrixCommand
和@HystrixCollapser
的spring method都会被Hystrix处理