设计工作室营业执照,宁波优化网站排名公司推荐,山东网站建设运营,做电商需要学什么2019独角兽企业重金招聘Python工程师标准 背景 Hystrix 旨在通过控制那些访问远程系统、服务和第三方库的节点#xff0c;从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离#xff0c;请求缓存和请求打包#xff… 2019独角兽企业重金招聘Python工程师标准 背景 Hystrix 旨在通过控制那些访问远程系统、服务和第三方库的节点从而对延迟和故障提供更强大的容错能力。Hystrix具备拥有回退机制和断路器功能的线程和信号隔离请求缓存和请求打包以及监控和配置等功能。 Dubbo是Alibaba开源的目前国内最流行的java rpc框架。 本文介绍在spring应用里怎么把Dubbo和Hystrix结合起来使用。 Spring Boot应用 生成dubbo集成spring boot的应用 配置spring-cloud-starter-netflix-hystrix spring boot官方提供了对hystrix的集成直接在pom.xml里加入依赖 1 2 3 4 5 dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-netflix-hystrix/artifactId version1.4.4.RELEASE/version /dependency 然后在Application类上增加EnableHystrix来启用hystrix starter 1 2 3 SpringBootApplication EnableHystrix public class ProviderApplication { 配置Provider端 在Dubbo的Provider上增加HystrixCommand配置这样子调用就会经过Hystrix代理。 1 2 3 4 5 6 7 8 9 10 11 12 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的调用里。 1 2 3 4 5 6 7 8 9 10 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应用 Demo地址 传统spring annotation应用的配置其实也很简单和spring boot应用不同的是 显式配置Spring AOP支持EnableAspectJAutoProxy显式通过Configuration配置HystrixCommandAspect Bean。 1 2 3 4 5 6 7 8 9 10 11 12 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来实现的。下面简单分析下实现。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 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 PointcutHystrixCommand, HystrixCollapser。所有带这两个注解的spring bean都会经过AOP处理在Around AOP处理函数里可以看到Hystrix会创建出HystrixInvokable再通过CommandExecutor来执行 spring-cloud-starter-netflix-hystrix的代码分析 EnableHystrix 引入了EnableCircuitBreakerEnableCircuitBreaker引入了EnableCircuitBreakerImportSelector 1 2 3 4 5 6 7 EnableCircuitBreaker public interface EnableHystrix { } Import(EnableCircuitBreakerImportSelector.class) public interface EnableCircuitBreaker { } EnableCircuitBreakerImportSelector继承了SpringFactoryImportSelectorEnableCircuitBreaker使spring加载META-INF/spring.factories里的EnableCircuitBreaker声明的配置在META-INF/spring.factories里可以找到下面的配置也就是引入了HystrixCircuitBreakerConfiguration。 1 2 org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker\ org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration 在HystrixCircuitBreakerConfiguration里可以发现创建了HystrixCommandAspect 1 2 3 4 5 6 7 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处理欢迎学Java和大数据的朋友们加入java架构交流 855835163 加群链接https://jq.qq.com/?_wv1027k5dPqXGI 群内提供免费的架构资料还有Java工程化、高性能及分布式、高性能、深入浅出。高架构。性能调优、SpringMyBatisNetty源码分析和大数据等多个知识点高级进阶干货的免费直播讲解 可以进来一起学习交流哦 直播课堂地址https://ke.qq.com/course/260263?flowToken1007014 转载于:https://my.oschina.net/u/3959468/blog/3013486