日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springcloud feign 加上hystrix的流程

發布時間:2025/3/19 编程问答 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的流程的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。