Hierarchical index-based API gateway routing matching method, system, computer device and readable storage medium
By optimizing API gateway route matching through a prefix tree based on hierarchical indexes and a matching pattern priority strategy, performance issues in large-scale routing rule scenarios are resolved, achieving efficient route matching and response speed.
Patent Information
- Application Number
- CN202610476249.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-04-10
- Publication Date
- 2026-08-25
AI Technical Summary
Existing API gateways suffer from low route matching efficiency in large-scale routing rule scenarios, leading to performance degradation and response latency, which existing solutions cannot effectively address.
A hierarchical index-based approach is adopted, which uses a prefix tree to construct and match pattern priority, splits the request path into a path segment array, and performs exact matching, prefix matching and wildcard matching in sequence. The routing rule storage strategy is optimized, and the matching efficiency is improved by using tree index and priority filtering mechanism.
The complexity of route matching is optimized from linear growth in the number of rules to being related to the path length, which improves the matching efficiency in large-scale route rule scenarios and reduces response latency and memory overhead.
Smart Images

Figure CN122640338A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to an API gateway routing matching method, system, computer device, and readable storage medium based on hierarchical indexing. Background Technology
[0002] With the evolution of distributed microservice architecture, API gateways, as the unified traffic entry point, undertake critical tasks such as request routing, load balancing, and security authentication. Among these, route matching, as the core processing step of the gateway, is mainly responsible for finding the corresponding backend service address and processing plugin based on metadata characteristics such as request path and method.
[0003] Existing API gateway solutions typically use linear lists to store routing rules and perform path matching by traversing each rule upon receiving a request. However, this approach leads to increased time complexity for matching as the number of rules grows dramatically, resulting in significant performance degradation and response latency. While some solutions introduce hash tables for precise path caching, they still rely on linear scans for wildcard and prefix matching, failing to fundamentally solve the performance problem.
[0004] There is currently no effective solution for improving route matching efficiency in large-scale routing rule scenarios in related technologies. Summary of the Invention
[0005] Therefore, it is necessary to provide an API gateway route matching method, system, computer device, and readable storage medium based on hierarchical indexing that can improve route matching efficiency in large-scale routing rule scenarios, addressing the aforementioned technical problems.
[0006] Firstly, this application provides an API gateway route matching method based on hierarchical indexing, including:
[0007] Receive request information, the request information including a request path;
[0008] Obtain the request path from the request information, and split the request path into an array of path segments;
[0009] Based on the path segment array, routing rules are matched sequentially in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode;
[0010] If a routing rule is successfully matched in the current matching mode, the matching stops and the corresponding routing rule is returned.
[0011] In one embodiment, the successful matching of routing rules in the exact matching mode includes: performing a step-by-step search starting from the root node of the prefix tree based on the path segment array; if each path segment in the path segment array has a corresponding node in the prefix tree, and the node corresponding to the last path segment in the path segment array has an end marker, then the routing rule in the exact matching mode is determined to be successfully matched.
[0012] Successful matching of routing rules in the prefix matching mode includes: searching step by step from the root node of the prefix tree based on the path segment array; if all fixed path segments in the path segment array have corresponding nodes in the prefix tree, and the node corresponding to the last fixed path segment in the fixed path segment has a prefix matching identifier, then the routing rule matching in the prefix matching mode is determined to be successful.
[0013] Successful matching of routing rules in the wildcard matching mode includes: searching level by level from the root node of the prefix tree based on the path segment array, determining the last node that matches the prefix tree as the target node, and determining whether the routing rule associated with the target node matches the request path. If so, the routing rule matching in the wildcard matching mode is successful.
[0014] In one embodiment, the process of sequentially performing routing rule matching further includes:
[0015] Obtain multiple routing rules, and construct the prefix tree based on the multiple routing rules;
[0016] Determine the matching pattern corresponding to the routing rule, and execute the routing rule storage policy corresponding to the matching pattern.
[0017] In one embodiment, constructing the prefix tree based on multiple routing rules includes:
[0018] Parse multiple routing rules and divide the routing rules into multiple path segments according to a preset separation logic to obtain a path segment array;
[0019] The prefix tree is constructed based on multiple arrays of path segments, wherein each node in the prefix tree represents one of the path segments.
[0020] In one embodiment, the execution of the routing rule storage strategy corresponding to the matching pattern includes:
[0021] If the matching mode is the exact matching mode, then the routing rule is stored in the node in the prefix tree corresponding to the complete path of the routing rule, and an end marker is set for the node;
[0022] If the matching mode is prefix matching mode, then all fixed path segments in the routing rules are extracted, and the routing rules are stored in the node corresponding to the last fixed path segment in the prefix tree, and a prefix matching identifier is set for the node;
[0023] If the matching mode is the wildcard matching mode, then the preceding path of the routing rule is stored in the corresponding preceding node in the prefix tree, and the routing rule is stored in the first storage area, wherein the preceding node stores index information pointing to the routing rule in the first storage area;
[0024] If the matching mode is a regular expression matching mode, the routing rule is stored in the second storage area.
[0025] In one embodiment, after performing routing rule matching in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode, the method further includes:
[0026] If the routing rule matching fails in the exact matching mode, prefix matching mode, and wildcard matching mode, then the routing rule matching is performed according to the regular expression matching mode.
[0027] If the routing rule matches successfully in the regular expression matching mode, the corresponding routing rule is returned.
[0028] In one embodiment, after obtaining the request path in the request information and before splitting the request path into an array of path segments, the method further includes: determining whether the request path matches the routing rules already cached in the cache area;
[0029] If yes, then stop matching and return the corresponding routing rule; if no, then split the request path into an array of path segments.
[0030] After a routing rule is successfully matched in the current matching mode, the following also applies:
[0031] The successfully matched routing rules are cached in the cache area.
[0032] Secondly, this application also provides an API gateway routing matching system based on hierarchical indexing, including:
[0033] A request receiving module is used to receive request information, including a request path.
[0034] The request processing module is used to obtain the request path from the request information and split the request path into an array of path segments;
[0035] The routing rule matching module is used to perform routing rule matching in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode and wildcard matching mode based on the path segment array; if the routing rule is successfully matched in the current matching mode, the matching is stopped and the corresponding routing rule is returned.
[0036] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0037] Receive request information, the request information including a request path;
[0038] Obtain the request path from the request information, and split the request path into an array of path segments;
[0039] Based on the path segment array, routing rules are matched sequentially in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode;
[0040] If a routing rule is successfully matched in the current matching mode, the matching stops and the corresponding routing rule is returned.
[0041] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, performs the following steps:
[0042] Receive request information, the request information including a request path;
[0043] Obtain the request path from the request information, and split the request path into an array of path segments;
[0044] Based on the path segment array, routing rules are matched sequentially in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode;
[0045] If a routing rule is successfully matched in the current matching mode, the matching stops and the corresponding routing rule is returned.
[0046] The aforementioned API gateway routing matching method, system, computer device, and readable storage medium based on hierarchical indexing employ the following steps: receiving request information, including a request path; obtaining the request path from the request information and splitting it into an array of path segments; based on the path segment array, sequentially performing routing rule matching in a pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode; if a routing rule matches successfully in the current matching mode, matching stops and the corresponding routing rule is returned. This optimizes the routing matching complexity from linear growth with the number of rules to being related to the path length, and improves routing matching efficiency in large-scale routing rule scenarios through tree indexing and priority filtering mechanisms. Attached Figure Description
[0047] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0048] Figure 1 This is an application environment diagram of an API gateway routing matching method based on hierarchical index in one embodiment;
[0049] Figure 2 This is a flowchart illustrating an API gateway routing matching method based on hierarchical indexing in one embodiment;
[0050] Figure 3 This is a flowchart illustrating an API gateway routing matching method based on hierarchical indexing in an example embodiment.
[0051] Figure 4 This is a block diagram of an API gateway routing matching system based on hierarchical indexing in one embodiment.
[0052] Figure 5 This is an internal structural diagram of a computer device in an example embodiment. Detailed Implementation
[0053] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0054] This application provides an API gateway routing matching method based on hierarchical indexing, which can be applied to, for example... Figure 1In the application environment shown, terminal 102 communicates with server 104 via a network. A data storage system can store the data that server 104 needs to process. The data storage system can be integrated onto server 104 or placed on a cloud or other network server. A user sends request information to server 104 through terminal 102, the request information including a request path. Server 104 receives the request information; obtains the request path from the request information and splits the request path into a path segment array; based on the path segment array, it sequentially performs routing rule matching in a pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode; if the routing rule matches successfully in the current matching mode, it stops matching and returns the corresponding routing rule. Server 104 sends the business response result obtained based on the corresponding routing rule to terminal 102. Terminal 102 can be, but is not limited to, various personal computers, laptops, smartphones, and tablets. Server 104 can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services.
[0055] In one embodiment, such as Figure 2 As shown, an API gateway route matching method based on hierarchical indexing is provided, the method including the following steps:
[0056] Step 202: Receive request information, which includes a request path.
[0057] Optionally, the request information may also include a request method.
[0058] Step 204: Obtain the request path from the request information and split the request path into an array of path segments.
[0059] Optionally, the request path can be split into an array of path segments according to a preset separation logic.
[0060] Step 206: Based on the path segment array, the routing rules are matched sequentially in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode and wildcard matching mode.
[0061] Optionally, the exact match mode is set to high priority, the prefix match mode to medium priority, and the wildcard match mode to low priority. The sequential execution of routing rule matching includes: performing routing rule matching in descending order of priority for the exact match mode, prefix match mode, and wildcard match mode.
[0062] Step 208: If the routing rule is successfully matched in the current matching mode, stop matching and return the corresponding routing rule.
[0063] The above-described API gateway route matching method based on hierarchical indexing involves receiving request information, including a request path; obtaining the request path from the request information and splitting it into an array of path segments; based on the path segment array, sequentially performing route rule matching in a pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode; if a route rule matches successfully in the current matching mode, matching stops and the corresponding route rule is returned. This optimizes the route matching complexity from linear growth with the number of rules to being related to the path length, and improves route matching efficiency in scenarios with large-scale route rules through tree indexing and priority filtering mechanisms.
[0064] In one embodiment, a successful matching of a routing rule in the exact matching mode includes: performing a step-by-step search starting from the root node of the prefix tree based on the path segment array; if each path segment in the path segment array has a corresponding node in the prefix tree, and the node corresponding to the last path segment in the path segment array has an end marker, then the routing rule in the exact matching mode is determined to be successfully matched.
[0065] Successful matching of routing rules in the prefix matching mode includes: searching step by step from the root node of the prefix tree based on the path segment array; if all fixed path segments in the path segment array have corresponding nodes in the prefix tree, and the node corresponding to the last fixed path segment in the fixed path segment has a prefix matching identifier, then the routing rule matching in the prefix matching mode is determined to be successful.
[0066] Successful matching of routing rules in the wildcard matching mode includes: searching level by level from the root node of the prefix tree based on the path segment array, determining the last node that matches the prefix tree as the target node, and determining whether the routing rule associated with the target node matches the request path. If so, the routing rule matching in the wildcard matching mode is successful.
[0067] The last path segment is the last path segment in the sequence position of the path segment array.
[0068] Optionally, a path matching tool can be used to determine whether the routing rules associated with the target node match the request path. The path matching tool can be the AntPathMatcher tool based on the Spring framework.
[0069] In this embodiment, the matching logic of each matching mode is defined. By verifying the routing rules associated with the identifier and the target node, the accuracy of the determination of complex paths under different matching modes is ensured.
[0070] In one embodiment, before sequentially performing routing rule matching, the method further includes: obtaining multiple routing rules, constructing the prefix tree based on the multiple routing rules; determining the matching mode corresponding to the routing rule, and executing the routing rule storage strategy corresponding to the matching mode.
[0071] Optionally, if the path string of the routing rule does not contain a preset matching special character, then the matching mode corresponding to the routing rule is determined to be an exact matching mode; if the path string of the routing rule ends with a preset prefix identifier, and the path part before the identifier is a fixed string, then the matching mode corresponding to the routing rule is determined to be a prefix matching mode; if the path string of the routing rule contains a path placeholder, and the path string does not conform to the characteristics of the prefix matching mode, then the matching mode corresponding to the routing rule is determined to be a wildcard matching mode; if the path string of the routing rule contains a regular expression metacharacter or the routing rule is marked with a preset regular expression guide, and the routing rule does not belong to the above-mentioned exact matching mode, prefix matching mode, and wildcard matching mode, then the matching mode corresponding to the routing rule is determined to be a regular expression matching mode.
[0072] It is understandable that, in order to optimize the gateway's startup speed and memory overhead, multiple routing rules are obtained before receiving request information containing the request path, and these multiple routing rules are stored in a third storage area; after receiving request information containing the request path and before sequentially executing routing rule matching, the prefix tree is constructed based on the multiple routing rules; the matching mode corresponding to the routing rule is determined, and the routing rule storage strategy corresponding to the matching mode is executed.
[0073] Optionally, when performing routing rule matching sequentially, if a new routing rule is to be added, the matching mode corresponding to the new routing rule is determined, the routing rule storage policy corresponding to the matching mode is executed, and the cache of the affected routing rules is cleared; if a routing rule is to be deleted, the matching mode corresponding to the routing rule to be deleted is determined, and the routing rule storage policy corresponding to the matching mode is used for clearing; if a routing rule is to be modified, the matching mode corresponding to the routing rule to be modified is determined, the routing rule storage policy corresponding to the matching mode is used for modification, and the cache of the affected routing rules is cleared.
[0074] Optionally, read-write locks or thread-safe hash tables can be used to ensure concurrency safety when adding, deleting, and modifying the routing rule, and memory message queues or event queues can be used to make update operations asynchronous, avoiding blocking request threads.
[0075] This embodiment constructs a multi-level index structure, providing a foundation for the rapid location and pruning of routing rules.
[0076] In one example implementation, if the routing rule is / health, then the matching mode corresponding to the routing rule is determined to be an exact match mode; if the routing rule is If the routing rule is determined to be a prefix matching mode, then the matching mode corresponding to the routing rule is determined to be a prefix matching mode; if the routing rule is If the routing rule is / user / \d+, then the matching mode corresponding to the routing rule is determined to be a wildcard matching mode; if the routing rule is / user / \d+, then the matching mode corresponding to the routing rule is determined to be a regular expression matching mode.
[0077] In one example implementation, when routing rule matching is performed sequentially, if it is necessary to change the routing rule... At that time, all need to be cleared. cache.
[0078] In one embodiment, constructing the prefix tree based on multiple routing rules includes: parsing multiple routing rules and dividing the routing rules into multiple path segments according to a preset separation logic to obtain a path segment array; constructing the prefix tree based on the multiple path segment arrays, wherein each node in the prefix tree represents one of the path segments.
[0079] Optionally, dividing the routing rule into multiple path segments according to a preset delimiter includes dividing the routing rule into multiple path segments according to a preset delimiter.
[0080] Optionally, a corresponding routing rule identifier or logical judge is associated with the leaf node or intermediate node of the prefix tree.
[0081] Optionally, to quickly locate routing rules, the prefix tree supports longest prefix matching.
[0082] Optionally, an array of path segments with only a single child node can be identified and the array of path segments can be merged into a single compressed node to reduce the depth of the prefix tree.
[0083] Optionally, the health of the prefix tree can be monitored, and if the prefix tree is abnormal, it can be downgraded to linear matching to ensure availability.
[0084] Optionally, the prefix tree supports importing multiple routing rules at once, and batch updates of the prefix tree are achieved through a merge construction process.
[0085] This embodiment enables the construction of a hierarchical physical index skeleton, saves memory overhead by sharing nodes of common prefix path segments, and supports fast step-by-step location of request path segment arrays.
[0086] In one example implementation, if the first routing rule is / ap / user / info, it can be divided into multiple path segments according to a preset separator. The path segment array composed of these multiple path segments is ["ap", "user", "info"]. Based on this, a node chain corresponding to the path segment array is constructed level by level in the prefix tree from the root node to the leaf node. In this node chain, the node corresponding to each subsequent path segment is a direct child node of the node corresponding to the previous path segment, thereby forming a path branch in the prefix tree that is consistent with the semantics of the routing rule. If the second routing rule is / ap / order / detail, the first routing rule and the second routing rule share the / ap node. During the query, rules that do not start with / ap can be quickly skipped to support fast location.
[0087] In one embodiment, the execution of the routing rule storage strategy corresponding to the matching mode includes: if the matching mode is the exact matching mode, storing the routing rule in the node corresponding to the complete path of the routing rule in the prefix tree, and setting an end identifier for the node; if the matching mode is the prefix matching mode, extracting all fixed path segments in the routing rule, storing the routing rule in the node corresponding to the last fixed path segment in the prefix tree, and setting a prefix matching identifier for the node; if the matching mode is the wildcard matching mode, storing the preceding path of the routing rule in the corresponding preceding node in the prefix tree, and storing the routing rule in a first storage area, wherein the preceding node stores index information pointing to the routing rule in the first storage area; if the matching mode is the regular expression matching mode, storing the routing rule in a second storage area.
[0088] The fixed path segment is a path segment in the routing rule that consists of a defined character sequence and does not undergo semantic change during the matching process. The preceding path is the set of all fixed path segments in the routing rule, starting from the first path segment and continuing until the first path segment containing a wildcard or variable placeholder.
[0089] Optionally, if the matching mode is the exact matching mode, the routing rule storage strategy further includes storing the routing rules in a fourth storage area. The fourth storage area adopts a hash index structure. It can be understood that by establishing a direct mapping relationship from the complete request path to the routing rules, when the system detects that the request path matches the exact matching condition, it can skip the step-by-step retrieval process of the prefix tree, thereby improving the response speed in large-scale routing rule scenarios.
[0090] Optionally, the first storage area adopts a hash index structure.
[0091] Optionally, the second storage area may be a regular expression rule pool.
[0092] In this embodiment, routing rules are categorized and stored according to the type of the corresponding matching mode, which enables rapid location and pruning of routing rules.
[0093] In one embodiment, when performing routing rule matching sequentially, if a new routing rule is to be added, and the matching mode corresponding to the routing rule is an exact match mode, then the routing rule is stored in the node in the prefix tree corresponding to the complete path of the routing rule, an end marker is set for the node, the routing rule is added to the hash index structure, and the cache of the affected routing rules is cleared. If a routing rule is to be deleted, and the matching mode corresponding to the routing rule is an exact match mode, then the routing rule is removed from the node in the prefix tree, and the hash index structure and cache are cleared simultaneously.
[0094] In one embodiment, after performing routing rule matching in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode, the method further includes: if routing rule matching fails in the exact matching mode, prefix matching mode, and wildcard matching mode, then performing routing rule matching in the regular expression matching mode; if routing rule matching succeeds in the regular expression matching mode, then returning the corresponding routing rule.
[0095] Optionally, the routing matching logic of the regular expression matching pattern is to perform routing matching using regular expressions.
[0096] This embodiment achieves a complete closed loop for routing matching logic. By using a fallback matching mechanism, it ensures high-performance index retrieval for mainstream structured requests while also supporting highly complex unstructured rules.
[0097] In one embodiment, after obtaining the request path in the request information and before splitting the request path into an array of path segments, the method further includes: determining whether the request path matches a route rule already cached in the cache area; if yes, then directly stopping the matching and returning the corresponding route rule; if no, then splitting the request path into an array of path segments; after a route rule is successfully matched in the current matching mode, the method further includes: caching the successfully matched route rule in the cache area.
[0098] Optionally, a high-performance local cache can be used to cache successfully matched routing rules in the cache area.
[0099] Optionally, the request method and request path in the request information can be cached in the buffer during caching.
[0100] Optionally, a lifespan and maximum capacity can be set to prevent the buffer from overflowing.
[0101] Optionally, the successfully matched routing rules can be cached in the cache area asynchronously. It is also optional to configure whether to cache low-confidence results.
[0102] This embodiment allows skipping the prefix tree search process, reducing the processing overhead and response latency of high-frequency repetitive requests.
[0103] In one example embodiment, an API gateway route matching method based on hierarchical indexing is provided. Figure 3 This is a flowchart of the steps of the method.
[0104] It should be understood that although the steps in the flowcharts of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0105] Based on the same inventive concept, this application also provides an API gateway routing matching system based on hierarchical indexes. The solution provided by this system is similar to the solution described in the above method. Therefore, the specific limitations of one or more embodiments of the API gateway routing matching system based on hierarchical indexes provided below can be found in the limitations of the API gateway routing matching method based on hierarchical indexes described above, and will not be repeated here.
[0106] In one embodiment, such as Figure 4 As shown, an API gateway routing matching system based on hierarchical indexing is provided, including:
[0107] Request receiving module 41 is used to receive request information, the request information including a request path;
[0108] Request processing module 42 is used to obtain the request path in the request information and split the request path into a path segment array;
[0109] The routing rule matching module 43 is used to perform routing rule matching in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode and wildcard matching mode based on the path segment array; if the routing rule is successfully matched in the current matching mode, the matching is stopped and the corresponding routing rule is returned.
[0110] The aforementioned API gateway routing matching system based on hierarchical indexing utilizes a request receiving module 41 to receive request information, including a request path; a request processing module 42 to obtain the request path from the request information and split the request path into a path segment array; and a routing rule matching module 43, based on the path segment array, sequentially performs routing rule matching in a pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode. If a routing rule matches successfully in the current matching mode, matching stops and the corresponding routing rule is returned. This optimizes the routing matching complexity from linear growth with the number of rules to being related to the path length, and improves routing matching efficiency in large-scale routing rule scenarios through tree indexing and priority filtering mechanisms.
[0111] Further, successful matching of routing rules in the exact matching mode includes: performing a step-by-step search based on the path segment array starting from the root node of the prefix tree; if each path segment in the path segment array has a corresponding node in the prefix tree, and the node corresponding to the last path segment in the path segment array has an end marker, then the routing rule in the exact matching mode is determined to be successfully matched; successful matching of routing rules in the prefix matching mode includes: performing a step-by-step search based on the path segment array starting from the root node of the prefix tree; if each fixed path segment in the path segment array has a corresponding node in the prefix tree, and the node corresponding to the last fixed path segment in the fixed path segment has a prefix matching marker, then the routing rule in the prefix matching mode is determined to be successfully matched; successful matching of routing rules in the wildcard matching mode includes: performing a step-by-step search based on the path segment array starting from the root node of the prefix tree; determining the last node that matches the prefix tree as the target node; determining whether the routing rule associated with the target node matches the request path; if so, the routing rule in the wildcard matching mode is successfully matched.
[0112] Furthermore, the API gateway routing matching system based on hierarchical index also includes a hierarchical index building module, which is connected to the routing rule matching module 43. The hierarchical index building module is used to obtain multiple routing rules before sequentially performing routing rule matching, construct the prefix tree based on the multiple routing rules, determine the matching mode corresponding to the routing rule, and execute the routing rule storage strategy corresponding to the matching mode.
[0113] Furthermore, constructing the prefix tree based on multiple routing rules includes: parsing multiple routing rules and dividing the routing rules into multiple path segments according to a preset separation logic to obtain a path segment array; constructing the prefix tree based on the multiple path segment arrays, wherein each node in the prefix tree represents one of the path segments.
[0114] Further, the execution of the routing rule storage strategy corresponding to the matching mode includes: if the matching mode is the exact matching mode, then the routing rule is stored in the node corresponding to the complete path of the routing rule in the prefix tree, and an end identifier is set for the node; if the matching mode is the prefix matching mode, then all fixed path segments in the routing rule are extracted, and the routing rule is stored in the node corresponding to the last fixed path segment in the prefix tree, and a prefix matching identifier is set for the node; if the matching mode is the wildcard matching mode, then the preceding path of the routing rule is stored in the corresponding preceding node in the prefix tree, and the routing rule is stored in the first storage area, wherein the preceding node stores index information pointing to the routing rule in the first storage area; if the matching mode is the regular expression matching mode, then the routing rule is stored in the second storage area.
[0115] Furthermore, after performing routing rule matching in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode, the routing rule matching module 43 is further configured to: if routing rule matching fails in the exact matching mode, prefix matching mode, and wildcard matching mode, then perform routing rule matching in the regular expression matching mode; if routing rule matching succeeds in the regular expression matching mode, then return the corresponding routing rule.
[0116] Furthermore, after obtaining the request path from the request information and before splitting the request path into a path segment array, the routing rule matching module 43 is also used to: determine whether the request path matches a routing rule already cached in the cache area; if yes, stop matching directly and return the corresponding routing rule; if no, split the request path into a path segment array; after a successful matching of a routing rule in the current matching mode, the module further includes: caching the successfully matched routing rule in the cache area.
[0117] The modules in the aforementioned hierarchical index-based API gateway routing matching system can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device as software, so that the processor can invoke and execute the corresponding operations of each module.
[0118] In one exemplary embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 5 As shown, this computer device includes a processor, memory, input / output interfaces (I / O), and a communication interface. The processor, memory, and I / O interfaces are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interfaces. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides the environment for the operating system and computer programs stored in the non-volatile storage media. The database stores routing rules and related configuration information. The I / O interfaces are used for exchanging information between the processor and external devices. The communication interface is used for communicating with external terminals via a network connection. When executed by the processor, the computer program implements a hierarchical index-based API gateway routing matching method.
[0119] Those skilled in the art will understand that Figure 5 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0120] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:
[0121] Receive request information, the request information including a request path; obtain the request path from the request information and split the request path into a path segment array; based on the path segment array, perform routing rule matching in a pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode and wildcard matching mode; if the routing rule matches successfully in the current matching mode, stop matching and return the corresponding routing rule.
[0122] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, performs the following steps:
[0123] Receive request information, the request information including a request path; obtain the request path from the request information and split the request path into a path segment array; based on the path segment array, perform routing rule matching in a pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode and wildcard matching mode; if the routing rule matches successfully in the current matching mode, stop matching and return the corresponding routing rule.
[0124] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.
[0125] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.
[0126] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. An API gateway route matching method based on hierarchical indexing, characterized in that, include: Receive request information, the request information including a request path; Obtain the request path from the request information, and split the request path into an array of path segments; Based on the path segment array, routing rules are matched sequentially in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode; If a routing rule is successfully matched in the current matching mode, the matching stops and the corresponding routing rule is returned.
2. The method according to claim 1, characterized in that, Successful matching of routing rules in the exact matching mode includes: searching step by step from the root node of the prefix tree based on the path segment array; if each path segment in the path segment array has a corresponding node in the prefix tree, and the node corresponding to the last path segment in the path segment array has an end marker, then the routing rule is determined to be successfully matched in the exact matching mode. Successful matching of routing rules in the prefix matching mode includes: searching step by step from the root node of the prefix tree based on the path segment array; if all fixed path segments in the path segment array have corresponding nodes in the prefix tree, and the node corresponding to the last fixed path segment in the fixed path segment has a prefix matching identifier, then the routing rule matching in the prefix matching mode is determined to be successful. Successful matching of routing rules in the wildcard matching mode includes: searching level by level from the root node of the prefix tree based on the path segment array, determining the last node that matches the prefix tree as the target node, and determining whether the routing rule associated with the target node matches the request path. If so, the routing rule matching in the wildcard matching mode is successful.
3. The method according to claim 1, characterized in that, Before sequentially performing routing rule matching, the following also includes: Obtain multiple routing rules, and construct the prefix tree based on the multiple routing rules; Determine the matching pattern corresponding to the routing rule, and execute the routing rule storage policy corresponding to the matching pattern.
4. The method according to claim 3, characterized in that, The construction of the prefix tree based on multiple routing rules includes: Parse multiple routing rules and divide the routing rules into multiple path segments according to a preset separation logic to obtain a path segment array; The prefix tree is constructed based on multiple arrays of path segments, wherein each node in the prefix tree represents one of the path segments.
5. The method according to claim 4, characterized in that, The routing rule storage strategy corresponding to the matching pattern includes: If the matching mode is the exact matching mode, then the routing rule is stored in the node in the prefix tree corresponding to the complete path of the routing rule, and an end marker is set for the node; If the matching mode is prefix matching mode, then all fixed path segments in the routing rules are extracted, and the routing rules are stored in the node corresponding to the last fixed path segment in the prefix tree, and a prefix matching identifier is set for the node; If the matching mode is the wildcard matching mode, then the preceding path of the routing rule is stored in the corresponding preceding node in the prefix tree, and the routing rule is stored in the first storage area, wherein the preceding node stores index information pointing to the routing rule in the first storage area; If the matching mode is a regular expression matching mode, the routing rule is stored in the second storage area.
6. The method according to claim 1, characterized in that, After performing routing rule matching in the pre-built prefix tree according to the priority order of exact matching mode, prefix matching mode, and wildcard matching mode, the process also includes: If the routing rule matching fails in the exact matching mode, prefix matching mode, and wildcard matching mode, then the routing rule matching is performed according to the regular expression matching mode. If the routing rule matches successfully in the regular expression matching mode, the corresponding routing rule is returned.
7. The method according to claim 1, characterized in that, After obtaining the request path from the request information, and before splitting the request path into an array of path segments, the method further includes: determining whether the request path matches the routing rules already cached in the cache area; If yes, then stop matching and return the corresponding routing rule; if no, then split the request path into an array of path segments. After a routing rule is successfully matched in the current matching mode, the following also applies: The successfully matched routing rules are cached in the cache area.
8. An API gateway routing matching system based on hierarchical indexing, characterized in that, include: A request receiving module is used to receive request information, including a request path. The request processing module is used to obtain the request path from the request information and split the request path into an array of path segments; The routing rule matching module is used to perform routing rule matching in the pre-constructed prefix tree according to the priority order of exact matching mode, prefix matching mode and wildcard matching mode based on the path segment array; if the routing rule is successfully matched in the current matching mode, the matching is stopped and the corresponding routing rule is returned.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method described in any one of claims 1 to 7.