Method and system for realizing dynamic thread pool based on micro-service framework and Nacos

By dynamically adjusting thread pool parameters using Nacos Configuration Center in the microservice framework, the problem that thread pool parameter configuration cannot meet the system processing requirements is solved, and dynamic adjustment without restarting the service is achieved, which improves the system's flexibility and processing capabilities.

CN119172218APending Publication Date: 2024-12-20WUHAN ZBANK CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411188290.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-28
Publication Date
2024-12-20

AI Technical Summary

Technical Problem

In the prior art, the thread pool parameter configuration cannot be dynamically adjusted, resulting in the need to restart the service to adjust the thread pool parameters when the concurrency volume changes, which is costly and inflexible.

Method used

Adopt the dynamic thread pool method based on the microservice framework and Nacos. By introducing Nacos service discovery and configuration dependencies, writing configuration files, and writing code to listen for changes in thread pool parameters, using the Nacos Configuration Center to dynamically adjust the number of core threads and maximum threads of the thread pool. Number and thread idle time.

Benefits of technology

It realizes dynamic adjustment of thread pool parameters without restarting the service, improves the flexibility and processing capabilities of the system, and reduces the modification cost.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119172218A_ABST
    Figure CN119172218A_ABST
Patent Text Reader

Abstract

The invention provides a method and system for realizing a dynamic thread pool based on a micro-service framework and Nacos, and the method comprises the following steps: S1, introducing the dependency of nacos service discovery and service configuration in a project, and writing a corresponding configuration file; s2, logging in a nacos configuration management system, and setting parameters of a thread pool core thread count, a maximum thread count and thread idle time; s3, compiling a code for monitoring the parameter change of the nacos thread pool; s4, modifying the thread pool parameter configuration of the corresponding service in the nacos configuration management system; and S5, printing the parameters of the core thread count and the maximum thread count of the thread pool before and after modification, configuring the parameters of the thread pool by using the nacos configuration center service, and writing a code for monitoring the configuration change of the nacos service thread pool. According to the method, the effect of dynamically configuring the thread pool parameters can be achieved, and the reasonable thread pool parameters are adjusted according to the actual situation to process the high-concurrency scene.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of computer business application research and development, and in particular to a method and system for realizing a dynamic thread pool based on a microservice framework and Nacos. Background Art

[0002] In background development, thread pool technology is often used, and the configuration of thread pool core parameters relies heavily on experience. However, due to the uncertainty in the system operation process, it is difficult for us to plan a reasonable thread pool parameter once and for all. We need to adjust the thread pool parameters appropriately according to the system's concurrency, the increasing number of users, and the temporary processing of large numbers of scenarios.

[0003] When adjusting the thread pool configuration parameters, it is generally necessary to restart the service, which makes the modification costly. One solution is to put the thread pool configuration on the platform side, and dynamically configure the core parameters according to the system operation status while running the development.

[0004] The existing thread pool is created by reading an initial configuration parameter (for example, the number of core threads is set to 5 and the maximum queue length is set to 1000). Once the line is completed, if the concurrency is too large, it is necessary to adjust the code and restart the service to achieve the effect of code optimization.

[0005] Taking Nacos as the service configuration center, a simple dynamic thread pool is implemented by modifying the number of core threads, maximum number of threads, maximum length of task queue, and thread idle time of the thread pool. Summary of the invention

[0006] In view of the technical problems existing in the prior art, the present invention provides a method and system for realizing dynamic thread pool based on microservice framework and Nacos, so as to avoid the problem that the online thread pool parameter configuration cannot meet the system processing requirements and the thread pool parameter setting code needs to be changed and the service needs to be restarted to take effect.

[0007] According to a first aspect of the present invention, the present invention provides a method for implementing a dynamic thread pool based on a microservice framework and Nacos, comprising the following steps:

[0008] Step S1. Introduce the service discovery and service configuration dependencies of nacos into the project, and write the corresponding configuration files;

[0009] Step S2. Log in to the nacos configuration management system and set the parameters of the number of core threads, maximum number of threads, and thread idle time of the thread pool;

[0010] Step S3. Write code to monitor changes in nacos thread pool parameters;

[0011] Step S4. Modify the thread pool parameter configuration of the corresponding service in the nacos configuration management system;

[0012] Step S5. Obtain the parameters of the core thread number and the maximum thread number of the thread pool before and after printing modification, use the nacos configuration center service to configure the thread pool parameters, and write the code to monitor the changes in the nacos service thread pool configuration.

[0013] Based on the above technical solution, the present invention can also make the following improvements.

[0014] Optionally, the configuration file includes:

[0015] Create two new configuration files bootstrap.yml and application.yml, where:

[0016] The bootstrap.yml file is used to configure the service port number and service name, nacos service discovery and service configuration address information;

[0017] The application.yml file is used to configure the startup environment.

[0018] Optionally, the code for writing a code to monitor changes in nacos thread pool parameters includes:

[0019] Define a dynamic thread pool configuration implementation class and implement the spring initialization interface. Add @RefreshScope and @Configuration annotations to the class.

[0020] Define the core thread number, maximum thread number, and thread idle time parameters in the nacos configuration as three private global variables, inject them into the thread pool configuration implementation class using the @Value annotation, and inject the thread pool creation class, nacos configuration management class NacosConfigManager, and nacos configuration property class NacosConfigProperties into the thread pool configuration implementation class using the @Autowired annotation.

[0021] Optionally, also include:

[0022] Define a monitoring method, for example, the method name is afterPropertiesSet, monitor the changes of nacos configuration file parameters, use the thread pool creation class to initialize a thread pool according to the nacos thread pool parameter configuration;

[0023] Then use the getConfigService() method of the NacosConfigManager class to add a listener addListener to listen to the order-service-dev.yml configuration file of the dev environment. Whenever the nacos configuration in order-service changes, the receiveConfigInfo method that changes the current thread pool parameter configuration is executed.

[0024] Optionally, also include:

[0025] Add a method for printing thread pool parameter information: for example, the method name is printThreadPoolStatus, which prints the number of core threads in the current thread pool, the number of threads executing tasks, the maximum number of threads in the thread pool, the number of submitted tasks, the queue length, and the thread idle time to verify whether the dynamic thread pool has successfully configured the thread pool parameters of nacos.

[0026] Optionally, also include:

[0027] Add a method to add tasks to the thread pool: for example, the method name is dynamicThreadPoolAddTask, which is used to test the operating efficiency of the thread pool and whether the parameter configuration is reasonable.

[0028] Optionally, also include:

[0029] Define a method to dynamically change the thread pool configuration: for example, the method name is changeThreadPoolConfig, which resets the number of core threads, maximum number of threads, and thread idle time of the current thread pool based on the parameters changed by monitoring the nacos configuration file.

[0030] Optionally, the modification of the thread pool parameter configuration of the corresponding service in the nacos configuration management system includes:

[0031] Write a test class to call the method named printThreadPoolStatus in step S3 to obtain the latest modified thread pool configuration parameter information.

[0032] Optionally, the method of obtaining the parameters of the number of core threads and the maximum number of threads in the thread pool before and after printing the modification, using the nacos configuration center service to configure the thread pool parameters, and writing the code for monitoring the changes in the nacos service thread pool configuration includes:

[0033] Find the order-service service on the nacos management page and click Configure to modify the thread pool parameters. Call the printThreadPoolStatus method in step S3 to obtain the current thread pool configuration parameter information.

[0034] Use the nacos configuration center service to configure thread pool parameters, and write code to monitor changes in the nacos service thread pool configuration.

[0035] According to a second aspect of the present invention, a system for implementing a dynamic thread pool based on a microservice framework and Nacos is provided, comprising:

[0036] The parameter acquisition module is used to introduce the service discovery and service configuration dependencies of nacos in the project and write the corresponding configuration files;

[0037] The first parameter processing module is used to log in to the nacos configuration management system and set the parameters of the number of core threads, the maximum number of threads, and the thread idle time of the thread pool;

[0038] The second parameter processing module is used to write the code for monitoring the changes of nacos thread pool parameters and modify the thread pool parameter configuration of the corresponding service in the nacos configuration management system;

[0039] The third parameter processing module is used to obtain the parameters of the core thread number and the maximum thread number of the thread pool before and after printing modification, use the nacos configuration center service to configure the thread pool parameters, and write the code to monitor the changes in the nacos service thread pool configuration.

[0040] Technical effects and advantages of the present invention:

[0041] The present invention provides a method and system for realizing dynamic thread pool based on microservice framework and Nacos. The thread pool configuration monitoring method is written through SpringBoot microservice framework and Nacos configuration center, and the parameters such as the number of core threads, the maximum number of threads, and the thread idle time of the thread pool are set through the front-end page of the nacos configuration system. The changes of the synchronous thread pool parameters are implemented through the monitoring method, thereby realizing the effect of dynamically changing the thread pool parameters.

[0042] Other features and advantages of the present invention will be described in the following description, and partly become apparent from the description, or understood by practicing the present invention. The purpose and other advantages of the present invention can be realized and obtained by the structures pointed out in the description, claims and drawings. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] Figure 1 It is a flow chart of a dynamic thread pool setting method provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0044] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0045] In this embodiment, technical terminology explanation: SpringBoot is a microservice framework; it is a Spring-based framework that aims to simplify the configuration and development process of Spring applications, and enables developers to quickly build independent, production-level applications through automatic configuration and the principle of convention over configuration.

[0046] Nacos: Alibaba's open source service configuration center; a dynamic service discovery, configuration management, and service management platform that makes it easier to build cloud-native applications. Dedicated to helping discover, configure, and manage microservices. Nacos provides a set of simple and easy-to-use feature sets that can help quickly implement dynamic service discovery, service configuration, service metadata, and traffic management. Build, deliver, and manage microservice platforms more agilely and easily. Nacos is a service infrastructure for building a modern application architecture centered on "services" (such as the microservice paradigm and the cloud-native paradigm).

[0047] It can be understood that based on the defects in the background technology, the embodiment of the present invention proposes a method for implementing a dynamic thread pool based on a microservice framework and Nacos, comprising the following steps:

[0048] Step S1. Introduce nacos service discovery nacos-discovery and service configuration nacos-config dependencies into the project, as shown in the figure:

[0049]

[0050] Then write the corresponding configuration files, including: creating two new configuration files bootstrap.yml and application.yml.

[0051] In the bootstrap.yml file, the port number and service name of the service, nacos service discovery and service configuration address are configured, as shown in the figure:

[0052]

[0053] In the application.yml file, it is used to configure the startup environment (dev\sit\uat\prod), as shown in the figure:

[0054] spring:

[0055] profiles:

[0056] actiVe:dev

[0057] Step S2. Log in to the nacos configuration management page, find the service named order-service, and click the edit button to set the parameters of the thread pool core thread number, maximum thread number, and thread idle time;

[0058] Step S3. Write the code for monitoring the changes of nacos thread pool parameters, including the following steps:

[0059] Define a dynamic thread pool configuration implementation class DynamicThreadPool, and implement the interface of spring initialization bean (InitializingBean), add @RefreshScope and @Configuration annotations to the class, and then define the core thread number, maximum thread number, and thread idle time parameters in the nacos configuration as three private global variables and inject them into the DynamicThreadPool class using the @Value annotation, and then inject the thread pool creation class ThreadPoolExecutor, the nacos configuration management class NacosConfigManager, and the nacos configuration property class NacosConfigProperties into the DynamicThreadPool class using the @Autowired annotation;

[0060] Define a listening method, for example, the method name is afterPropertiesSet, listen to the changes of nacos configuration file parameters, use ThreadPoolExecutor to initialize a thread pool according to the nacos thread pool parameter configuration, and then use the getConfigService() method of the NacosConfigManager class to add a listener addListener to listen to the order-service-dev.yml configuration file of the dev environment. Whenever the nacos configuration in order-service changes, the receiveConfigInfo method that changes the current thread pool parameter configuration will be executed;

[0061] Add a thread pool parameter information print method, such as the method named printThreadPoolStatus, which prints out the number of core threads in the current thread pool, the number of threads executing tasks, the maximum number of threads in the thread pool, the number of submitted tasks, the queue length, and the thread idle time, so as to verify whether the dynamic thread pool has successfully configured the thread pool parameters of nacos;

[0062] A new method 1 for adding tasks to the thread pool is added. This is used to test the operating efficiency and parameter configuration of the thread pool.

[0063] Finally, define a method to dynamically change the thread pool configuration. For example, the method name is changeThreadPoolConfig. The number of core threads, maximum number of threads, and thread idle time of the current thread pool are reset according to the parameters changed by monitoring the nacos configuration file.

[0064] Step S4. Modify the thread pool parameter configuration of the corresponding service in the nacos configuration management system;

[0065] In this embodiment, it specifically includes: calling the method named printThreadPoolStatus in step S3 to obtain the latest modified thread pool configuration parameter information.

[0066] Step S5. Obtain the parameters of the core thread number and the maximum thread number of the thread pool before and after printing modification, use the nacos configuration center service to configure the thread pool parameters, and write the code to monitor the changes in the nacos service thread pool configuration.

[0067] In this embodiment, the specific steps include: finding the order-service service on the nacos management page and clicking on the configuration to modify the thread pool parameters. Adjust the number of core threads to 50 and the maximum number of threads to 100, and then call the printThreadPoolStatus method in step S3 to obtain the current thread pool configuration parameter information;

[0068] Finally, use the nacos configuration center service to configure the thread pool parameters, and write code to monitor changes in the nacos service thread pool configuration, so that you can dynamically adjust the thread pool parameters without restarting the server.

[0069] In summary, an embodiment of the present invention provides a method for realizing a dynamic thread pool based on a microservice framework and Nacos. A thread pool configuration monitoring method is written through the SpringBoot microservice framework and the Nacos configuration center, and parameters such as the number of core threads, the maximum number of threads, and the thread idle time of the thread pool are set through the nacos configuration system front-end page. The changes in the synchronous thread pool parameters are implemented through the monitoring method, thereby achieving the effect of dynamically changing the thread pool parameters.

[0070] According to a second aspect of the present invention, a system for implementing a dynamic thread pool based on a microservice framework and Nacos is provided, comprising:

[0071] The parameter acquisition module is used to introduce the service discovery and service configuration dependencies of nacos in the project and write the corresponding configuration files;

[0072] The first parameter processing module is used to log in to the nacos configuration management system and set the parameters of the number of core threads, the maximum number of threads, and the thread idle time of the thread pool;

[0073] The second parameter processing module is used to write the code for monitoring the changes of nacos thread pool parameters and modify the thread pool parameter configuration of the corresponding service in the nacos configuration management system;

[0074] The third parameter processing module is used to obtain the parameters of the core thread number and the maximum thread number of the thread pool before and after printing modification, use the nacos configuration center service to configure the thread pool parameters, and write the code to monitor the changes in the nacos service thread pool configuration.

[0075] It can be understood that the system for implementing a dynamic thread pool based on a microservice framework and Nacos provided by the present invention corresponds to the method for implementing a dynamic thread pool based on a microservice framework and Nacos provided in the aforementioned embodiments. The relevant technical features of the system for implementing a dynamic thread pool based on a microservice framework and Nacos can refer to the relevant technical features of the method for implementing a dynamic thread pool based on a microservice framework and Nacos, which will not be repeated here.

[0076] In summary, the embodiments of the present invention provide a method and system for implementing a dynamic thread pool based on a microservice framework and Nacos, so as to avoid the problem that the online thread pool parameter configuration cannot meet the system processing requirements and the thread pool parameter setting code needs to be changed to restart the service to take effect (for example, a scheduled task that needs to be executed every day, the amount of data suddenly increases to one million, and the thread pool parameters are set too small or unreasonable before going online, which may cause insufficient system memory and reduced program running efficiency).

[0077] In addition, an embodiment of the present invention provides an electronic device, which includes: a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus. The processor can call logic instructions in the memory to execute a method for implementing a dynamic thread pool based on a microservice framework and Nacos.

[0078] In addition, the logic instructions in the above-mentioned memory can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0079] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0080] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0081] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.

[0082] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for implementing dynamic thread pool based on microservice framework and Nacos. It is characterized in that The following steps are involved: Step S1. Introduce the service discovery and service configuration dependencies of nacos into the project, and write the corresponding configuration files; Step S2. Log in to the nacos configuration management system and set the parameters of the number of core threads, maximum number of threads, and thread idle time of the thread pool; Step S3. Write code to monitor changes in nacos thread pool parameters; Step S4. Modify the thread pool parameter configuration of the corresponding service in the nacos configuration management system; Step S5. Obtain the parameters of the core thread number and the maximum thread number of the thread pool before and after printing modification, use the nacos configuration center service to configure the thread pool parameters, and write the code to monitor the changes in the nacos service thread pool configuration.

2. According to claim 1, a method for implementing a dynamic thread pool based on a microservice framework and Nacos, It is characterized in that In step S1, the writing of the corresponding configuration file includes: Create two new configuration files bootstrap.yml and application.yml, where: The bootstrap.yml file is used to configure the service port number and service name, nacos service discovery and service configuration address information; The application.yml file is used to configure the startup environment.

3. According to claim 1, a method for implementing a dynamic thread pool based on a microservice framework and Nacos, It is characterized in that In step S3, the code for writing and monitoring the changes of nacos thread pool parameters includes: Define a dynamic thread pool configuration implementation class to implement the spring initialization interface, and add @RefreshScope and @Configuration annotations to the configuration implementation class; Define the core thread number, maximum thread number, and thread idle time parameters in the nacos configuration as private global variables, inject them into the thread pool configuration implementation class using the @Value annotation, and inject the thread pool creation class, configuration management class, and nacos configuration property class into the thread pool configuration implementation class using the @Autowired annotation.

4. According to claim 3, a method for implementing a dynamic thread pool based on a microservice framework and Nacos, It is characterized in that Also includes: Define a method to monitor changes in nacos configuration file parameters, and use the thread pool creation class to initialize a thread pool according to the nacos thread pool parameter configuration; Use the getConfigService() method of the configuration management class to add a listener to listen to the order-service-dev.yml configuration file of the dev environment. Whenever the nacos configuration in the order-service changes, the method of changing the current thread pool parameter configuration is executed.

5. According to claim 4, a method for implementing a dynamic thread pool based on a microservice framework and Nacos, It is characterized in that Also includes: Add a method for printing thread pool parameter information to print out the number of core threads in the current thread pool, the number of threads executing tasks, the maximum number of threads in the thread pool, the number of submitted tasks, the queue length, and the thread idle time to verify whether the dynamic thread pool has successfully configured the thread pool parameters of nacos.

6. According to claim 5, a method for implementing a dynamic thread pool based on a microservice framework and Nacos, It is characterized in that Also includes: Add a method to add tasks to the thread pool to test the running efficiency of the thread pool and whether the parameter configuration is reasonable.

7. According to claim 6, a method for implementing a dynamic thread pool based on a microservice framework and Nacos, It is characterized in that Also includes: Define a method to dynamically change the thread pool configuration, and reset the number of core threads, maximum number of threads, and thread idle time of the current thread pool according to the parameters changed by monitoring the nacos configuration file.

8. According to claim 1, a method for implementing a dynamic thread pool based on a microservice framework and Nacos, It is characterized in that In step S4, the modification of the thread pool parameter configuration of the corresponding service in the nacos configuration management system includes: Write a test class to call the thread pool parameter information printing method in step S3 to obtain the latest modified thread pool configuration parameter information.

9. According to claim 1, a method for implementing a dynamic thread pool based on a microservice framework and Nacos, It is characterized in that In step S5, the parameters of the core number of threads and the maximum number of threads of the thread pool before and after the modification are obtained and printed, the thread pool parameters are configured using the nacos configuration center service, and the code for monitoring the configuration changes of the nacos service thread pool is written, including: Find the order-service service on the nacos management page and click Configure to modify the thread pool parameters. Call the thread pool parameter information printing method in step S3 to obtain the current thread pool configuration parameter information. Use the nacos configuration center service to configure the thread pool parameters, and write code to monitor changes in the nacos service thread pool configuration.

10. A system for implementing dynamic thread pool based on microservice framework and Nacos. It is characterized in that include: The parameter acquisition module is used to introduce the service discovery and service configuration dependencies of nacos in the project and write the corresponding configuration files; The first parameter processing module is used to log in to the nacos configuration management system and set the parameters of the number of core threads, the maximum number of threads, and the thread idle time of the thread pool; The second parameter processing module is used to write the code for monitoring the changes of nacos thread pool parameters and modify the thread pool parameter configuration of the corresponding service in the nacos configuration management system; The third parameter processing module is used to obtain the parameters of the core thread number and the maximum thread number of the thread pool before and after printing modification, use the nacos configuration center service to configure the thread pool parameters, and write the code to monitor the changes in the nacos service thread pool configuration.

Citation Information

Cited By

  • Dynamic AppId polling acquisition method and system

    CN120768700A