In depth analysis of zuul filter built in spring cloud

preface

Spring cloud is written and enabled for zuul by default. What are the functions of these filters? We might as well follow the @ enablezuulserver and @ enablezuulproxy annotations. I believe you are familiar with these two annotations (at least you have seen them). If you feel strange, it doesn't matter. 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 common annotation.

In the official documents of spring cloud, it is only said that @ enablezuulserver is a "blank" zuul, so where is the blank? What is the difference between @ enablezuulproxy and @ enablezuulproxy? Many questions will be answered in this paper.

Before that, let's understand what requestcontext is:

Requestcontext: used to pass messages between filters. Its data is stored in the ThreadLocal of each request. It is used to store where the request is routed, errors, HttpServletRequest and httpservletresponse, all of which are stored in the requestcontext. Requestcontext extends concurrenthashmap, so any data can be stored in the context.

@Enablezuulserver filter

1、 Pre type filter

(1) Servletdetectionfilter: this filter is used to check whether the request passes through the spring dispatcher. After checking, set the Boolean value through isdispatcherservletrequest.

(2) Formbodywrapperfilter: parses the form data and re encodes the request.

(3) Debugfilter: as the name suggests, the filter for debugging can be passed through zuul debug. Request = true, or add the parameter debug = true when requesting, such as $zuul_ HOST:ZUUL_ PORT/path? Debug = true turns on the filter. In this way, the filter will put requestcontext setDebugRouting() 、RequestContext. Setdebugrequest() is set to true.

2、 Route type filter

Sendforwardfilter: this filter uses servlet requestdispatcher to forward requests, and the forwarding location is stored in requestcontext getCurrentContext(). Get ("forward. To"). You can set the route to:

Then visit $zuul_ HOST:ZUUL_ Port / ABC, observe the execution process of the filter.

3、 Post type filter

Sendresponsefilter: write the response of the micro service represented by zuul to the current response.

4、 Error type filter

Senderrorfilter: if requestcontext If getthrowable () is not null, it will be forwarded to / error by default. You can also set error The path property modifies the default forwarding path.

@Enablezuulproxy filter

If the annotation @ enablezuulproxy is used, spring cloud will install the following filters in addition to the above filters:

1、 Pre type filter

Predecorationfilter: this filter determines the address to be routed and how to route according to the provided routelocator. The router can also set various proxy related headers for back-end requests.

2、 Route type filter

(1) Ribbon routing filter: this filter uses ribbon, hystrix and pluggable HTTP clients to send requests. Serviceid is in requestcontext getCurrentContext(). Get ("serviceid"). The filter can use different HTTP clients, such as

(2) Simplehostroutingfilter: this filter sends requests to the specified URL through Apache httpclient. The URL is in requestcontext Getroutehost().

summary

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. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>