原文出处:
hengyunabc
背景
Hystrix 旨在通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离,请求缓存和请求打包,以及监控和配置等功能。
Dubbo是Alibaba开源的,目前国内最流行的java rpc框架。
本文介绍在spring应用里,怎么把Dubbo和Hystrix结合起来使用。
Spring Boot应用
生成dubbo集成spring boot的应用
对于不熟悉dubbo 集成spring boot应用的同学,可以在这里直接生成dubbo + spring boot的工程: http://start.dubbo.io/
配置spring-cloud-starter-netflix-hystrix
spring boot官方提供了对hystrix的集成,直接在pom.xml里加入依赖:
<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 {
配置Provider端
在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端
对于Consumer端,则可以增加一层method调用,并在method上配置@HystrixCommand
。当调用出错时,会走到fallbackMethod = "reliable"
的调用里。
@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的集成。
传统Spring Annotation应用
传统spring annotation应用的配置其实也很简单,和spring boot应用不同的是:
- 显式配置Spring AOP支持:
@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 AOP原理
在上面的例子里可以看到,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() || hystrixColl