Nacos service-based gateway dynamic routing method, device, computer equipment, and computer-readable storage medium

By using the dynamic routing method of the Nacos service in the Spring Cloud Gateway, monitoring changes in the routing configuration file and automatically updating the routing, the cumbersome operation problem in the existing technology is solved and a simplified dynamic routing solution is implemented.

CN116633727BActive Publication Date: 2025-09-23BEIJING INSIGHT NETWORK CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202310625897.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-05-30
Publication Date
2025-09-23
Estimated Expiration
2043-05-30

AI Technical Summary

Technical Problem

The existing dynamic routing solution based on Spring Cloud Gateway requires adding configuration information of MySQL database or Redis non-relational database in the gateway service, which makes the operation cumbersome.

Method used

The dynamic routing method of the gateway using the Nacos service is achieved by creating a routing configuration file in the NacosConfig configuration center, using a listener to monitor configuration file changes, and automatically updating the routing configuration without restarting the gateway service to achieve dynamic routing.

Benefits of technology

It simplifies the operation process, avoids dependence on MySQL or Redis database, and realizes automatic update and monitoring of routing information without restarting the gateway service.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116633727B_ABST
    Figure CN116633727B_ABST
Patent Text Reader

Abstract

The present invention discloses a gateway dynamic routing method, device, computer equipment and computer-readable storage medium based on Nacos service, and relates to the field of gateway routing technology. The method first responds to human-computer interaction operations and sequentially completes the creation of a routing configuration file, a Gateway microservice, a GatewayConfig.java file, an interface inheritance class and a change monitoring class. Then, based on an added listener, changes in the routing configuration file are monitored. Finally, when the listener receives configuration update information, a dynamic routing definition method is triggered to update the changed routing configuration file to the application process of the gateway. In this way, not only can routing monitoring be achieved based on the configuration of the Nacos service and the purpose of automatically taking effect without restarting the gateway service is achieved, but also there is no need to add database configuration information in the gateway service, which can greatly simplify the operation and facilitate practical application and promotion.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of gateway routing technology, and in particular relates to a gateway dynamic routing method, device, computer equipment and computer-readable storage medium based on Nacos service. Background Art

[0002] A business system refers to the business processes necessary to achieve a company's positioning, the roles played by various partners, and the methods and content of stakeholder collaboration and transactions. As the sole traffic entry point for a business system, the gateway encapsulates the system's internal architecture, ensuring that all requests first pass through the gateway, which then routes the request to the appropriate microservice. (Microservices is a software architectural style based on small functional blocks with a single responsibility and function. These blocks are modularized to form complex, large-scale applications. These blocks communicate with each other using a language-independent set of APIs.)

[0003] Static routing is a traditional routing method in which routing entries are manually configured by the user (generally speaking, static routes are added to the routing table one by one by the network administrator). Therefore, the routing information is fixed and will not change even if the network conditions have changed or it has been reconfigured. Therefore, if a new microservice is added, the gateway service needs to be reconfigured and then restarted. Compared to static routing, dynamic routing (which means that the router can automatically build its own routing table and can adjust it in time according to changes in actual conditions) has the advantage that modifications, additions, or deletions to the routing configuration can take effect dynamically without restarting the gateway service.

[0004] Currently, there are two main dynamic routing approaches for Spring Cloud Gateway (a new Spring Cloud project developed based on technologies such as Spring 5.0, Spring Boot 2.0, and Project Reactor, aiming to provide a simple, effective, and unified API routing management approach for microservice architectures): a MySQL + API-based dynamic routing approach and a Redis + API-based dynamic routing approach. These two approaches are essentially the same, differing only in their data storage methods. The general implementation strategy is as follows: defining route creation, deletion, modification, and query interfaces through an Application Programming Interface (API), modifying route information through the API, then storing the modified data in a MySQL database or Redis non-relational database, and refreshing routes to achieve dynamic updates. However, this approach requires adding MySQL or Redis non-relational database configuration information to the Gateway service, making it relatively cumbersome. Therefore, finding a simpler, new dynamic routing solution suitable for Spring Cloud Gateway is a topic that those skilled in the art urgently need to research. Summary of the Invention

[0005] The purpose of the present invention is to provide a gateway dynamic routing method, device, computer equipment and computer-readable storage medium based on Nacos service, so as to solve the problem that the existing dynamic routing solution based on Spring Cloud Gateway needs to add the configuration information of MySQL database or Redis non-relational database in the Gateway service, which makes the operation more complicated.

[0006] In order to achieve the above object, the present invention adopts the following technical solutions:

[0007] In the first aspect, a gateway dynamic routing method based on Nacos service is provided, which is executed by a server communicating with a router based on Spring Cloud Gateway, including:

[0008] In response to the first human-computer interaction operation, starting the Nacos service and creating a routing configuration file in the NacosConfig configuration center of the Nacos service, wherein the routing configuration file includes at least one piece of routing information of the initial configuration;

[0009] In response to the second human-computer interaction operation, a Gateway microservice is created and a service configuration file is added, wherein the service configuration file includes the storage address of the specified routing configuration file and the relevant data configured and used for gateway dynamic routing;

[0010] In response to a third human-computer interaction operation, creating a GatewayConfig.java file, wherein the GatewayConfig.java file includes the storage address and the related data read from the service configuration file;

[0011] In response to the fourth human-computer interaction operation, a class is created to inherit the ApplicationEventPublisherAware interface to inject the event publisher ApplicationEventPublisher into the Gateway microservice, so that the Gateway microservice has the ability to publish events, specifically including: completing the initialization of the event push handle and adding a dynamic routing definition method;

[0012] In response to the fifth human-computer interaction operation, another class is created to monitor changes in the routing configuration file, specifically including: specifying that the current object depends on the GatewayConfig.java file, initializing the configuration content for the Nacos service, specifying the routing configuration path through the NacosConfig configuration center to obtain the routing configuration file, and adding a listener to the NacosConfig configuration center to monitor changes in the routing configuration file;

[0013] When the listener receives configuration update information indicating that the routing configuration file has changed, it triggers the call of the dynamic routing definition method and updates the changed routing configuration file to the application process of the Spring CloudGateway gateway.

[0014] Based on the above invention content, a new dynamic routing solution suitable for Spring Cloud Gateway is provided, that is, first responding to human-computer interaction operations to complete the creation of routing configuration files, Gateway microservice creation, GatewayConfig.java file creation, interface inheritance class creation and change monitoring class creation in sequence, and then based on the added listener to monitor changes in the routing configuration file, finally, when the listener receives configuration update information indicating that the routing configuration file has changed, it triggers the call of the dynamic routing definition method, and updates the changed routing configuration file to the application process of the Spring Cloud Gateway. In this way, not only can the routing be monitored based on the configuration of the Nacos service and the purpose of automatically taking effect without restarting the gateway service can be achieved, but there is also no need to add configuration information of the Mysql database or Redis non-relational database in the Gateway service, which can greatly simplify the operation and facilitate practical application and promotion.

[0015] In a possible design, the relevant data packet contains the namespace namespace of the NacosConfig configuration center, the configuration set unique identifier data-id and / or the configuration group unique identifier group-id.

[0016] In a possible design, completing the initialization of the event push handle includes: completing the initialization of the event push handle by specifying the event publisher ApplicationEventPublisher.

[0017] In one possible design, the dynamic routing definition method includes a user definition method for adding routing information, updating routing information and / or deleting routing information.

[0018] In a possible design, in the dynamic route definition mode, the operation of reading the route information is implemented through the route definition locator routeDefinitionLocator, and the operation of adding, updating and / or deleting the route information is implemented through the route definition editor routeDefinitionWriter.

[0019] In a possible design, specifying that the current object depends on the GatewayConfig.java file includes: specifying that the current object depends on the GatewayConfig.java file through the @DependsOn annotation.

[0020] In one possible design, initializing the configuration content for the Nacos service includes: obtaining the storage address and a namespace namespace of the NacosConfig configuration center in the relevant data, so as to serve as the configuration content for the Nacos service.

[0021] In the second aspect, a gateway dynamic routing device based on Nacos service is provided, which is suitable for being arranged in a server connected to a router based on Spring Cloud Gateway gateway, and includes an operation response module and a trigger call module connected to the communication;

[0022] The operation response module is used to respond to the first human-computer interaction operation, start the Nacos service, and create a routing configuration file in the NacosConfig configuration center of the Nacos service, wherein the routing configuration file includes at least one routing information of the initial configuration;

[0023] The operation response module is further configured to respond to the second human-computer interaction operation, create a Gateway microservice, and add a service configuration file, wherein the service configuration file includes a designated storage address of the routing configuration file and configured related data for gateway dynamic routing;

[0024] The operation response module is further configured to respond to a third human-computer interaction operation and create a GatewayConfig.java file, wherein the GatewayConfig.java file includes the storage address and the related data read from the service configuration file;

[0025] The operation response module is further used to respond to the fourth human-computer interaction operation, create a class to inherit the ApplicationEventPublisherAware interface, so as to inject the event publisher ApplicationEventPublisher into the Gateway microservice, so that the Gateway microservice has the ability to publish events, specifically including: completing the initialization of the event push handle and adding a dynamic routing definition method;

[0026] The operation response module is further configured to respond to a fifth human-computer interaction operation and create another class to monitor changes in the routing configuration file, specifically including: specifying that the current object depends on the GatewayConfig.java file, initializing the configuration content for the Nacos service, obtaining the routing configuration file by specifying a routing configuration path through the NacosConfig configuration center, and adding a listener to the NacosConfig configuration center to monitor changes in the routing configuration file;

[0027] The trigger call module is used to trigger the call of the dynamic routing definition method when the listener receives configuration update information indicating that the routing configuration file has changed, and update the changed routing configuration file to the application process of the Spring Cloud Gateway gateway.

[0028] In a third aspect, the present invention provides a computer device comprising a memory, a processor and a transceiver which are communicatively connected in sequence, wherein the memory is used to store a computer program, the transceiver is used to send and receive messages, and the processor is used to read the computer program and execute the gateway dynamic routing method as described in the first aspect or any possible design of the first aspect.

[0029] In a fourth aspect, the present invention provides a computer-readable storage medium having instructions stored thereon. When the instructions are run on a computer, the gateway dynamic routing method as described in the first aspect or any possible design of the first aspect is executed.

[0030] In a fifth aspect, the present invention provides a computer program product comprising instructions, which, when executed on a computer, causes the computer to execute the gateway dynamic routing method as described in the first aspect or any possible design of the first aspect.

[0031] Beneficial effects of the above scheme:

[0032] (1) The present invention creatively provides a new dynamic routing solution suitable for Spring Cloud Gateway, that is, first responding to human-computer interaction operations to complete the creation of routing configuration files, Gateway microservices, GatewayConfig.java files, interface inheritance classes and change monitoring classes in sequence, and then based on the added listener to monitor the changes in the routing configuration files, and finally when the listener receives configuration update information indicating that the routing configuration files have changed, it triggers the call of the dynamic routing definition method, and updates the changed routing configuration files to the application process of the Spring Cloud Gateway. In this way, not only can the routing be monitored based on the configuration of the Nacos service and the purpose of automatically taking effect without restarting the gateway service can be achieved, but there is also no need to add the configuration information of the Mysql database or the Redis non-relational database in the Gateway service, which can greatly simplify the operation and facilitate practical application and promotion. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0034] Figure 1 A flowchart of a Nacos-based gateway dynamic routing method provided in an embodiment of the present application.

[0035] Figure 2 This is a schematic diagram of the structure of a gateway dynamic routing device based on Nacos service provided in an embodiment of the present application.

[0036] Figure 3 A schematic diagram of the structure of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0037] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the present invention will be briefly introduced below in conjunction with the drawings and the description of the embodiments or the prior art. Obviously, the following description of the structure of the drawings is only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative work. It should be noted that the description of these embodiments is used to help understand the present invention, but does not constitute a limitation of the present invention.

[0038] It should be understood that although the terms first, second, etc. may be used herein to describe various objects, these objects should not be limited by these terms. These terms are merely used to distinguish one object from another. For example, a first object can be referred to as a second object, and similarly, a second object can be referred to as a first object without departing from the scope of the exemplary embodiments of the present invention.

[0039] It should be understood that the term "and / or" that may appear in this document is merely a description of the association relationship between associated objects, indicating that there may be three relationships. For example, A and / or B can indicate three situations: A exists alone, B exists alone, or A and B exist at the same time. For another example, A, B and / or C can indicate the existence of any one of A, B and C or any combination of them. The term " / and" that may appear in this document describes another type of association object relationship, indicating that there may be two relationships. For example, A / and B can indicate two situations: A exists alone or A and B exist at the same time. In addition, the character " / " that may appear in this document generally indicates that the previous and next associated objects are in an "or" relationship.

[0040] Example:

[0041] like Figure 1 As shown, the gateway dynamic routing method based on Nacos service provided in the first aspect of this embodiment can be executed by, but is not limited to, a server with certain computing resources and a communication connection to a router based on Spring Cloud Gateway. Figure 1 As shown, the gateway dynamic routing method may include, but is not limited to, the following steps S1 to S6.

[0042] S1. In response to a first human-computer interaction operation, start the Nacos service and create a routing configuration file in the NacosConfig configuration center of the Nacos service, wherein the routing configuration file includes at least one routing information of the initial configuration.

[0043] In step S1, the first human-computer interaction operation and the multiple human-computer interaction operations in the subsequent steps are all manual operations performed by the user on the human-computer interaction interface in order to achieve the corresponding operation purpose, and can be detected by conventional methods. For example, the view layer in the Model View Controller (MVC) architecture (which is an existing software architecture that realizes the separation of business logic, data and interface display, and includes a view layer, a control layer and a model layer from top to bottom) can be preferably used to realize the purpose of human-computer interaction operation detection (that is, the Model View Controller MVC architecture is adopted in the custom page generation tool), so that after the human-computer interaction operation is detected, the human-computer interaction operation can be transmitted to the model layer via the control layer, and finally the operation is responded to in the model layer. In this way, based on the characteristic of the MVC architecture of separating business logic, data and interface display, when the user initiates a human-computer interaction operation through human-computer interaction, he only needs to perform simple operations on the control interface provided by the view layer, thereby greatly facilitating the user's human-computer interaction and improving the user experience.

[0044] In step S1, the Nacos (Dynamic Naming and Configuration Service, where Na refers to the naming / nameServer registry, co refers to the configuration center, and service refers to the service-centric nature of the registry / configuration center) service is a dynamic service discovery, configuration management, and service management platform that makes it easier to build cloud-native applications. It is dedicated to helping users discover, configure, and manage microservices, and provides a set of easy-to-use feature sets that can help users quickly implement dynamic service discovery, service configuration, service metadata, and traffic management. Therefore, the routing configuration file can be routinely created in the NacosConfig configuration center, for example, to initially configure routing information for routing to the reporting microservice. Specific operation codes are as follows:

[0045]

[0046] S2. In response to the second human-computer interaction operation, create a Gateway microservice and add a service configuration file, wherein the service configuration file includes the specified storage address of the routing configuration file and the configured relevant data for gateway dynamic routing.

[0047] In step S2, the storage address can be specifically represented as springcloud.nacos.config.server-addr. The relevant data will be read through the subsequently created GatewayConfig.java file to achieve the monitoring purpose, specifically including but not limited to the namespace namespace of the NacosConfig configuration center, the configuration set unique identifier data-id and / or the configuration group unique identifier group-id, etc., wherein the namespace namespace is used to perform tenant-granular configuration isolation, so that the same configuration set unique identifier data-id and / or configuration group unique identifier group-id can exist in different namespaces; the configuration group unique identifier group-id is a group of configuration sets in the Nacos service, which is one of the dimensions of organizational configuration; the configuration set unique identifier data-id is the unique identifier of a configuration set, which is also one of the dimensions of organizational configuration.

[0048] S3. In response to the third human-computer interaction operation, create a GatewayConfig.java file, wherein the GatewayConfig.java file includes but is not limited to the storage address and the related data read from the service configuration file.

[0049] In step S3, the GatewayConfig.java file may also include a configuration reading timeout set by the user, so that in the subsequent monitoring process, if the routing configuration file is not read after the configuration reading timeout, a reminder message indicating an abnormality in the routing configuration file reading is triggered.

[0050] S4. In response to the fourth human-computer interaction operation, create a class to inherit the ApplicationEventPublisherAware interface to inject the event publisher ApplicationEventPublisher into the Gateway microservice, so that the Gateway microservice has the ability to publish events, specifically including: completing the initialization of the event push handle and adding a dynamic routing definition method.

[0051] In step S4, the class is an existing programming term, and its English name is Class. It is the basis for object-oriented programming (OOP) to implement information encapsulation, that is, a class is a user-defined reference data type, also known as a class type. Each class contains a data description and a set of functions for operating data or transmitting messages, and an instance of a class is called an object. The ApplicationEventPublisherAware interface is Spring's event mechanism (i.e., it can push messages, and there are listeners to listen. Once the listener listens to the message, it will process the corresponding logic). To implement this class, it is necessary to rely on an event push handle (which is used to push messages); and in order to ensure the correctness and reliability of event push (i.e., to ensure that when pushing an event, the event can be correctly sent to the specified listener), it is necessary to perform some preparatory work when initializing the event push handle, such as obtaining necessary configuration information, setting an event listener, and registering an event listener (the completion of these preparatory work can ensure the smooth progress of event push). Specifically, the initialization of the event push handle is completed by specifying the event publisher ApplicationEventPublisher. The dynamic routing definition method is used to describe a user-defined dynamic routing method, specifically including but not limited to a user-defined method for adding, updating, and / or deleting routing information. More specifically, in the dynamic routing definition method, operations for reading routing information can be implemented using a routing definition locator (routeDefinitionLocator), and operations for adding, updating, and / or deleting routing information can be implemented using a routing definition editor (routeDefinitionWriter). Both the routing definition locator (routeDefinitionLocator) and the routing definition editor (routeDefinitionWriter) are existing tools.

[0052] S5. In response to the fifth human-computer interaction operation, create another class to monitor changes in the routing configuration file, specifically including: specifying that the current object depends on the GatewayConfig.java file, initializing the configuration content for the Nacos service, specifying the routing configuration path through the NacosConfig configuration center to obtain the routing configuration file, and adding a listener to the NacosConfig configuration center to monitor changes in the routing configuration file.

[0053] In step S5, the current object refers to an instance of the other class. Changes to the routing configuration file are generally caused by manual updates. For example, first log in to the established Nacos address, then create a new configuration file, then define routing-related information in the configuration file, and finally save it. The program will automatically monitor changes to the routing configuration file. The purpose of the aforementioned dependency is to obtain the IP address, port number, namespace namespace, and configuration set unique identifier data-id of the Nacos service, so as to correctly obtain the specific address of the workspace to which the above configuration file belongs. Specifically, specifying that the current object depends on the GatewayConfig.java file includes: specifying that the current object depends on the GatewayConfig.java file through the @DependsOn annotation. The purpose of the aforementioned configuration content initialization is to read the attribute information configured in the GatewayConfig.java file, such as the namespace namespace and configuration set unique identifier data-id, to ensure correct connection to the Nacos service. Specifically, initializing the configuration content for the Nacos service includes: obtaining the storage address and the namespace namespace in the relevant data and the NacosConfig configuration center, so as to serve as the configuration content for the Nacos service.

[0054] S6. When the listener receives configuration update information indicating that the routing configuration file has changed, it triggers the call of the dynamic routing definition method and updates the changed routing configuration file to the application process of the Spring CloudGateway gateway.

[0055] In step S6, for example, if the routing configuration file is updated, the listener is automatically triggered to obtain the routing information deletion result in the routing definition locator RouteDefinitionLocator, and then all routing information is added again without restarting any service, and it is automatically effective.

[0056] Therefore, based on the gateway dynamic routing method described in the aforementioned steps S1 to S6 and based on the Nacos service, a new dynamic routing solution suitable for the Spring Cloud Gateway gateway is provided, that is, first responding to human-computer interaction operations, the routing configuration file creation, the Gateway gateway microservice creation, the GatewayConfig.java file creation, the interface inheritance class creation and the change monitoring class creation are completed in sequence, and then based on the added listener to monitor the changes in the routing configuration file, and finally when the listener receives the configuration update information indicating that the routing configuration file has changed, it triggers the call of the dynamic routing definition method and updates the changed routing configuration file to the application process of the Spring Cloud Gateway gateway. In this way, not only can the routing monitoring be realized based on the configuration of the Nacos service and the purpose of automatically taking effect without restarting the gateway service, but there is also no need to add the configuration information of the Mysql database or the Redis non-relational database in the Gateway gateway service, which can greatly simplify the operation and facilitate practical application and promotion.

[0057] like Figure 2 As shown, the second aspect of this embodiment provides a virtual device for implementing the gateway dynamic routing method described in the first aspect, which is suitable for being arranged in a server that is communicatively connected to a router based on a Spring Cloud Gateway gateway, and includes an operation response module and a trigger call module that are communicatively connected;

[0058] The operation response module is used to respond to the first human-computer interaction operation, start the Nacos service, and create a routing configuration file in the NacosConfig configuration center of the Nacos service, wherein the routing configuration file includes at least one routing information of the initial configuration;

[0059] The operation response module is further configured to respond to the second human-computer interaction operation, create a Gateway microservice, and add a service configuration file, wherein the service configuration file includes a designated storage address of the routing configuration file and configured related data for gateway dynamic routing;

[0060] The operation response module is further configured to respond to a third human-computer interaction operation and create a GatewayConfig.java file, wherein the GatewayConfig.java file includes the storage address and the related data read from the service configuration file;

[0061] The operation response module is further used to respond to the fourth human-computer interaction operation, create a class to inherit the ApplicationEventPublisherAware interface, so as to inject the event publisher ApplicationEventPublisher into the Gateway microservice, so that the Gateway microservice has the ability to publish events, specifically including: completing the initialization of the event push handle and adding a dynamic routing definition method;

[0062] The operation response module is further configured to respond to a fifth human-computer interaction operation and create another class to monitor changes in the routing configuration file, specifically including: specifying that the current object depends on the GatewayConfig.java file, initializing the configuration content for the Nacos service, obtaining the routing configuration file by specifying a routing configuration path through the NacosConfig configuration center, and adding a listener to the NacosConfig configuration center to monitor changes in the routing configuration file;

[0063] The trigger call module is used to trigger the call of the dynamic routing definition method when the listener receives configuration update information indicating that the routing configuration file has changed, and update the changed routing configuration file to the application process of the Spring Cloud Gateway gateway.

[0064] The working process, working details and technical effects of the aforementioned device provided in the second aspect of this embodiment can be found in the gateway dynamic routing method described in the first aspect, and will not be repeated here.

[0065] like Figure 3As shown, the third aspect of this embodiment provides a computer device for executing the gateway dynamic routing method as described in the first aspect, including a memory, a processor and a transceiver that are sequentially connected in communication, wherein the memory is used to store a computer program, the transceiver is used to send and receive messages, and the processor is used to read the computer program and execute the gateway dynamic routing method as described in the first aspect. For example, the memory may include, but is not limited to, a random access memory (RAM), a read-only memory (ROM), a flash memory, a first-in first-out memory (FIFO) and / or a first-in last-out memory (FILO), etc.; the processor may include, but is not limited to, a microprocessor of the STM32F105 series. In addition, the computer device may also include, but is not limited to, a power module, a display screen and other necessary components.

[0066] The working process, working details and technical effects of the aforementioned computer device provided in the third aspect of this embodiment can be found in the gateway dynamic routing method described in the first aspect, and will not be repeated here.

[0067] A fourth aspect of this embodiment provides a computer-readable storage medium storing instructions including the gateway dynamic routing method described in the first aspect. Specifically, the computer-readable storage medium stores instructions that, when executed on a computer, execute the gateway dynamic routing method described in the first aspect. The computer-readable storage medium refers to a data storage medium and may include, but is not limited to, a floppy disk, an optical disk, a hard disk, a flash memory, a USB flash drive, and / or a memory stick. The computer may be a general-purpose computer, a dedicated computer, a computer network, or other programmable device.

[0068] The working process, working details and technical effects of the aforementioned computer-readable storage medium provided in the fourth aspect of this embodiment can be referred to the gateway dynamic routing method described in the first aspect, and will not be repeated here.

[0069] A fifth aspect of this embodiment provides a computer program product comprising instructions, which, when executed on a computer, causes the computer to execute the gateway dynamic routing method as described in the first aspect. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device.

[0070] Finally, it should be noted that the above description is only a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention shall be included in the scope of protection of the present invention.

Claims

1. A gateway dynamic routing method based on Nacos service, characterized in that: Executed by a server communicating with a SpringCloud Gateway-based router, including: In response to the first human-computer interaction operation, starting the Nacos service and creating a routing configuration file in the NacosConfig configuration center of the Nacos service, wherein the routing configuration file includes at least one piece of routing information of the initial configuration; In response to the second human-computer interaction operation, a Gateway microservice is created and a service configuration file is added, wherein the service configuration file includes the storage address of the routing configuration file and related data for gateway dynamic routing, the storage address is specified by the second human-computer interaction operation, the related data is configured by the second human-computer interaction operation, and the related data contains the namespace namespace of the NacosConfig configuration center, the configuration set unique identifier data-id and / or the configuration group unique identifier group-id; In response to a third human-computer interaction operation, creating a GatewayConfig.java file, wherein the GatewayConfig.java file includes the storage address and the related data read from the service configuration file; In response to the fourth human-computer interaction operation, a class is created to inherit the ApplicationEventPublisherAware interface to inject the event publisher ApplicationEventPublisher into the Gateway microservice, so that the Gateway microservice has the ability to publish events, specifically including: completing the initialization of the event push handle and adding a dynamic routing definition method; In response to the fifth human-computer interaction operation, another class is created to monitor changes in the routing configuration file, specifically including: specifying that the current object depends on the GatewayConfig.java file, initializing the configuration content for the Nacos service, specifying the routing configuration path through the NacosConfig configuration center to obtain the routing configuration file, and adding a listener to the NacosConfig configuration center to monitor changes in the routing configuration file; When the listener receives configuration update information indicating that the routing configuration file has changed, it triggers the call of the dynamic routing definition method and updates the changed routing configuration file to the application process of the Spring Cloud Gateway gateway.

2. The gateway dynamic routing method according to claim 1, characterized in that: Complete the initialization of the event push handle, including: completing the initialization of the event push handle by specifying the event publisher ApplicationEventPublisher.

3. The gateway dynamic routing method according to claim 1, characterized in that: The dynamic routing definition method includes a user definition method for adding routing information, updating routing information and / or deleting routing information.

4. The gateway dynamic routing method according to claim 3, characterized in that: In the dynamic route definition mode, the operation of reading route information is implemented through the route definition locator routeDefinitionLocator, and the operation of adding, updating and / or deleting route information is implemented through the route definition editor routeDefinitionWriter.

5. The gateway dynamic routing method according to claim 1, characterized in that: Specifying that the current object depends on the GatewayConfig.java file includes: specifying that the current object depends on the GatewayConfig.java file through the @DependsOn annotation.

6. The gateway dynamic routing method according to claim 1, characterized in that: Initializing the configuration content for the Nacos service includes: obtaining the storage address and the namespace namespace of the NacosConfig configuration center to serve as the configuration content for the Nacos service.

7. A gateway dynamic routing device based on Nacos service, characterized in that: Suitable for deployment in a server, wherein the server communication connection is based on a router of Spring Cloud Gateway, and the gateway dynamic routing device includes an operation response module and a trigger call module connected in communication; The operation response module is used to respond to the first human-computer interaction operation, start the Nacos service, and create a routing configuration file in the NacosConfig configuration center of the Nacos service, wherein the routing configuration file includes at least one routing information of the initial configuration; The operation response module is further used to respond to the second human-computer interaction operation, create a Gateway microservice, and add a service configuration file, wherein the service configuration file includes the storage address of the routing configuration file and related data for gateway dynamic routing, the storage address is specified by the second human-computer interaction operation, the related data is configured by the second human-computer interaction operation, and the related data contains the namespace namespace of the NacosConfig configuration center, the configuration set unique identifier data-id and / or the configuration group unique identifier group-id; The operation response module is further configured to respond to a third human-computer interaction operation and create a GatewayConfig.java file, wherein the GatewayConfig.java file includes the storage address and the related data read from the service configuration file; The operation response module is further used to respond to the fourth human-computer interaction operation, create a class to inherit the ApplicationEventPublisherAware interface, so as to inject the event publisher ApplicationEventPublisher into the Gateway microservice, so that the Gateway microservice has the ability to publish events, specifically including: completing the initialization of the event push handle and adding a dynamic routing definition method; The operation response module is further configured to respond to a fifth human-computer interaction operation and create another class to monitor changes in the routing configuration file, specifically including: specifying that the current object depends on the GatewayConfig.java file, initializing the configuration content for the Nacos service, obtaining the routing configuration file by specifying a routing configuration path through the NacosConfig configuration center, and adding a listener to the NacosConfig configuration center to monitor changes in the routing configuration file; The trigger call module is used to trigger the call of the dynamic routing definition method when the listener receives configuration update information indicating that the routing configuration file has changed, and update the changed routing configuration file to the application process of the Spring Cloud Gateway gateway.

8. A computer device, characterized in that: The invention comprises a memory, a processor and a transceiver which are communicatively connected in sequence, wherein the memory is used to store a computer program, the transceiver is used to send and receive messages, and the processor is used to read the computer program and execute the gateway dynamic routing method according to any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that The computer-readable storage medium stores instructions, and when the instructions are executed on the computer, the gateway dynamic routing method as described in any one of claims 1 to 6 is executed.

Citation Information

Patent Citations

  • Route updating method and device

    CN111314141A

  • Dynamic routing arrangement method and system realized based on Spring Cloud Gateway

    CN113794636A