In-depth analysis of Spring Cloud's built-in Zuul filters

Spring Cloud has some filters written and enabled for Zuul by default. What do these filters do? We might as well follow the two annotations @EnableZuulServer and @EnableZuulProxy. I believe everyone is familiar with these two annotations (at least they have seen them). It doesn't matter if you feel unfamiliar, you can simply understand @EnableZuulProxy as an enhanced version of @EnableZuulServer. In fact, when Zuul is used with Eureka, Ribbon and other components,

@EnableZuulProxy is our commonly used annotation.

In the official documentation of Spring Cloud, it only says that @EnableZuulServer is a "blank" Zuul, so where exactly is the blank? What is the difference between @EnableZuulProxy and @EnableZuulProxy? Many questions will be answered in this article.

Before that, let's understand what RequestContext is:

RequestContext: Used to pass messages between filters. Its data is kept in the ThreadLocal on each request. It is used to store where the request is routed, errors, HttpServletRequest, HttpServletResponse are all stored in the RequestContext. RequestContext extends ConcurrentHashMap, so any data can be stored in the context.

@EnableZuulServer filter

First, the pre type filter

(1) ServletDetectionFilter: This filter is used to check whether the request passes through the Spring Dispatcher. After checking, set a boolean via isDispatcherServletRequest.

(2) FormBodyWrapperFilter: Parse the form data and re-encode the request.

(3) DebugFilter: As the name suggests, a filter for debugging can be passed , or a parameter can be zuul.debug.request=true added when requesting , such as enabling the filter. That way, the filter will set , to true.debug=true$ZUUL_HOST:ZUUL_PORT/path?debug=trueRequestContext.setDebugRouting() RequestContext.setDebugRequest()

Second, the route type filter

SendForwardFilter: This filter forwards requests using the Servlet RequestDispatcher, and the forwarding location is stored in RequestContext.getCurrentContext().get("forward.to") . Routing can be set to:

?
1
2
3
4
5
zuul:
  routes:
  abc:
   path: /abc/**
   url: forward:/abc

Then visit $ZUUL_HOST:ZUUL_PORT/abc and observe the execution of the filter.

Three, post type filter

SendResponseFilter: Write the response of the microservice proxied by Zuul to the current response.

Four, error type filter

SendErrorFilter: If it RequestContext.getThrowable() is not null, it will be forwarded to /error by default, or you can set error.pathproperties to modify the default forwarding path.

@EnableZuulProxy filter

If the annotation @EnableZuulProxy is used, Spring Cloud installs the following filters in addition to the above filters:

First, the pre type filter

PreDecorationFilter: This filter determines the address to route to and how to route based on the provided RouteLocator. The router can also set various proxy-related headers for backend requests.

Second, the route type filter

(1) RibbonRoutingFilter: This filter uses Ribbon, Hystrix and pluggable HTTP clients to send requests. serviceId is RequestContext.getCurrentContext().get("serviceId") in. This filter can be used with different HTTP clients such as

  1. Apache HttpClient: The default HTTP client
  2. Squareup OkHttpClient v3: To use this client, the required com.squareup.okhttp3dependencies must be in the classpath and set ribbon.okhttp.enabled = true.
  3. Netflix Ribbon HTTP client: Set ribbon.restclient.enabled = true to enable this HTTP client. It should be noted that this client has certain limitations, such as not supporting the PATCH method, in addition, it has a built-in retry mechanism.

(2) SimpleHostRoutingFilter: This filter sends a request to the specified URL through Apache HttpClient. URL is RequestContext.getRouteHost() in.

Summarize

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Script Home.

Original link: http://www.itmuch.com/spring-cloud/zuul/zuul-filter-in-spring-cloud/

Guess you like

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