深入Spring Boot:快速集成Dubbo + Hystrix

背景
Hystrix 旨在通過控制那些訪問遠程系統、服務和第三方庫的節點,從而對延遲和故障提供更強大的容錯能力。Hystrix具備擁有回退機制和斷路器功能的線程和信號隔離,請求緩存和請求打包,以及監控和配置等功能。
Dubbo是Alibaba開源的,目前國內最流行的java rpc框架。
本文介紹在spring應用裏,怎麼把Dubbo和Hystrix結合起來使用。
Spring Boot應用
生成dubbo集成spring boot的應用
配置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:br/>@SpringBootApplication
@EnableHystrix
public class ProviderApplication {
br/>配置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") })
br/>@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。br/>@Configuration
@EnableDubbo(scanBasePackages = "com.alibaba.dubbo.samples.annotation.action")
br/>@PropertySource("classpath:/spring/dubbo-consumer.properties")
@ComponentScan(value = {"com.alibaba.dubbo.samples.annotation.action"})
br/>@EnableAspectJAutoProxy
static public class ConsumerConfiguration {

@Bean
public HystrixCommandAspect hystrixCommandAspect() {
return new HystrixCommandAspect();
}
}
Hystrix集成Spring AOP原理
在上面的例子裏可以看到,Hystrix對Spring的集成是通過Spring AOP來實現的。下面簡單分析下實現。br/>@Aspect
public class HystrixCommandAspect {
br/>@Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)")
public void hystrixCommandAnnotationPointcut() {
br/>}
@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來執行
spring-cloud-starter-netflix-hystrix的代碼分析
@EnableHystrix 引入了@EnableCircuitBreaker,@EnableCircuitBreaker引入了EnableCircuitBreakerImportSelector1br/>@EnableCircuitBreaker
public @interface EnableHystrix {
}

@Import(EnableCircuitBreakerImportSelector.class)
public @interface EnableCircuitBreaker {br/>}
EnableCircuitBreakerImportSelector繼承了SpringFactoryImportSelector<EnableCircuitBreaker>,使spring加載META-INF/spring.factories裏的EnableCircuitBreaker聲明的配置在META-INF/spring.factories裏可以找到下面的配置,也就是引入了HystrixCircuitBreakerConfiguration。1
org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker=\
org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration
在HystrixCircuitBreakerConfiguration裏可以發現創建了HystrixCommandAspect1
@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等集成。
總結
對於dubbo provider的@Service是一個spring bean,直接在上面配置@HystrixCommand即可
對於dubbo consumer的@Reference,可以通過加一層簡單的spring method包裝,配置@HystrixCommand即可
Hystrix本身提供HystrixCommandAspect來集成Spring AOP,配置了@HystrixCommand和@HystrixCollapser的spring method都會被Hystrix處理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章