“This is the second day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”

I. Analysis of Zuul gateway architecture

As shown above, the Zuul gateway is divided into three parts. Green part, pink part, blue part.

  1. Filter management module (green module): let developers manage filters, filter management, mainly upload action and activation action. Filters are saved to the database after being uploaded. Developers can manage the status of filters through an interface. For example, canary wire activation

  2. Filter loading module (pink module): is the filter loading module, periodically scan the filter to see if the filter has changed. The filter manager first pulls the changed filters into the local filter directory. If the change is made, the Filter Loader is executed to load it into the Filter Runner of the gateway runtime

  3. Filter runtime module (blue module): Gateway filter runtime module. The runtime module itself is an HTTP servlet that can run in the Tomcat container. When the request comes in, it is first handed to the Zuul servlet, which then passes the request to the Zuul Filter Runner. Zuul Filter Runner is the core component of the entire gateway

The request passes through a pre-routing filter, a routing filter, and a post-routing filter. The core part of the Zuul gateway is the filter chain, which runs the filters in turn. One component that is important is the Request Context, and as requests flow through the Zuul gateway filter chain, they need to share filtering information. For example, the prefilter sets some information for the routing filter to read. The exchange of information is through the Request Context, which is like a store that can be shared between filters, and is thread-safe, with each Request having a local Request Context.

 

Lifecycle of request processing

 

  1. When the request comes in, it first goes through a series of pre filters, pre Filters.

  2. After the pre-filter is processed, the routing filter is entered. The routing filter is the filter that sends requests to the background service and receives responses

  3. After routing filter, the post filter is passed and the response is received. Then the response is returned to the client.

  4. In the filtering process of these three filters, if any error occurs in any link, the error filter will be entered. Error Filter The error filter is sent to the Post filter or the client as a response.

This is a request that is processed at the gateway during its lifetime.

 

Three. Basic concepts of filters

1. Type:

It is defined in the routing process at which the filter is applied. There are pre Routing Filter, routing filter, and Post Routing filter

Define the Order in which the filters are executed in the same type

Each type of filter may have one or more filters, for example, the Pre Routing filter may have multiple filters. They do it in order, not out of order. They will have a custom order.

3. Condition Criteria: Criteria that must be met for filter execution

Should a filter be executed at all while the filter is being executed and streamed? What conditions are met for execution? That’s what the filter condition defines.

4. Action is the Action

What action the filter will perform if the condition is met.

 

Standard filter type

 

1. Pre: Pre-filter

Filters to perform before a request is routed to the original server

  • Certification: certification safety, whether meet the conditions, certification for safety to let go
  • Routing: Which of the following microservices should be invoked when the current request comes in? A or B
  • Request log: Request log, log comes,- write log, monitor it

2. Routing: indicates a route filter

Handles filters that send requests to the source server

3. post

The filter to be executed when the response is returned from the source server

  • Add HTTP headers to the response: Add debugging header logs
  • Collect statistics and metrics: This request, how is its performance, is there an error? You can gather some information
  • The response is streamed back to the client

4. error

A filter to be executed if any of the three filters has an error

 

Example of zuul gateway filter

The Zuul gateway filter is a Groovy script that has a feature that can be compiled dynamically. Uploaded filters are then dynamically compiled and loaded at the Zuul gateway.

Here is a simple pre-routing filter

Class DeviceDelayFilter extends ZuulFilter {// Define the type of filter, pre, routing, Post@override String filterType() {return 'pre'} // Defines the order in which filters are executed @override int filterOrder() {return 100} // Define the conditions of the filter to perform Boolean shouldFilter () {return RequestContext. GetCurrentContext () getRouteHost ()! = null && RequestContext. GetCurrentContext () sendZuulResponse ()} / filter/defined specific logic Object the run () {return null}}Copy the code

Refer to the comment above for the exact meaning of the method.

Zuul has an example of its own filter management, which filters are defined in Zuul

 

This interface is the production process needs to develop their own management interface.