In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you SpringCloud Zuul path matching example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
Whether using traditional routing configuration or service routing configuration, we need to define a matching expression for each routing rule, that is, the path parameter mentioned above. In Zuul, the path expression for route matching is defined in Ant style.
Ant-style path expressions are easy to use and have a total of three wildcards:
Wildcard description? Match any single character * match any number of characters * * match any number of characters and support multi-level directories
We can further understand the meaning of these three wildcards and use them for reference through the examples in the following table:
URL path description / user-service/? It can match the path of concatenating a task character after / user-service/, such as / user-service/a, / user-service/b, / user-service/c/user-service/*. It can match the path of any character concatenated after / user-service/, such as / user-service/a, / user-service/aaa, / user-service/bbb. But it cannot match / user-service/a/b/user-service/**. It can match not only the contents contained in / user-service/*, but also multi-level directory paths such as / user-service/a/b.
In addition, when we use wildcards, we often encounter the problem that a URL path may be matched by multiple expressions of different routes. For example, in a scenario like this, we implemented user-service service at the beginning of system construction, and configured the following routing rules:
Zuul.routes.user-service.path=/user-service/**zuul.routes.user-service.serviceId=user-service
However, with the iteration of the version, we split some functions of the user-service service and split some functions that originally belonged to the user-service service into another brand-new service user-service-ext. The external call URL path of these split hopes to comply with the rule / user-service/ext/**. At this time, we need to add a routing rule in the configuration file. The complete configuration is as follows:
Zuul.routes.user-service.path=/user-service/**zuul.routes.user-service.serviceId=user-servicezuul.routes.user-service-ext.path=/user-service/ext/**zuul.routes.user-service-ext.serviceId=user-service-ext
At this point, the URL path that invokes the user-service-ext service is actually matched by both / user-service/** and / user-service/ext/** expressions. Logically, the API gateway service needs to choose the / user-service/ext/** route first, and then match the / user-service/** route to achieve the above requirements. However, if you use the above configuration, in fact, there is no guarantee of such routing priority.
From the following route matching algorithm, we can see that it uses routing rules to match the request path through linear traversal, and then returns and ends the matching process after the request path obtains the first matching route rule. Therefore, when there are multiple matching routing rules, the matching result depends entirely on the order in which the routing rules are saved.
@ Overridepublic Route getMatchingRoute (final String path) {... ZuulRoute route = null; if (! matchesIgnoredPatterns (adjustedPath)) {for (Entry entry: this.routes.get (). EntrySet ()) {String pattern = entry.getKey (); log.debug ("Matching pattern:" + pattern); if (this.pathMatcher.match (pattern, adjustedPath)) {route = entry.getValue (); break;} log.debug ("route matched=" + route); return getRoute (route, adjustedPath);}
The code shown below is the basic routing rule loading algorithm. We can see that these routing rules are saved through LinkedHashMap, that is, the routing rules are saved in an orderly manner, while the loading of content is added in turn by traversing the routing rules in the configuration file, so the root cause of the problem is the reading of the contents in the configuration file.
Protected Map locateRoutes () {LinkedHashMap routesMap = new LinkedHashMap (); for (ZuulRoute route: this.properties.getRoutes (). Values ()) {routesMap.put (route.getPath (), route);} return routesMap;}
Since the configuration of properties is not guaranteed to be orderly, in order to ensure the priority of routing, we need to use the YAML file to implement orderly routing rules, such as using the following definition:
Zuul:routes:user-service-ext:path: / user-service/ext/**serviceId: user-service-extuser-service:path: / user-service/**serviceId: user-service
Ignore expression
The Ant expression defined by the path parameter has been able to configure the routing rules of the API gateway, but in order to configure routing rules more finely and flexibly, Zuul also provides a parameter to ignore the expression: zuul.ignored-patterns. This parameter can be used to set URL expressions that you do not want to be routed by API gateways.
For example, based on the example in getting started, if we don't want the / hello interface to be routed, we can set it like this:
Zuul.ignored-patterns=/**/hello/**zuul.routes.api-a.path=/api-a/**zuul.routes.api-a.serviceId=hello-service
You can then try to access hello-service 's / hello interface: http://localhost:5555/api-a/hello through the gateway. Although the access path fully conforms to the / api-a/** rules defined by the path parameter, because the path conforms to the rules defined by the zuul.ignored-patterns parameter, it will not be routed correctly. At the same time, we can also see the output information that does not match the route in the console or log:
O.s.c.n.z.f.pre.PreDecorationFilter: No route found for uri: / api-a/hello
In addition, when using this parameter, you should also note that its scope is not for a particular route, but for all routes. Therefore, when setting up, you need to fully consider the URL rules to prevent ignoring the URL path that should not be ignored.
If you have any ideas or questions to discuss or communicate, you can enter the exchange area to post your thoughts or questions.
The above is all the contents of the article "sample Analysis of Zuul path matching of SpringCloud". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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: 0
*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.