Dynamic routing configuration method and system based on Spring Cloud Gateway
By storing and dynamically loading routing information in the database and Redis, the problem of API gateway routing configuration needs to be shut down and restarted is solved, and a seamless update and fast response routing configuration is achieved, improving the stability and response speed of the system.
Patent Information
- Application Number
- CN202510731241.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-03
- Publication Date
- 2025-09-02
AI Technical Summary
In the prior art, the routing configuration of API gateways requires downtime and restart, resulting in service interruption and increased operation and maintenance complexity, and it is impossible to quickly respond to changes in business needs.
The routing information is stored in the database and mapped into a routing data entity object, serialized and cached in Redis, and the routing information is loaded dynamically through the Spring Cloud Gateway gateway, and the routing configuration is updated using distributed locking and real-time listening mechanisms.
It realizes updating routing rules without shutting down, improving system continuity and stability, reducing request delays, supporting rapid iterative development, and reducing operation and maintenance costs and complexity.
Smart Images

Figure CN120583025A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technologies, and in particular to a dynamic routing configuration method and system based on Spring Cloud Gateway. Background Art
[0002] In the existing technology, the implementation of most API gateway functions relies on the routing configuration in memory. When the routing rules need to be modified, it is usually necessary to redeploy or restart the application to load the latest routing configuration. However, this solution has the following defects: each update requires shutting down and restarting the service, which may cause service interruption. At the same time, frequent restart operations increase the complexity of operation and maintenance work, resulting in increased workload. In addition, due to the delay in processing each request, it is impossible to quickly respond to changes in business needs. Summary of the Invention
[0003] In response to the shortcomings of the existing technology, the present invention provides a dynamic routing configuration method and system based on Spring Cloud Gateway, which can update routing rules without shutting down the system, greatly improving the continuity and stability of the system, and improving the response speed and service quality of the entire system.
[0004] To achieve the above object, the present invention provides the following technical solutions:
[0005] On the one hand, a dynamic routing configuration method based on Spring Cloud Gateway is provided, which includes the following steps:
[0006] The routing information is stored in a database, and based on the interaction logic, the routing information obtained from the database is mapped into a routing data entity object, and after the routing data entity object is converted into a gateway routing format, all routing information is serialized and cached in Redis;
[0007] The Spring Cloud Gateway receives external requests and determines whether there is corresponding routing information in Redis based on the external request. If so, the corresponding routing information is loaded directly from Redis to respond to the external request.
[0008] Preferably, if the corresponding routing information does not exist in Redis, fall back to the database query to determine whether the corresponding routing information exists in the database;
[0009] If the corresponding routing information exists in the database, the corresponding routing information is refreshed to Redis and the external request is responded to;
[0010] If the corresponding routing information does not exist in the database, it is prompted that there is no routing information matching the external request.
[0011] Preferably, if the corresponding routing information is not successfully refreshed to Redis, a routing information change prompt is generated to remind the user to change the routing information in the database.
[0012] Preferably, changing the routing information in the database includes adding one or more of routing information, modifying routing information, and deleting routing information.
[0013] Preferably, after completing the modification of the routing information in the database, the routing information in Redis is updated synchronously.
[0014] Preferably, synchronously updating the routing information in Redis includes the following steps:
[0015] Determine whether there is the same original routing information as the changed routing information in Redis. If so, merge the changed routing information in the database with the original routing information in Redis, and cache the merged routing information in Redis to update the routing information in Redis.
[0016] If it does not exist, dynamic routing configuration is performed based on the Spring Cloud Gateway gateway, a distributed lock is used to create the latest routing information, and the routing information is cached in Redis to update the routing information in Redis.
[0017] Preferably, after the routing information in Redis is updated, the updated routing information in Redis is parsed, and the parsed routing information is serialized, and the routing information after the data serialization is cached in Redis and published.
[0018] Preferably, the routing information published by Redis is monitored in real time; if the routing information in Redis is monitored to have been updated, the Spring Cloud Gateway is notified to adopt the new routing configuration.
[0019] Preferably, the database comprises a MySQL database.
[0020] On the other hand, a dynamic routing configuration system based on Spring Cloud Gateway is also provided, which includes:
[0021] A database for storing routing information;
[0022] A logic encapsulation unit, which is used to encapsulate the interaction logic with the database through the constructed data access layer;
[0023] a format conversion unit, which maps the routing information obtained from the database into a routing data entity object based on the interaction logic, converts the routing data entity object into a gateway routing format, and serializes all routing information that has completed format conversion;
[0024] A storage unit, which is used to store serialized routing information and publish routing information;
[0025] The gateway is used to determine whether the corresponding routing information exists in Redis based on the external request. If so, the gateway directly loads the corresponding routing information from Redis to respond to the external request; if not, the gateway falls back to the database query to determine whether the corresponding routing information exists in the database;
[0026] a routing information updating unit, configured to generate a routing information change prompt to remind the user to change the routing information in the database;
[0027] And, after completing the modification of the routing information in the database, the routing information in Redis is updated synchronously.
[0028] Compared with the prior art, the present invention has the following beneficial effects:
[0029] The present invention can update routing rules without shutting down the system, greatly improving the continuity and stability of the system. At the same time, it uses Redis as a cache layer to accelerate the query process of routing information and reduce the delay in each request processing. Therefore, through the dynamic refresh mechanism, changes in business needs can be quickly reflected in the actual running services, supporting a rapid iterative development model, improving the response speed and service quality of the entire system. Furthermore, since there is no need to frequently restart the application to make the new routing configuration effective, operation and maintenance personnel can manage the routing configuration more flexibly and efficiently, reducing maintenance costs and complexity. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Figure 1 This is a schematic diagram of the steps of the dynamic routing configuration method based on Spring Cloud Gateway in the present invention;
[0031] Figure 2 This is a structural diagram of the dynamic routing configuration method system based on Spring Cloud Gateway in the present invention. DETAILED DESCRIPTION
[0032] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.
[0033] Example 1:
[0034] like Figure 1 As shown, this embodiment provides a dynamic routing configuration method based on Spring Cloud Gateway, which includes the following steps:
[0035] S1. Store routing information in a database, encapsulate the interaction logic with the database through a constructed data access layer, map routing information obtained from the database into routing data entity objects based on the interaction logic, convert the routing data entity objects into gateway routing format, serialize all routing information, cache it in Redis, and publish the routing information;
[0036] In this embodiment, the routing information can be predefined and includes a routing data entity and a routing data entity structure, wherein the routing data entity includes one or more attributes such as ID, Predicate, Filter, configuration, message type, and message data. Thus, a single route, i.e., a routing data entity, can be described based on a custom data structure to support the usage requirements of various scenarios. The routing data entity structure includes one or more of the following: business system, intra-system group, routing name, routing version, routing status, assertion configuration, data processing configuration, and custom configuration. At the same time, the database includes a MySQL database. Thus, the routing data entity and the routing data entity structure can be stored in the MySQL database for a long time and can be externally managed and updated.
[0037] S2. Receive external requests from Nginx, business-side servers, etc. through the Spring Cloud Gateway, and determine whether there is corresponding routing information in Redis based on the external request. If so, load the corresponding routing information directly from Redis to respond to the external request;
[0038] If not, fall back to the database query to determine whether the corresponding routing information exists in the database;
[0039] Furthermore, if the corresponding routing information exists in the database, the corresponding routing information is refreshed to Redis and responds to the external request. If the refresh is unsuccessful, step S3 is executed;
[0040] If the corresponding routing information does not exist in the database, it is prompted that there is no routing information matching the external request;
[0041] S3. Generate a routing information change prompt to remind the user to change the routing information in the database, wherein changing the routing information in the database includes adding routing information, modifying routing information, and deleting one or more of the routing information;
[0042] At the same time, in this embodiment, the user can modify the routing information in the database through the UI interface of the smart terminal, and the smart terminal includes one or more of a smart phone, a tablet computer, a PC, and a smart wearable device;
[0043] S4. After completing the change of the routing information in the database, the routing information in Redis is updated synchronously;
[0044] Specifically, synchronously updating the routing information in Redis includes the following steps:
[0045] S41. Determine whether there is original routing information in Redis that is the same as the changed routing information. If so, merge the changed routing information in the database with the original routing information in Redis, and cache the merged routing information in Redis to update the routing information in Redis. If not, perform dynamic routing configuration based on the Spring Cloud Gateway gateway, use distributed locks to create the latest routing information, and cache the routing information in Redis to update the routing information in Redis. In this way, the distributed lock can prevent other threads from modifying the routing information at the same time, avoid the generation of dirty data, and ensure data security.
[0046] S42. Parse the updated routing information in Redis, perform data serialization on the parsed routing information (e.g., ID, routing name, assertion information, filter, request URI, etc.), cache the serialized routing information in Redis, and publish it. In this embodiment, the serialized routing information can be stored in the form of a JSON format file.
[0047] S5. Use the API or custom event listener provided by Spring Cloud Gateway to monitor the routing information published by Redis in real time.
[0048] If the routing information in Redis is updated, the Spring Cloud Gateway is notified to adopt the new routing configuration, thereby achieving the effect of updating the routing configuration without restarting.
[0049] Therefore, this embodiment can update routing rules without shutting down the system, greatly improving the continuity and stability of the system, avoiding the need to restart the gateway service and causing service interruption. At the same time, Redis is used as a cache layer to accelerate the query process of routing information and reduce the delay in each request processing. Therefore, through the dynamic refresh mechanism, changes in business needs can be quickly reflected in the actual running services, supporting a rapid iterative development model, and improving the response speed and service quality of the entire system. Furthermore, since there is no need to frequently restart the application to make the new routing configuration take effect, operation and maintenance personnel can manage the routing configuration more flexibly and efficiently, reducing maintenance costs and complexity.
[0050] Example 2:
[0051] This embodiment provides a dynamic routing configuration system based on Spring Cloud Gateway, which can implement the dynamic routing configuration method in Example 1, such as Figure 2 As shown, the dynamic routing configuration system includes:
[0052] Database 1, which is used to store routing information;
[0053] A logic encapsulation unit 2, which is used to encapsulate the interaction logic with the database through the constructed data access layer;
[0054] A format conversion unit 3 maps the routing information obtained from the database into a routing data entity object based on the interaction logic, converts the routing data entity object into a gateway routing format, and serializes all the routing information that has completed the format conversion;
[0055] A storage unit 4, which is used to store serialized routing information and publish routing information, and the storage unit 4 includes Redis;
[0056] Gateway 5, which is used to determine whether corresponding routing information exists in Redis based on the external request. If so, the corresponding routing information is directly loaded from Redis to respond to the external request; if not, it falls back to the database query to determine whether the corresponding routing information exists in the database. The gateway 5 includes a Spring Cloud Gateway gateway;
[0057] Routing information updating unit 6, which is used to generate a routing information change prompt to remind the user to change the routing information in the database, and the process is the same as step S3;
[0058] And, after completing the modification of the routing information in the database, the routing information in Redis is updated synchronously, and the process is the same as step S4.
[0059] In summary, the present invention can update routing rules without shutting down the system, which greatly improves the continuity and stability of the system. At the same time, Redis is used as a cache layer to accelerate the query process of routing information and reduce the delay in each request processing. Therefore, through the dynamic refresh mechanism, changes in business needs can be quickly reflected in the actual running services, supporting a rapid iterative development model, and improving the response speed and service quality of the entire system. Furthermore, since there is no need to frequently restart the application to make the new routing configuration take effect, operation and maintenance personnel can manage the routing configuration more flexibly and efficiently, reducing maintenance costs and complexity.
[0060] It should be noted that, in the above embodiments, terms such as "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the process, method, article, or device comprising the element.
[0061] While embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions, and variations may be made to these embodiments without departing from the principles and spirit of the invention, and that the scope of the invention is defined by the appended claims and their equivalents.
Claims
1. A dynamic routing configuration method based on Spring Cloud Gateway, characterized in that: The steps include: The routing information is stored in a database, and based on the interaction logic, the routing information obtained from the database is mapped into a routing data entity object, and after the routing data entity object is converted into a gateway routing format, all routing information is serialized and cached in Redis; The Spring Cloud Gateway receives external requests and determines whether there is corresponding routing information in Redis based on the external request. If so, the corresponding routing information is loaded directly from Redis to respond to the external request.
2. The dynamic routing configuration method according to claim 1, wherein: If the corresponding routing information does not exist in Redis, fall back to the database query to determine whether the corresponding routing information exists in the database; If the corresponding routing information exists in the database, the corresponding routing information is refreshed to Redis and the external request is responded to; If the corresponding routing information does not exist in the database, it is prompted that there is no routing information matching the external request.
3. The dynamic routing configuration method according to claim 2, wherein: If the corresponding routing information is not successfully refreshed to Redis, a routing information change prompt is generated to remind the user to change the routing information in the database.
4. The dynamic routing configuration method according to claim 3, wherein: Modifying the routing information in the database includes adding routing information, modifying routing information, and deleting one or more items of routing information.
5. The dynamic routing configuration method according to claim 3, wherein: After completing the change to the routing information in the database, the routing information in Redis is updated synchronously.
6. The dynamic routing configuration method according to claim 5, wherein: Synchronously update the routing information in Redis, including the following steps: Determine whether there is the same original routing information as the changed routing information in Redis. If so, merge the changed routing information in the database with the original routing information in Redis, and cache the merged routing information in Redis to update the routing information in Redis. If it does not exist, dynamic routing configuration is performed based on the Spring Cloud Gateway gateway, a distributed lock is used to create the latest routing information, and the routing information is cached in Redis to update the routing information in Redis.
7. The dynamic routing configuration method according to claim 6, wherein: After updating the routing information in Redis, the updated routing information in Redis is parsed, and the parsed routing information is serialized, and the serialized routing information is cached in Redis and published.
8. The dynamic routing configuration method according to claim 7, wherein: Monitor the routing information published by Redis in real time; if the routing information in Redis is updated, notify Spring Cloud Gateway to adopt the new routing configuration.
9. The dynamic routing configuration method according to claim 1, wherein: The database includes a MySQL database.
10. A dynamic routing configuration system based on Spring Cloud Gateway, characterized in that: include: A database for storing routing information; A logic encapsulation unit, which is used to encapsulate the interaction logic with the database through the constructed data access layer; a format conversion unit, which maps the routing information obtained from the database into a routing data entity object based on the interaction logic, converts the routing data entity object into a gateway routing format, and serializes all routing information that has completed format conversion; A storage unit, which is used to store serialized routing information and publish routing information; The gateway is used to determine whether the corresponding routing information exists in Redis based on the external request. If so, the gateway directly loads the corresponding routing information from Redis to respond to the external request; if not, the gateway falls back to the database query to determine whether the corresponding routing information exists in the database; a routing information updating unit, configured to generate a routing information change prompt to remind the user to change the routing information in the database; And, after completing the modification of the routing information in the database, the routing information in Redis is updated synchronously.
Citation Information
Patent Citations
Method and system for dynamically routing back-end address by API (Application Program Interface) gateway
CN114143261A
Gateway interaction method, system and server
CN117240659A
API gateway forwarding method and system based on Spring Cloud Gateway
CN117459445A
API gateway and operation method thereof
WO2024232462A1