Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of RibbonRoutingFilter of Zuul in Springcloud

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the role of Zuul RibbonRoutingFilter in Springcloud". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The version of spring-cloud-netflix-zuul is 2.1.2.release.

Figure 1 is the inheritance diagram of RibbonRoutingFilter.

Fig. 1 inheritance diagram

RibbonRoutingFilter is a ZuulFilter of type roue, as shown in the following List-1, whose filterOrder is 10; construct commandContext first in the run method, and then call the internal method forward.

List-1

@ Overridepublic String filterType () {return ROUTE_TYPE;} @ Overridepublic int filterOrder () {return RIBBON_ROUTING_FILTER_ORDER;} @ Overridepublic boolean shouldFilter () {RequestContext ctx = RequestContext.getCurrentContext (); return (ctx.getRouteHost () = = null & & ctx.get (SERVICE_ID_KEY)! = null & & ctx.sendZuulResponse ());} @ Overridepublic 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);}}

As shown in List-2 below, first build RibbonCommand with ribbonCommandFactory, and then call execute () of RibbonCommand, where RibbonCommandFactory and RibbonCommand are interfaces, with three implementations.

List-2

Protected ClientHttpResponse forward (RibbonCommandContext context) throws Exception {Map 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 and RibbonCommand interface

Public interface RibbonCommand extends HystrixExecutable {} public interface RibbonCommandFactory {T create (RibbonCommandContext context);}

The three implementation classes of RibbonCommandFactory are shown in figure 2 below

Figure 2

HttpClientRibbonCommandFactory is used to build HttpClientRibbonCommand, OkHttpRibbonCommandFactory is used to build OkHttpRibbonCommand, and RestClientRibbonCommandFactory is used to build RestClientRibbonCommand.

This is the end of the content of "what is the role of Zuul's RibbonRoutingFilter in Springcloud". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report