您好,登錄后才能下訂單哦!
本篇內容介紹了“Springcloud中Zuul的RibbonRoutingFilter有什么作用”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
spring-cloud-netflix-zuul的版本是2.1.2.release.
圖1是RibbonRoutingFilter的繼承圖
圖1 類繼承圖
RibbonRoutingFilter是個roue類型的ZuulFilter,如下List-1所示,它的filterOrder是10; 在run方法中首先構造commandContext,之后調用內部方法forward。
List-1
@Override public String filterType() { return ROUTE_TYPE; } @Override public int filterOrder() { return RIBBON_ROUTING_FILTER_ORDER; } @Override public boolean shouldFilter() { RequestContext ctx = RequestContext.getCurrentContext(); return (ctx.getRouteHost() == null && ctx.get(SERVICE_ID_KEY) != null && ctx.sendZuulResponse()); } @Override public Object run() { RequestContext context = RequestContext.getCurrentContext(); this.helper.addIgnoredHeaders(); try { RibbonCommandContext commandContext = buildCommandContext(context); ClientHttpResponse response = forward(commandContext); setResponse(response); return response; } catch (ZuulException ex) { throw new ZuulRuntimeException(ex); } catch (Exception ex) { throw new ZuulRuntimeException(ex); } }
如下List-2所示,首先用ribbonCommandFactory構建RibbonCommand,之后調用RibbonCommand的execute(),這里的RibbonCommandFactory和RibbonCommand都是接口,具體實現有三個。
List-2
protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception { Map<String, Object> info = this.helper.debug(context.getMethod(), context.getUri(), context.getHeaders(), context.getParams(), context.getRequestEntity()); RibbonCommand command = this.ribbonCommandFactory.create(context); try { ClientHttpResponse response = command.execute(); this.helper.appendDebug(info, response.getRawStatusCode(), response.getHeaders()); return response; } catch (HystrixRuntimeException ex) { return handleException(info, ex); } }
List-3 RibbonCommandFactory和RibbonCommand接口
public interface RibbonCommand extends HystrixExecutable<ClientHttpResponse> { } public interface RibbonCommandFactory<T extends RibbonCommand> { T create(RibbonCommandContext context); }
RibbonCommandFactory的三個實現類如下圖2
圖2
HttpClientRibbonCommandFactory用于構建HttpClientRibbonCommand, OkHttpRibbonCommandFactory用于構建OkHttpRibbonCommand, RestClientRibbonCommandFactory用于構建RestClientRibbonCommand。
“Springcloud中Zuul的RibbonRoutingFilter有什么作用”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。