springcloud feign 加上hystrix的流程
生活随笔
收集整理的這篇文章主要介紹了
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加载流程