Springcloud practical tips: zuul’s path matching

Whether we use the configuration method of traditional routing or service routing, we need to define a matching expression for each routing rule, that is, the path parameter mentioned above. In zuul, the path expression of route matching is defined in ant style.

Ant style path expression is very simple to use. It has the following three wildcards:

We can further understand the meaning of these three wildcards through the examples in the following table and use them by reference:

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, we implemented the user service service at the beginning of system construction and configured the following routing rules:

However, with the iteration of the version, we split some functions of the user service service and split some functions originally belonging to the user service service into another brand-new service user service ext. these split external call URL paths hope to comply with the rules / user service / ext / * *, At this time, we need to add a routing rule to the configuration file. The complete configuration is as follows:

At this time, the URL path calling the user service ext service will actually be matched by the expressions / user service / * * and / user service / ext / * * at the same time. Logically, the API gateway service needs to preferentially select the / user service / ext / * * route, and then match the / user service / * * route to meet the above requirements. However, if the above configuration method is used, such routing priority can not be guaranteed.

From the following routing matching algorithm, we can see that when using routing rules to match the request path, it uses linear traversal. After the request path obtains the first matching routing rule, it will return and end the matching process. Therefore, when there are multiple matching routing rules, the matching results completely depend on the saving order of routing rules.

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 saving of routing rules is orderly, and the content loading is added in turn by traversing the routing rules in the configuration file. Therefore, the root cause of the problem is the reading of the content in the configuration file.

Because the configuration content of properties cannot guarantee order, in order to ensure the priority of routing, we need to use yaml file to configure to realize orderly routing rules, such as the following definitions:

Ignore expression

The ant expression defined by the path parameter has been able to complete the routing rule configuration function on the API gateway. However, zuul also provides an ignore expression parameter: zuul ignored-patterns 。 This parameter can be used to set the URL expression that you do not want to be routed by the API gateway.

For example, based on the example in the quick start, if we don't want the / Hello interface to be routed, we can set it as follows:

Then, you can try to access the / Hello interface of Hello service through the gateway: http://localhost:5555/api -a/hello 。 Although the of the access path fully complies with the / api-a / * * rule defined by the path parameter, because the path complies with zuul The ignored patterns parameter defines the rules, so it will not be routed correctly. At the same time, we can also see the output information of no matching route in the console or log:

In addition, when using this parameter, you should also note that its range is not for one route, but for all routes. Therefore, when setting, you need to comprehensively 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 exchange, you can enter the exchange area to express your ideas or questions.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>