标签:isolation sample boot %s ons 传统 port lstat 框架
背景@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处理
深入Spring Boot:快速集成Dubbo + Hystrix
标签:isolation sample boot %s ons 传统 port lstat 框架
原文地址:https://blog.51cto.com/14158311/2353358