当前位置:
首页 >
springcloud feign 加上hystrix的流程
发布时间:2025/3/19
23
豆豆
生活随笔
收集整理的这篇文章主要介绍了
springcloud feign 加上hystrix的流程
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
一、maven配置
引入feign默认会依赖hystrix,只要不排除就行。 <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId> <!-- <exclusions>--> <!-- <exclusion>--> <!-- <groupId>io.github.openfeign</groupId>--> <!-- <artifactId>feign-hystrix</artifactId>--> <!-- </exclusion>--> <!-- </exclusions>--></dependency>application配置开启feign的hystrix配置。
feign.hystrix.enabled=true二、还是上文那个例子,测试
看到代理对象变为HystrixInvocationHandler,
接着往下调,后面跟单独的feign一样。只是在feignHandler上做了一个包装。
这里要注意,这个线程不是TOMCAT的工作线程,而是使用hystrix中的隔离任务线程池中执行。
正常的线程如下为nio-execute-真正的feign远程接口调用是在hystrix的线程池中。HystrixInvocationHandler.invoke方法为代理方法,最终调用到hystrixCommand.execute()
@Overridepublic Object invoke(final Object proxy, final Method method, final Object[] args)throws Throwable {// early exit if the invoked method is from java.lang.Object// code is the same as ReflectiveFeign.FeignInvocationHandlerif ("equals".equals(method.getName())) {try {Object otherHandler =args.length > 0 && args[0] != null ? Proxy.getInvocationHandler(args[0]) : null;return equals(otherHandler);} catch (IllegalArgumentException e) {return false;}} else if ("hashCode".equals(method.getName())) {return hashCode();} else if ("toString".equals(method.getName())) {return toString();}HystrixCommand<Object> hystrixCommand = new HystrixCommand<Object>(setterMethodMap.get(method)) {@Overrideprotected Object run() throws Exception {try {return HystrixInvocationHandler.this.dispatch.get(method).invoke(args);} catch (Exception e) {throw e;} catch (Throwable t) {throw (Error) t;}}@Overrideprotected Object getFallback() {if (fallbackFactory == null) {return super.getFallback();}try {Object fallback = fallbackFactory.create(getFailedExecutionException());Object result = fallbackMethodMap.get(method).invoke(fallback, args);if (isReturnsHystrixCommand(method)) {return ((HystrixCommand) result).execute();} else if (isReturnsObservable(method)) {// Create a cold Observablereturn ((Observable) result).toBlocking().first();} else if (isReturnsSingle(method)) {// Create a cold Observable as a Singlereturn ((Single) result).toObservable().toBlocking().first();} else if (isReturnsCompletable(method)) {((Completable) result).await();return null;} else {return result;}} catch (IllegalAccessException e) {// shouldn't happen as method is public due to being an interfacethrow new AssertionError(e);} catch (InvocationTargetException e) {// Exceptions on fallback are tossed by Hystrixthrow new AssertionError(e.getCause());}}};if (isReturnsHystrixCommand(method)) {return hystrixCommand;} else if (isReturnsObservable(method)) {// Create a cold Observablereturn hystrixCommand.toObservable();} else if (isReturnsSingle(method)) {// Create a cold Observable as a Singlereturn hystrixCommand.toObservable().toSingle();} else if (isReturnsCompletable(method)) {return hystrixCommand.toObservable().toCompletable();}return hystrixCommand.execute();}
hystrixCommand.execute(),这个就是进入队列,然后在队列进行执行,同步等待。
总结
以上是生活随笔为你收集整理的springcloud feign 加上hystrix的流程的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: spring cloud feign 加
- 下一篇: springboot aop加载流程