In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to understand the implementation order of Spring Cloud Gateway Filters, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Based on Spring Cloud Gateway 2.1.1.RELEASE.
Before we talk about the sorting problem of SCG's Filter, we need to compare how Spring Cloud Gateway treats Filter differently from Zuul2.
Scope of Filter
SCG adopts the combination of Global Filter and Route Filter.
Zuul2 is all Global Filter.
What SCG calls Route Filter is something like this:
Spring: cloud: gateway: routes:-id: tomcat_route uri: http://tomcat:8080 predicates:-Path=/tomcat/docs filters:-StripPrefix=1-RemoveRequestHeader=X-Request-Foo
The StripPrefix and RemoveRequestHeader above are Route Filter, while the Global Filter of SCG is implicit and does not need to be explicitly configured. They are called by SCG when the request comes.
In other words, you can configure a different Route, and then configure a different Route Filter for each Route, all of which is decided during the configuration phase.
Zuul2 is all Global Filter, so you have to decide whether to work within each Filter at runtime. In addition, you have to set up the url sent to Origin (the proxied service). Here is an example (from Zuul2 Sample):
Public class Routes extends HttpInboundSyncFilter {@ Override public boolean shouldFilter (HttpRequestMessage httpRequestMessage) {/ /... Return true;} @ Override public HttpRequestMessage apply (HttpRequestMessage request) {/ /... / / Route healthchecks to the healthcheck endpoint.; context.setEndpoint (ZuulEndPointRunner.PROXY_ENDPOINT_FILTER_NAME); context.setRouteVIP ("tomcat"); return request;}} the role of Filter
There is only one Filter in the concept of SCG (leaving aside the difference between Global and Route), which uses code to distinguish between Pre Filter and Post Filter. Routing Filter is also mentioned in the document, which is actually Pre Filter.
Zuul2 appears in the code to provide InboundFilter (responsible for incoming requests), OutboundFilter (responsible for outgoing responses), and ProxyEndpoint (responsible for request to Origin, stringing Inbound and Outbound).
Here is SCG's Pre Filter (tailored from the official example 12.2 Writing Custom GatewayFilter Factories):
Public class PreGatewayFilterFactory extends AbstractGatewayFilterFactory {@ Override public GatewayFilter apply (Config config) {return (exchange, chain)-> {/ / business logic return chain.filter ();};}
An example of Post Filter:
Public class PostGatewayFilterFactory extends AbstractGatewayFilterFactory {@ Override public GatewayFilter apply (Config config) {return (exchange, chain)-> {return chain.filter (exchange) .then (/ * business logic * /);};}
In Zuul2, you have to implement HttpInboundSyncFilter and HttpOutboundSyncFilter,ProxyEndpoint respectively. You don't need to do it yourself.
The problem with SCG Filter
The advantage of SCG is obvious. It does what Zuul2 doesn't do:
Decide which Origin to forward incoming requests to for you. In Zuul2, this is up to you to implement.
The configuration determines which Filter will be applied to this Route. In Zuul2, this is up to you to judge.
However, with the in-depth understanding of SCG, it is found that there are some holes in the execution order of Filter, which is easy to make mistakes if you don't know clearly.
Sorting of Filter
As mentioned earlier, during the execution of SCG, Global Filter and Route Filter are executed together, so what is their order?
Let's take a look at Global Filter first. You can go to / actuator/gateway/globalfilters (see documentation) to get the sort of Global Filter:
So if you write a custom Global Filter, what is its order? It depends on the situation:
If your custom Global Filter implements the Ordered interface or writes the @ Order annotation, then its order is the value it sets itself.
Otherwise, it has no order.
For this, you can see the source code of FilteringWebHandler.java.
Let's take a look at Route Filter, which is also divided into two situations:
If RouteFilter implements the Ordered interface or writes the @ Order annotation, then its order is the value it sets.
Otherwise, its order starts at 1 and is sorted in the order defined in Route.
For this, you can see the source code of RouteDefinitionRouteLocator.java.
Finally, SCG combines the two to do a sort, and for Filter without order, its order defaults to Ordered.LOWEST_PRECEDENCE. For this, you can see the source code of FilteringWebHandler.java.
Summarize with a picture:
Execution order of Filter
First, take a look at this picture in SCG document 3. How It Works:
This diagram probably tells you the process of calling SCG. You can see that it has passed through a bunch of Filters, but it does not tell you the order in which Filter is executed. Then in SCG's 6.1 Combined Global Filter and GatewayFilter Ordering, it is mentioned:
As Spring Cloud Gateway distinguishes between "pre" >
This means that if the Filter is Pre Filter, then the execution order is the same as the sort order, and if the Filter is Post Filter, the execution order is opposite to the sort order. I sorted out the execution order of SCG's native GlobalFilter:
You can see that GatewayMetricsFilter is both a Pre Filter and a Post Filter.
Summary
When you execute a Route, SCG combines Global Filter and Route Filter and sorts:
If Global Filter is not given to order, keep order as null to sort.
Order for Route Filter that is not given to order starts at 1 and gives values in the order defined in Route.
For sorting logic, see AnnotationAwareOrderComparator.
For Pre Filter, the execution order is the same as the sort order
For Post Filter, the execution order is the opposite of the sort order
If you want to customize Global Filter, generally speaking:
Custom Global Pre Filter should be executed before Routing Filter
Custom Global Post Filter should be executed after Routing Filter or after NettyWriteResponseFilter
If you want to customize Route Filter, generally speaking:
Custom Route Pre Filter is between ForwardPathFilter and RouteToRequestUrlFilter, and there is no need to implement the Ordered interface or add @ Order annotations
Custom Route Post Filter is relatively rare. It is executed after Routing Filter or NettyWriteResponseFilter.
On how to understand the order of the implementation of Spring Cloud Gateway Filters to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 277
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.