In this paper, the original address, [] jsbintask blog (https://jsbintask.cn/2019/03/28/springcloud/springcloud-zuul-process/) (best), reprint please indicate the source!

preface

After the company recently decided to use microservices for its latest architecture, it finally chose SpringCloud’s system after discussion. I was in charge of gateway, authentication service development. Keep a journal of what you learn during this time.

Gateway technology selection

Springcloud selects the latest stable version Greenwich, so there are two framework choices for Gateway: SpringCloud Gateway and Zuul. After investigation, I finally choose Zuul, for the following reasons:

  1. The project is currently undergoing a rapid iteration, and Zuul is more stable than Gateway.
  2. The Gateway documentation is still a work in progress, and during my investigation, I found that there was a lot of documentation and even code left on the official websiteTODOThis is a big hole!Gateway document
  3. Gateway is more difficult to use than Zuul. Gateway is developed using Spring5, function-based, responsive programming, and may be new to ZuulReactiveIt is difficult for people to read the source code.
  4. Although Zuul is not as good as Gateway in terms of performance, this time consumption is not as important to our business.
Proxy	    Avg Latency	    Avg Req/Sec/Thread
gateway	    6.61ms	        3.24k
linkered	7.62ms	        2.82k
zuul	    12.56ms	        2.09k
none	    2.09ms	        11.77k
Copy the code

How Zuul works

The key to using Zuul is to customize the Filter, which of course is not the Filter for the Servlet, and different types of filters use the same configuration but have different effects. In the spirit of knowing why, the whole Zuul process source debug again;

The entrance

Zuul’s entry for processing requests is a Servlet: ZuulServlet. SpringCloud also provides another entry for processing, a Servlet Filter: ZuulServletFilter: modify the configuration file zuul.use-filter=true. It enters the ZuulServlet as follows:

The source code walkthrough

  1. Initialize RequestContext, which is actually oneThreadLocal, put httpServlet and httResponse into it for the convenience of customized ZuulFilter.
  2. callpre filter.
  3. callroute filter.
  4. callpost filter.Note: If any exception occurs in the process of calling each filter, it will be callederror filter.Then let’s look at how pre Filter is called:

    In other words, the flow chart above now looks like this:

    The whole process from request entry to zuul call has been sorted out. Next, we just need to start customizing filter to handle our business logic.

conclusion

In this chapter, we compare the advantages and disadvantages of Zuul and Gateway from the perspective of technology selection. Finally, the entire process of Zuul processing requests is summarized by reading the source code. Next chapter: How to customize Zuul Filter and pits encountered!

Keep your eyes on me! This is dry stuff!