A method for multi-task scheduling and load balancing based on springboot
Through the multi-task scheduling system based on SpringBoot, combined with the edge task plug-in module, central task scheduling module, Nacos and database cluster, the problems of low resource utilization and insufficient flexibility of the traditional single-task scheduling system are solved, and efficient task scheduling and load balancing are achieved.
Patent Information
- Application Number
- CN202411363381.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-27
- Publication Date
- 2025-09-26
- Estimated Expiration
- 2044-09-27
AI Technical Summary
Traditional single-task scheduling systems have problems such as low resource utilization, long response time, high pressure, lack of flexibility and fault tolerance mechanism, and inability to dynamically adjust.
A multi-task scheduling method based on SpringBoot is adopted. Through the combination of edge task plug-in module, central task scheduling module, Nacos and database cluster, module registration, task control message delivery, status message reporting and load balancing are realized.
It improves resource utilization, reduces development and maintenance workload, solves the problems of low resource utilization, long response time, high pressure, lack of flexibility and fault tolerance mechanism of single applications, and realizes automatic offline of module failure and automatic online expansion.
Smart Images

Figure CN119336495B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to a method for realizing multi-task scheduling and load balancing based on Spring Boot. Background Art
[0002] Task schedulers are an indispensable component of modern software systems, playing a key role in distributed computing, cloud computing, big data processing, enterprise applications, and service automation. Traditional single-task scheduling, where a single application handles all tasks, suffers from low resource utilization, long response times, high load, and a lack of flexibility (inability to dynamically adjust and lacks fault tolerance). A SpringBoot-based approach to multi-task scheduling and load balancing utilizes multiple modules, with Nacos load balancing dispatching tasks to each module. In emergencies such as module failures, if Nacos fails to receive heartbeat messages from that module within a certain period, it immediately removes the module from the module list and reschedules the task to another functioning module. When system capacity needs to be expanded, the newly added module is started and automatically registered with Nacos, enabling automatic module rollout and expansion. The design and implementation of a multi-task scheduler involves numerous complex technologies and concepts. Key technical considerations include operating system scheduling principles, distributed systems, parallel and concurrent processing, resource management, dependency management, fault tolerance and recovery, time and periodicity management, and APIs and integration. A thorough understanding of these background technologies is crucial for designing and implementing efficient, reliable, and scalable task scheduling systems. Summary of the Invention
[0003] In view of the problems existing in the prior art, the purpose of the present invention is to provide a technical solution for a method of implementing multi-task scheduling and load balancing based on Spring Boot.
[0004] The method for implementing multi-task scheduling and load balancing based on springboot is characterized by comprising an edge task plug-in module, a central task scheduling module, Nacos and a database cluster;
[0005] The edge task plug-in module is used to be integrated into related applications. Once the edge task plug-in module completes module registration on Nacos, it only needs to call the relevant API to complete the communication and related operations between modules. These include: issuing task control messages according to user instructions; reporting task status messages according to task execution status; receiving task control messages and task status messages, and performing corresponding operations;
[0006] The central task scheduling module is used for centralized scheduling and management of multiple tasks. After the central task scheduling module completes module registration on Nacos, it only needs to call the relevant API to complete the communication and related operations between modules. It includes: receiving the task control message sent by the source edge task plug-in module, parsing the message and saving it in the database cluster, and forwarding the message to the destination edge task plug-in module; regularly querying the task information saved in the database cluster, and when the task processing is abnormal, actively sending the task control message to the destination edge task plug-in module; receiving the task status message reported by the destination edge task plug-in module, parsing the message and updating it in the database cluster, and forwarding the message to the source edge task plug-in module;
[0007] Nacos is used for module registration and configuration center management, including: registering and discovering the central task scheduling module and edge task plug-in module running in the server, and distributing message requests through load balancing strategies; saving various configuration files required by the module; reading relevant configuration files from Nacos when the module starts, and implementing configuration hot updates by modifying the Nacos configuration files while the module is running;
[0008] The database cluster is used to save task information. The task information table contains: unique ID, task unique number, task type, source service, destination service, task start time, task end time, task details, task status, creation time, update time, task group, cache identifier, and remarks. When the central task scheduling module receives the task information, it saves the task information to the task information table of the database cluster to support subsequent related operations and management of the task.
[0009] The method for implementing multi-task scheduling and load balancing based on springboot is characterized by including the following processes: module registration, task control message issuance, and task status message reporting; when the module is registered, the edge task plug-in module and the central task processing module register their own relevant information in Nacos, and each module finds the corresponding module information through the service name; when the task control message is issued, the source edge task plug-in module calls the API to issue the task control message to the central task scheduling module, and the central task scheduling module forwards the message to the corresponding destination edge task plug-in module, and the destination edge task plug-in module performs corresponding operation processing after receiving the message; when the task status message is reported, the destination edge task plug-in module calls the API according to the task processing situation and reports the task status message to the central task scheduling module, and the central task scheduling module forwards the message to the corresponding source edge task plug-in module, and the source edge task plug-in module performs corresponding operation processing after receiving the message.
[0010] The method for implementing multi-task scheduling and load balancing based on springboot is characterized in that the task control message field includes a unique ID, a task unique number, a task type, a source service, a destination service, a task start time, a task end time, task details, a task group, a cache identifier and remarks information; the task status message field includes: a task unique number, time and task status.
[0011] The method for implementing multi-task scheduling and load balancing based on Springboot is characterized by comprising the following steps:
[0012] 1) Before starting the edge task plug-in module, package the edge task plug-in module into a jar package, integrate the edge task plug-in module jar package into the application and develop it, finally package it into an executable application and configure the corresponding gRPC information;
[0013] 2) When the edge task plug-in module is started, it obtains relevant configuration parameters from the Nacos configuration center, obtains the server's external IP information, starts its own service IP, port, gRPC IP and gRPC port, and registers the gRPC IP, gRPC port address and weight with Nacos; starts the task monitoring thread and regularly reports the health status of the executing task;
[0014] 3) Before the central task scheduling module runs, configure the asynchronous task delivery thread pool, task restriction information, gRPC information, and database cluster, and finally start the database cluster service;
[0015] 4) When the central task scheduling module starts, it obtains relevant configuration parameters from the Nacos configuration center, connects to the database cluster according to the configuration, obtains the server's external IP information, starts its own service IP, port, gRPC IP and gRPC port, and registers the gRPC IP and gRPC port addresses with Nacos; starts the task processing thread and processes task control messages and task status messages;
[0016] 5) The user sends task control messages through the edge task plug-in module, including:
[0017] Sending new task messages: The source edge task plug-in module sends a new task message, calls the load balancing policy interface configured by Nacos to obtain the central task scheduling module information, and sends the new task message to the central task scheduling module through the gRPC protocol; after receiving the new message, the central task scheduling module determines whether the message is legal. If it is legal, it saves the task information to the database cluster; then it determines whether the task reaches the concurrency limit and whether it is cached. If there is no limit or the number of concurrent tasks is less than the limit, the message is forwarded directly; otherwise, the task is cached or rejected; when forwarding the message, the central task scheduling module calls the load balancing policy interface configured by Nacos to obtain the destination edge task plug-in module information, and forwards the message to the destination edge task plug-in module through the gRPC protocol; after the destination edge task plug-in module successfully receives the new task message, it processes it according to the task information;
[0018] Sending a task deletion message: The source edge task plug-in module sends a task deletion message, calls the load balancing policy interface configured by Nacos to obtain the central task scheduling module information, and sends the task deletion message to the central task scheduling module through the gRPC protocol; when the central task scheduling receives the deletion message or detects a task processing exception during scheduled monitoring, it calls the load balancing policy interface configured by Nacos to obtain the destination edge task plug-in module information, and sends a message to the destination edge task plug-in module through the gRPC protocol; after the destination edge task plug-in module successfully receives the task deletion message, it deletes the task according to the task unique number;
[0019] 6) The edge task plug-in module reports the task status when processing the task, including:
[0020] Task health status reporting: The destination edge task plug-in module periodically queries the health status of the task being executed, calls the load balancing policy interface configured by Nacos to obtain the information of the central task scheduling module, and reports the task health status message to the central task scheduling module through the gRPC protocol; when the central task scheduling module receives the task health status message, it determines whether the message is legal. If it is legal, the task status in the database cluster is updated to healthy according to the task unique number, and the update time is the time carried in the message. At the same time, the load balancing policy interface configured by Nacos is called to obtain the source edge task plug-in module information, and the task information is forwarded to the source edge task plug-in module; if it is illegal, the message is discarded and not processed;
[0021] Task completion status reporting: After the destination edge task plug-in module completes the task normally, it calls the load balancing policy interface configured by Nacos to obtain the central task scheduling module information, and reports the task completion status message to the central task scheduling module through the gRPC protocol; When the central task scheduling module receives the task completion status message, it determines whether the message is legal; if it is legal, it deletes the task information in the database cluster according to the task unique number, indicating that the task has been completed normally. At the same time, it calls the load balancing policy interface configured by Nacos to obtain the source edge task plug-in module information and forwards the task information to the source edge task plug-in module; if it is illegal, it discards the message without processing;
[0022] Task abnormality status reporting: When an abnormality occurs during task execution by the destination edge task plug-in module, the load balancing policy interface configured by Nacos is called to obtain the central task scheduling module information, and the task abnormality status message is reported to the central task scheduling module through the gRPC protocol. When the central task scheduling module receives the task abnormality status message, it determines whether the message is legal. If it is legal, the task status in the database cluster is updated as abnormal according to the task unique number, and the update time is the time carried in the message. At the same time, the load balancing policy interface configured by Nacos is called to obtain the source edge task plug-in module information, and the task information is forwarded to the source edge task plug-in module. If it is illegal, the message is discarded and not processed.
[0023] 7) When Nacos load balancing distributes messages, it includes:
[0024] Edge task plug-in module message load balancing: When the edge task plug-in module needs to report task health, completion, and abnormal status messages to the central task scheduling module, it calls the load balancing strategy interface configured by Nacos to obtain the central task scheduling module information. After receiving the interface request, Nacos obtains all IP, port, and weight information of the central task scheduling module through the interface parameter service name and group information; and returns the optimal central task scheduling module IP and port through load balancing strategies such as random, polling, weight, and minimum number of connections.
[0025] Central task scheduling module message load balancing: When the central task scheduling module needs to send task addition and deletion messages to the edge task plug-in module, it calls the load balancing strategy interface configured by Nacos to obtain the edge task plug-in module information. After receiving the interface request, Nacos obtains all IP, port, and weight information of the edge task plug-in module through the interface parameter service name and group information. Through the load balancing strategies such as random, polling, weight, and minimum number of connections, Nacos returns the optimal central task scheduling module IP and port.
[0026] The method for realizing multi-task scheduling and load balancing based on Spring Boot is characterized in that polling refers to selecting modules in sequence according to the order of the module list, which is applicable to the situation where all modules need to have a uniform load; random refers to randomly selecting a module for calling, which is applicable to the situation where there are a large number of modules and a large load difference; weight refers to selecting modules according to their weights, and modules with higher weights have a higher probability of being selected; and the minimum number of connections refers to selecting the module with the least number of current connections, which is applicable to the situation where each module needs to balance the current number of connections.
[0027] The method for implementing multi-task scheduling and load balancing based on SpringBoot is characterized in that the application integrates the edge task plug-in module, which means that the SpringBoot version of the edge task plug-in module must be consistent with the SpringBoot version of the application, the edge task plug-in module is packaged into a jar package and uploaded to the Maven library, and the application service introduces the Maven information of the edge task plug-in module jar package in the POM;
[0028] The gRPC information configuration refers to: creating a new gRPC configuration file in the Nacos configuration center, configuring the gRPC port information required by the module, and the gRPC IP information will automatically obtain the local external IP during system startup;
[0029] The asynchronous task dispatch thread pool configuration refers to: creating a new thread pool configuration file in the Nacos configuration center, configuring relevant thread pool information, including: the number of core threads, the maximum number of threads, the buffer queue, the system shutdown thread pool waiting time, the thread name, and the thread pool strategy;
[0030] The task restriction information configuration refers to: a task restriction configuration file in the Nacos configuration center, task restriction information in JSON string format, Key is the group to be restricted, and Value is the set of restricted quantities.
[0031] The method for implementing multi-task scheduling and load balancing based on springboot is characterized in that the conditions for judging whether the task message is legal in step 5) are as follows: whether it is an empty request, whether the task unique label is not empty and unique, whether the type is empty, whether the source service is empty, whether the destination service is empty, whether the task start time is empty and less than the task end time, whether the task end time is empty and greater than the task start time, whether the task details are empty and are a JSON string, whether the task group is empty, and whether the cache identifier is empty. If any of the above conditions is not met, it is illegal.
[0032] The method for implementing multi-task scheduling and load balancing based on Springboot is characterized in that the condition for judging whether the task is limited in concurrency in step 5) is as follows: when the task group exists in the set of concurrency restrictions and the number of tasks being executed by the task group is greater than the restricted number, concurrency is restricted.
[0033] The method for implementing multi-task scheduling and load balancing based on springboot is characterized in that the condition for judging whether the task status message is legal in step 6) is as follows: if the reporting time in the task status message is less than the current system time, it is legal.
[0034] The present invention is based on the SpringBoot framework. Any application developed based on SpringBoot can integrate the edge task plug-in module to realize message interaction with the central task scheduling module, which can realize the task control message sending and task status message reporting functions, thereby reducing the development workload and subsequent maintenance workload of developers; through multi-task load balancing scheduling, automatic offline of module failure, automatic online of module expansion and other operations, it solves the problems of low resource utilization, long response time, high pressure, lack of flexibility (unable to adjust dynamically, lack of fault tolerance mechanism) of single application, and can be applied to various software systems involving multi-task scheduling and load balancing requirements. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] Figure 1 This is a schematic diagram of the entire process of multi-task scheduling and load balancing of the present invention;
[0036] Figure 2 Register the flow chart for the module of the present invention;
[0037] Figure 3 Add a message sending flow chart for the task of the present invention;
[0038] Figure 4 This is a flow chart of sending a task deletion message of the present invention;
[0039] Figure 5 This is a flowchart for reporting the task health status of the present invention;
[0040] Figure 6 This is a flowchart for reporting the task completion status of the present invention;
[0041] Figure 7 This is a flowchart for reporting abnormal task status of the present invention. DETAILED DESCRIPTION
[0042] The present invention will be further described below in conjunction with the accompanying drawings:
[0043] A method for implementing multi-task scheduling and load balancing based on SpringBoot includes an edge task plug-in module, a central task scheduling module, Nacos, and a database cluster.
[0044] The edge task plug-in module is designed to be integrated into related applications. Once the edge task plug-in module completes module registration on Nacos (including service name, group name, weight, IP address, and port, etc.), communication and related operations between modules can be completed by simply calling the relevant API. These include: issuing task control messages (such as adding tasks and deleting tasks) according to user instructions; reporting task status messages (such as task health, task completion, and task exceptions) based on task execution status; receiving task control messages and task status messages and performing corresponding operations.
[0045] The central task scheduling module is used for centralized scheduling and management of multiple tasks. Once the central task scheduling module completes module registration on Nacos (including service name, group name, weight, IP address, and port, etc.), it can complete communication and related operations between modules by simply calling relevant APIs. This includes: receiving task control messages sent by the source edge task plug-in module, parsing the messages, saving them in the database cluster, and forwarding the messages to the destination edge task plug-in module; regularly querying task information stored in the database cluster; and proactively sending task control messages (such as deleting tasks) to the destination edge task plug-in module when task processing is abnormal (such as task timeout); receiving task status messages reported by the destination edge task plug-in module, parsing the messages, updating them in the database cluster, and forwarding the messages to the source edge task plug-in module.
[0046] Nacos is used for module registration and configuration center management. This includes registering and discovering central task scheduling modules and edge task plug-in modules running on the server, distributing message requests using load balancing strategies (such as random, round-robin, weighted, and minimum number of connections), and storing various configuration files required by the modules. When a module starts, it reads the relevant configuration files from Nacos. During module operation, the configuration can be hot-updated by modifying the Nacos configuration files.
[0047] The database cluster stores task information. The task information table primarily includes the following: unique ID, task unique number, task type, source service, destination service, task start time, task end time, task details, task status, creation time, update time, task group, cache identifier, and notes. Upon receiving task information, the central task scheduling module saves it to the task information table in the database cluster to support subsequent task operations and management.
[0048] The process of the present invention includes: module registration, task control message issuance, and task status message reporting. When the module is registered, the edge task plug-in module and the central task processing module register their own relevant information (including service name, group name, weight, IP address and port, etc.) in Nacos, and each module finds the corresponding module information through the service name. When the task control message is issued, the source edge task plug-in module calls the API and sends the task control message to the central task scheduling module. The central task scheduling module forwards the message to the corresponding destination edge task plug-in module. The destination edge task plug-in module performs corresponding operation processing after receiving the message. When the task status message is reported, the destination edge task plug-in module calls the API according to the task processing situation and reports the task status message to the central task scheduling module. The central task scheduling module forwards the message to the corresponding source edge task plug-in module. The source edge task plug-in module performs corresponding operation processing after receiving the message.
[0049] The task control message field of the present invention includes unique ID, task unique number, task type, source service, destination service, task start time, task end time, task details, task group, cache identifier, and remarks information; the task status message field includes: task unique number, time, and task status.
[0050] The method of the present invention comprises the following steps:
[0051] 1) Start the Nacos service and database cluster service first. Before starting the application, integrate the edge task plug-in module and register it in Nacos. Before starting the central task scheduling module, register it in Nacos and configure the database cluster. The details are as follows:
[0052] Edge task plug-in module integration: Package the edge task plug-in module into a jar package, integrate the edge task plug-in module jar package into the application and develop it. The application Spring Boot version must be consistent with the edge task plug-in module version. Finally, package the executable program containing the edge task plug-in module and configure the corresponding gRPC information.
[0053] Nacos configuration: Use the Java startup command: -Dnacos.config.server-addr and -Dnacos.config.namespace. nacos.config.server-addr includes the Nacos IP address and port, and nacos.config.namespace is the Nacos namespace name;
[0054] Start the Nacos service: Unzip Nacos and unzip the downloaded Nacos package to a specified directory. Run Nacos, enter the Nacos bin directory, and run the corresponding startup script. For standalone mode, you can directly run the Nacos startup script. In a Linux environment, you can use the following command: . / bin / startup.sh -m standalone.
[0055] 2) When the edge task plug-in module is started, it obtains relevant configuration parameters from the Nacos configuration center, obtains the server's external IP information, starts its own service IP, port, gRPC IP and port, and registers the gRPC IP, port address and weight in the Nacos list. It also starts the task monitoring thread and regularly reports the health status of the executing task.
[0056] 3) Before the central task scheduling module runs, configure the asynchronous task delivery thread pool, task restriction information, gRPC information, and database cluster, and finally start the database cluster service.
[0057] 4) When the central task scheduling module starts, it obtains relevant configuration parameters from the Nacos configuration center, connects to the database cluster according to the configuration, obtains the server's external IP information, starts its own service IP, port, gRPC IP and port, and registers the gRPC IP and port address with Nacos. It then starts the task processing thread and processes task control messages and task status messages.
[0058] 5) The user sends task control messages through the edge task plug-in module, including:
[0059] Sending a new task message: The source edge task plug-in module sends a new task message (including unique ID, task unique number, task type, source service, destination service, task start time, task end time, task details, task group, cache flag, and notes). It then calls the load balancing policy interface configured in Nacos (e.g., random, round-robin, weighted, minimum number of connections, etc.) to obtain information from the central task scheduling module and sends the new task message to the central task scheduling module via the gRPC protocol. Upon receiving the new task message, the central task scheduling module determines whether the message is valid (e.g., whether it is an empty request, whether the task unique number is not empty and unique, whether the type is empty, whether the source service is empty, whether the destination service is empty, whether the task start time is empty and less than the task end time, whether the task end time is empty and greater than the task start time, whether the task details are empty and a JSON string, whether the task group is empty, and whether the cache flag is empty). If so, the task information is saved to the database cluster. The central task scheduling module then determines whether the task has reached the concurrency limit (for example, when the task group is within the concurrency limit set and the number of tasks currently being executed by the task group is greater than the limit) and whether to cache the task. If there is no limit or the number is less than the concurrency limit, the message is forwarded directly; otherwise, the task is cached or rejected. When forwarding the message, the central task scheduling module calls the load balancing policy interface configured in Nacos (such as random, polling, weighted, minimum number of connections, etc.) to obtain the destination edge task plug-in module information and forwards the message to the destination edge task plug-in module via the gRPC protocol. After successfully receiving the new task message, the destination edge task plug-in module processes it based on the task information.
[0060] Sending a task deletion message: The source edge task plug-in module sends a task deletion message (e.g., unique ID, task unique number, and notes), calls the load balancing policy interface configured in Nacos (e.g., random, round-robin, weighted, or minimum number of connections) to obtain information about the central task scheduling module, and then sends the task deletion message to the central task scheduling module via gRPC. Upon receiving the deletion message or detecting a task processing exception during scheduled monitoring, the central task scheduling module calls the load balancing policy interface configured in Nacos (e.g., random, round-robin, consistent hashing, and weighted round-robin) to obtain information about the destination edge task plug-in module and sends a message to the destination edge task plug-in module via gRPC. After successfully receiving the task deletion message, the destination edge task plug-in module deletes the task based on its unique number.
[0061] 6) The edge task plug-in module reports the task status when processing the task, including:
[0062] Task health status reporting: The destination edge task plug-in module periodically queries the health status of the executing task, calls the load balancing policy interface configured in Nacos (such as random, round-robin, consistent hashing, and weighted round-robin) to obtain information from the central task scheduling module, and reports the task health status message (task unique number, time, and task status) to the central task scheduling module via the gRPC protocol. Upon receiving the task health status message, the central task scheduling module determines whether the message is valid (for example, if the reporting time is less than the current system time). If valid, it updates the task status in the database cluster to healthy based on the task unique number, with the update time being the time carried in the message. It also calls the load balancing policy interface configured in Nacos (such as random, round-robin, weighted, and minimum number of connections) to obtain information from the source edge task plug-in module and forwards the task information to the source edge task plug-in module. If invalid, the message is discarded and not processed.
[0063] Task Completion Status Reporting: After the destination edge task plug-in module successfully completes its task, it calls the load balancing policy interface configured in Nacos (e.g., random, round-robin, consistent hashing, and weighted round-robin) to obtain information from the central task scheduling module. It then reports the task completion status message (task unique number, time, and task status) to the central task scheduling module via gRPC. Upon receiving the task completion status message, the central task scheduling module determines whether the message is valid (e.g., if the reported time is less than the current system time). If valid, it deletes the task information from the database cluster based on the task unique number, indicating that the task has successfully completed. It also calls the load balancing policy interface configured in Nacos (e.g., random, round-robin, weighted, and minimum number of connections) to obtain information from the source edge task plug-in module and forwards the task information to the source edge task plug-in module. If invalid, the message is discarded and not processed.
[0064] Task exception status reporting: When an exception occurs while executing a task, the destination edge task plug-in module calls the load balancing policy interface configured in Nacos (e.g., random, round-robin, weighted, minimum number of connections, etc.) to obtain information from the central task scheduling module. It then reports the task exception status message (task unique number, time, and task status) to the central task scheduling module via the gRPC protocol. Upon receiving the task exception status message, the central task scheduling module determines whether the message is valid (e.g., if the reporting time is less than the current system time). If valid, the task status in the database cluster is updated to abnormal based on the task unique number, with the update time being the time carried in the message. At the same time, the load balancing policy interface configured in Nacos (e.g., random, round-robin, weighted, minimum number of connections, etc.) is called to obtain information from the source edge task plug-in module and forward the task information to the source edge task plug-in module. If invalid, the message is discarded and not processed.
[0065] 7) When Nacos load balancing distributes messages, it includes:
[0066] Edge task plug-in module message load balancing: When the edge task plug-in module needs to report task health, completion, exception and other status messages to the central task scheduling module, it calls the load balancing strategy interface configured by Nacos (where the parameters include service name, group name, strategy mode, etc.) to obtain the central task scheduling module information. After receiving the interface request, Nacos obtains all IP, port, and weight information of the central task scheduling module through the interface parameters service name and group information. Through load balancing strategies such as random, polling, weight, and minimum number of connections, it returns the optimal central task scheduling module IP and port;
[0067] Central Task Scheduling Module Message Load Balancing: When the central task scheduling module needs to send messages such as task addition and deletion to the edge task plug-in module, it calls the load balancing policy interface configured in Nacos (where parameters include service name, group name, and policy mode) to obtain the edge task plug-in module information. After receiving the interface request, Nacos obtains all IP addresses, ports, and weight information of the edge task plug-in module based on the interface parameters service name and group information. Using load balancing strategies such as random, round-robin, weighted, and least number of connections, it returns the optimal central task scheduling module IP and port.
[0068] Round robin selects modules in the order of their list, suitable for scenarios where all modules need to have a balanced load. Random selects a module at random, suitable for scenarios where there are a large number of modules with varying loads. Weight selects modules based on their weights, with modules with higher weights being more likely to be selected. Least connections selects the module with the fewest connections, suitable for scenarios where connections need to be balanced across modules.
[0069] The application integration of the edge task plug-in module in the present invention means that the Spring Boot version of the edge task plug-in module must be consistent with the Spring Boot version of the application. The edge task plug-in module is packaged into a jar package and uploaded to the Maven repository. The application service introduces the Maven information of the edge task plug-in module jar package in the POM.
[0070] gRPC information configuration means: creating a new gRPC configuration file in the Nacos configuration center, configuring the gRPC port information required by the module, and the gRPC IP information will automatically obtain the local external IP during system startup.
[0071] Asynchronous task delivery thread pool configuration means: creating a new thread pool configuration file in the Nacos Configuration Center and configuring related thread pool information, including: number of core threads, maximum number of threads, buffer queue, system shutdown thread pool wait time, thread name, and thread pool strategy.
[0072] Task restriction information configuration refers to the task restriction configuration file in the Nacos configuration center. Task restriction information is in JSON string format, the Key is the group to be restricted, and the Value is the set of restricted quantities.
[0073] The conditions for judging whether the task message is legal in step 5) above are as follows: whether it is an empty request, whether the task unique identifier is not empty and unique, whether the type is empty, whether the source service is empty, whether the destination service is empty, whether the task start time is empty and less than the task end time, whether the task end time is empty and greater than the task start time, whether the task details are empty and a JSON string, whether the task group is empty, and whether the cache identifier is empty. If any of the above conditions is not met, the judgment is illegal.
[0074] The conditions for determining whether the task is concurrency restricted in the above step 5) are as follows: when the task group exists in the set of concurrency restrictions and the number of tasks being executed by the task group is greater than the restricted number, concurrency is restricted.
[0075] The condition for judging whether the task status message is legal in the above step 6) is as follows: if the reporting time in the task status message is less than the current system time, it is legal.
[0076] The present invention will be further described with reference to the accompanying drawings:
[0077] The edge task plug-in module is used to integrate into related applications. When the edge task plug-in module completes module registration on Nacos (see Figure 1 After that, you only need to call the relevant API to complete the communication and related operations between modules.
[0078] The module task process includes: issuing task control and reporting task status. The issuing task control means that the source edge task plug-in module calls the Nacos API to obtain the central task scheduling module information (see Appendix Figure 1 ②), the task control message is sent to the central task scheduling module through the gRPC protocol (see Appendix Figure 1 After receiving the control message, the central task scheduling module saves the task information to the database cluster (see Appendix Figure 1 At the same time, the central task scheduling module calls the Nacos API to obtain the target edge task plug-in module information (see Appendix Figure 1 ⑤), forward the message to the destination edge task plug-in module (attached) through the gRPC protocol. Figure 1 After the destination edge task plug-in module successfully receives the task control message, it performs the corresponding operation according to the task control information.
[0079] When the central task scheduling module detects a task processing exception, it calls the Nacos API to obtain the target edge task plug-in module information (see Appendix Figure 1 ⑤), send the task control message to the destination edge task plug-in module (attached) through the gRPC protocol. Figure 1 After the destination edge task plug-in module successfully receives the task control message, it performs the corresponding operation according to the task control information.
[0080] Reporting task status means that the target edge task plug-in module calls the Nacos API to obtain the central task scheduling module information (see Appendix Figure 1 ⑧), report the task status message to the central task scheduling module through the gRPC protocol (see Appendix Figure 1 After receiving the message, the central task scheduling module updates the task information to the database cluster (see Figure 1 At the same time, the central task scheduling module calls the Nacos API to obtain the source edge task plug-in module information (see Appendix Figure 1 ⑤ in the middle), forward the message to the source edge task plug-in module (attached) through the gRPC protocol Figure 1 After the source edge task plug-in module successfully receives the task control message, it performs corresponding operations based on the task status information.
[0081] The edge task plug-in module of this invention can be integrated into any application developed based on the Spring Boot framework. The central task scheduling module centrally manages tasks, allowing developers to improve efficiency, enhance code quality, promote agile development, reduce risks, and foster innovation. Enterprises can reduce personnel costs, improve efficiency, and boost innovation and market competitiveness.
[0082] Notes:
[0083] Spring Boot: An open source project based on the Spring Framework that aims to simplify the initial setup and development of Spring applications. Spring Boot provides an out-of-the-box experience that enables developers to quickly build independent, production-grade Spring-based applications.
[0084] Nacos, short for Dynamic Naming and Configuration Service, is a dynamic service discovery, configuration management, and service management platform that makes it easier to build cloud-native applications. Nacos is dedicated to helping you discover, configure, and manage microservices. It provides a set of easy-to-use features to help you quickly implement dynamic service discovery, service configuration, service metadata, and traffic management. Nacos helps you build, deliver, and manage microservices platforms more agilely and easily. Nacos is the service infrastructure for building modern service-centric application architectures, such as the microservices paradigm and the cloud-native paradigm.
[0085] gRPC: gRPC is a modern, open-source, high-performance remote procedure call (RPC) framework that can run in any environment. It can efficiently connect services within and across data centers, supporting load balancing, tracing, health checks, and authentication. It is also suitable for the last mile of distributed computing, connecting devices, mobile applications, and browsers to backend services.
[0086] An API (Application Programming Interface) is a software architecture design that defines how different software components interact with each other. APIs allow different software modules to communicate with each other without needing to know each other's internal implementation details. APIs play a crucial role in modern software development, particularly in areas such as distributed systems, microservices architectures, mobile app development, and web services.
[0087] jar: In Java projects, .jar (Java ARchive) files are a common packaging format used to encapsulate multiple class files, associated metadata, and other resources (such as images and configuration files). .jar files can contain not only compiled bytecode files (.class files), but also dependent libraries and other resource files.
[0088] POM: In Java projects, especially those using the Maven build tool, the pom.xml (Project Object Model) file is the core configuration file for the project. It contains project metadata and build settings, including dependency management, build lifecycle, plugin configuration, and other project-related information.
[0089] Maven: Maven is a popular Java project management and build tool that helps developers manage project builds, dependencies, and documentation by providing a Project Object Model (POM). Maven's design philosophy is "convention over configuration," which means that Maven simplifies project management and build processes through predefined directory structures and build lifecycles.
Claims
1. A method for implementing multi-task scheduling and load balancing based on springboot, characterized in that Includes edge task plug-in module, central task scheduling module, Nacos and database cluster; The edge task plug-in module is used to be integrated into related applications. Once the edge task plug-in module completes module registration on Nacos, it only needs to call the relevant API to complete the communication and related operations between modules. These include: issuing task control messages according to user instructions; reporting task status messages according to task execution status; receiving task control messages and task status messages, and performing corresponding operations; The central task scheduling module is used for centralized scheduling and management of multiple tasks. After the central task scheduling module completes module registration on Nacos, it only needs to call the relevant API to complete the communication and related operations between modules. It includes: receiving the task control message sent by the source edge task plug-in module, parsing the message and saving it in the database cluster, and forwarding the message to the destination edge task plug-in module; regularly querying the task information saved in the database cluster, and when the task processing is abnormal, actively sending the task control message to the destination edge task plug-in module; receiving the task status message reported by the destination edge task plug-in module, parsing the message and updating it in the database cluster, and forwarding the message to the source edge task plug-in module; Nacos is used for module registration and configuration center management, including: registering and discovering the central task scheduling module and edge task plug-in module running in the server, and distributing message requests through load balancing strategies; saving various configuration files required by the module; reading relevant configuration files from Nacos when the module starts, and implementing configuration hot updates by modifying the Nacos configuration files while the module is running; The database cluster is used to save task information. The task information table contains: unique ID, task unique number, task type, source service, destination service, task start time, task end time, task details, task status, creation time, update time, task group, cache identifier, and remarks. When the central task scheduling module receives the task information, it saves the task information to the task information table of the database cluster to support subsequent related operations and management of the task.
2. The method for implementing multi-task scheduling and load balancing based on springboot according to claim 1 is characterized in that It includes the following processes: module registration, task control message issuance, and task status message reporting; when registering the module, the edge task plug-in module and the central task processing module register their own relevant information in Nacos, and each module finds the corresponding module information through the service name; when the task control message is issued, the source edge task plug-in module calls the API and sends the task control message to the central task scheduling module. The central task scheduling module forwards the message to the corresponding destination edge task plug-in module. The destination edge task plug-in module performs corresponding operations after receiving the message; when reporting the task status message, the destination edge task plug-in module calls the API according to the task processing status and reports the task status message to the central task scheduling module. The central task scheduling module forwards the message to the corresponding source edge task plug-in module. The source edge task plug-in module performs corresponding operations after receiving the message.
3. The method for realizing multi-task scheduling and load balancing based on springboot according to claim 2 is characterized in that The task control message fields include unique ID, task unique number, task type, source service, destination service, task start time, task end time, task details, task group, cache identifier and remarks; The task status message fields include: task unique number, time and task status.
4. The method for realizing multi-task scheduling and load balancing based on springboot according to claim 1 is characterized in that The steps include: 1) Before starting the edge task plug-in module, package the edge task plug-in module into a jar package, integrate the edge task plug-in module jar package into the application and develop it, finally package it into an executable application and configure the corresponding gRPC information; 2) When the edge task plug-in module is started, it obtains relevant configuration parameters from the Nacos configuration center, obtains the server's external IP information, starts its own service IP, port, gRPC IP and gRPC port, and registers the gRPC IP, gRPC port address and weight with Nacos; Start the task monitoring thread and report the health status of the tasks being executed regularly; 3) Before the central task scheduling module runs, configure the asynchronous task delivery thread pool, task restriction information, gRPC information, and database cluster, and finally start the database cluster service; 4) When the central task scheduling module starts, it obtains relevant configuration parameters from the Nacos configuration center, connects to the database cluster according to the configuration, obtains the server's external IP information, starts its own service IP, port, gRPC IP and gRPC port, and registers the gRPC IP and gRPC port addresses with Nacos; Start the task processing thread to process task control messages and task status messages; 5) The user sends task control messages through the edge task plug-in module, including: Sending new task messages: The source edge task plug-in module sends a new task message, calls the load balancing policy interface configured by Nacos to obtain the central task scheduling module information, and sends the new task message to the central task scheduling module through the gRPC protocol; after receiving the new message, the central task scheduling module determines whether the message is legal. If it is legal, it saves the task information to the database cluster; then it determines whether the task reaches the concurrency limit and whether it is cached. If there is no limit or the number of concurrent tasks is less than the limit, the message is forwarded directly; otherwise, the task is cached or rejected; when forwarding the message, the central task scheduling module calls the load balancing policy interface configured by Nacos to obtain the destination edge task plug-in module information, and forwards the message to the destination edge task plug-in module through the gRPC protocol; after the destination edge task plug-in module successfully receives the new task message, it processes it according to the task information; Sending a task deletion message: The source edge task plug-in module sends a task deletion message, calls the load balancing policy interface configured by Nacos to obtain the central task scheduling module information, and sends the task deletion message to the central task scheduling module through the gRPC protocol; when the central task scheduling receives the deletion message or detects a task processing exception during scheduled monitoring, it calls the load balancing policy interface configured by Nacos to obtain the destination edge task plug-in module information, and sends a message to the destination edge task plug-in module through the gRPC protocol; after the destination edge task plug-in module successfully receives the task deletion message, it deletes the task according to the task unique number; 6) The edge task plug-in module reports the task status when processing the task, including: Task health status reporting: The destination edge task plug-in module periodically queries the health status of the executing task, calls the load balancing policy interface configured by Nacos to obtain the central task scheduling module information, and reports the task health status message to the central task scheduling module through the gRPC protocol; when the central task scheduling module receives the task health status message, it determines whether the message is legal; if it is legal, it updates the task status in the database cluster to healthy according to the task unique number, and the update time is the time carried in the message. At the same time, it calls the load balancing policy interface configured by Nacos to obtain the source edge task plug-in module information and forwards the task information to the source edge task plug-in module; if it is illegal, the message is discarded and not processed; Task completion status reporting: After the destination edge task plug-in module completes the task normally, it calls the load balancing policy interface configured by Nacos to obtain the central task scheduling module information, and reports the task completion status message to the central task scheduling module through the gRPC protocol; When the central task scheduling module receives the task completion status message, it determines whether the message is legal; if it is legal, it deletes the task information in the database cluster according to the task unique number, indicating that the task has been completed normally. At the same time, it calls the load balancing policy interface configured by Nacos to obtain the source edge task plug-in module information and forwards the task information to the source edge task plug-in module; if it is illegal, it discards the message without processing; Task abnormality status reporting: When an abnormality occurs during task execution by the destination edge task plug-in module, the load balancing policy interface configured by Nacos is called to obtain the central task scheduling module information, and the task abnormality status message is reported to the central task scheduling module through the gRPC protocol. When the central task scheduling module receives the task abnormality status message, it determines whether the message is legal. If it is legal, the task status in the database cluster is updated as abnormal according to the task unique number, and the update time is the time carried in the message. At the same time, the load balancing policy interface configured by Nacos is called to obtain the source edge task plug-in module information, and the task information is forwarded to the source edge task plug-in module. If it is illegal, the message is discarded and not processed. 7) When Nacos load balancing distributes messages, it includes: Edge task plug-in module message load balancing: When the edge task plug-in module needs to report task health, completion, or exception status messages to the central task scheduling module, it calls the load balancing strategy interface configured by Nacos to obtain the central task scheduling module information. After receiving the interface request, Nacos obtains all IP, port, and weight information of the central task scheduling module through the interface parameter service name and group information. It returns the optimal central task scheduling module IP and port through load balancing strategies such as random, polling, weight, and minimum number of connections. Central task scheduling module message load balancing: When the central task scheduling module needs to send task addition and deletion messages to the edge task plug-in module, it calls the load balancing strategy interface configured by Nacos to obtain the edge task plug-in module information. After receiving the interface request, Nacos obtains all IP, port, and weight information of the edge task plug-in module through the interface parameter service name and group information. Through the load balancing strategies such as random, polling, weight, and minimum number of connections, Nacos returns the optimal central task scheduling module IP and port.
5. The method for realizing multi-task scheduling and load balancing based on springboot according to claim 4 is characterized in that The polling method refers to selecting modules in the order of the module list, which is suitable for situations where all modules need to have a uniform load. The random method refers to randomly selecting a module for calling, which is suitable for situations where there are a large number of modules and large differences in load. The weight method refers to selecting modules based on their weights, and modules with higher weights have a higher probability of being selected. The minimum number of connections refers to selecting the module with the least current number of connections, which is suitable for situations where each module needs to balance the current number of connections.
6. The method for realizing multi-task scheduling and load balancing based on springboot according to claim 4 is characterized in that The application integration edge task plug-in module means: the SpringBoot version of the edge task plug-in module must be consistent with the SpringBoot version of the application, the edge task plug-in module is packaged into a jar package and uploaded to the Maven library, and the application service introduces the edge task plug-in module jar package Maven information in the POM; The gRPC information configuration refers to: creating a new gRPC configuration file in the Nacos configuration center, configuring the gRPC port information required by the module, and the gRPC IP information will automatically obtain the local external IP during system startup; The asynchronous task dispatch thread pool configuration refers to: creating a new thread pool configuration file in the Nacos configuration center, configuring relevant thread pool information, including: the number of core threads, the maximum number of threads, the buffer queue, the system shutdown thread pool waiting time, the thread name, and the thread pool strategy; The task restriction information configuration refers to: a task restriction configuration file in the Nacos configuration center, task restriction information in JSON string format, Key is the group to be restricted, and Value is the set of restricted quantities.
7. The method for realizing multi-task scheduling and load balancing based on springboot according to claim 4 is characterized in that The conditions for judging whether the task message is legal in step 5) are as follows: whether it is an empty request, whether the task unique label is not empty and unique, whether the type is empty, whether the source service is empty, whether the destination service is empty, whether the task start time is empty and less than the task end time, whether the task end time is empty and greater than the task start time, whether the task details are empty and a JSON string, whether the task group is empty, and whether the cache identifier is empty. If any of the above conditions is not met, the judgment is illegal.
8. The method for realizing multi-task scheduling and load balancing based on springboot according to claim 4 is characterized in that The condition for determining whether the task is concurrency restricted in step 5) is as follows: when the task group exists in the concurrency restricted set and the number of tasks being executed by the task group is greater than the restricted number, concurrency is restricted.
9. The method for realizing multi-task scheduling and load balancing based on springboot according to claim 4 is characterized in that The condition for judging whether the task status message is legal in step 6) is as follows: if the reporting time in the task status message is less than the current system time, it is legal.
Citation Information
Patent Citations
Gateway load balancing method based on Nacos
CN116346725A
Task scheduling method, apparatus and system
WO2022166480A1