Zuul source code learning

The gateway in spring-cloud is built on the basis of the open source component zuul of netflix company. Let's learn the implementation details of zuul.

zuul-simple-webapp

First, check the loading process through the official demo.


Paste_Image.png

We know the basic structure and running process of a web project. The web.xmlfile is very important. From the file, we can see a running process organized.

StartServer listener

The servlet context is initialized after the container starts. Most of them use static classes to implement simple interest mode to initialize some objects.

  • Initialize the monitor object


    Paste_Image.png


    We can see that the MonitoringHelper.initMocks()method initializes a CounterFactorycounter, the TracerFactorytracker. (both are singleton objects)

  • groovy filter loading

FilterLoader: The filter loader is a singleton object, mainly responsible for loading and compiling files


Paste_Image.png


The filter loader is mainly responsible for finding the file, compiling and executing, among which there is an important FilterRegistryobject, this object is also a singleton, this file represents all the registered filters

FilterFileManager: file manager, singleton object. Mainly responsible for the management of filter files


Paste_Image.png


The file manager is mainly responsible for registering all filter files FilterRegistryin and dynamically monitoring file changes.

  • java filter loading

FilterRegistryThe object is directly registered with the java object.

  • Clean up the container initialization

Paste_Image.png

Core ZuulServlet

Let's take a look at the processing flow of the core servlet.


Paste_Image.png

We need to execute the filter we wrote using groovy, java, and finally call FilterProcessorto execute the filter.
We found that if a user requests, it enters the servlet, and the service is processed in the way of pre->route->post. So have a question ? I access /hello, processing pre is no problem, the same effect as filter, but how can I know that /hello is processed successfully, and run post after success? We interpret the source code with doubts, identify DEBUG, debugRequest in pre, and set the target host url. In the route, this stage is the processing before accessing /hello. First, the request is processed, zuulHeader is added, an http request is encapsulated according to the methods GET, POST, and PUT, and then the result is obtained by accessing the link through httpclient. So now I have some understanding of the idea.


Paste_Image.png

This explains one of my doubts. It turns out that zuul acts as a gateway and is responsible for processing all requests, and then forwarding the proxy to get the results. The java version of the proxy server, I was still thinking about a question before, if the proxy forwards, if it forwards to myself How to do? Whether or not to enter the pre-route again, this is an endless loop, and in the end, I took a detour in understanding and did not understand the author's ideas.

File Upload

We found that servlet initialization can accept a parameter buffer-requests. If it is true, it may mean that you want to upload pictures, you need to use Transfer-Encoding → chunked segmented transmission, then as a proxy server, you need to forward proxy. How to segment proxy forwarding?


Paste_Image.png

We found that the requested file was received using a byte array and then forwarded through httpclient. We found that the file upload function brought by the gateway has performance problems, such as a 5m file, zuul receives 5m, and then forwards 5m by proxy. I feel that the better solution here is to write the route processing file directly on the zuul server and save it in the zuul server or third-party storage. In Qiniu Cloud SDK, file, byte array, and stream methods are provided for uploading. At present, our business is relatively simple, so it is much better to use the first solution. After all, the Qiniu cloud test is OK, but the production environment costs money. It is also a good method to separate the upload function from the gateway. Directly named f.xxx.com, I feel very good.



Author: Yixiao yo
Link: http://www.jianshu.com/p/3b385119ae15
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.
 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327102695&siteId=291194637