NSQ-based consumption throttling method, device and equipment and storage medium
Patent Information
- Application Number
- CN202310291941.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-17
- Publication Date
- 2026-09-18
- Estimated Expiration
- 2043-03-17
AI Technical Summary
[0005]在本实施例中提供了一种基于NSQ的消费限流方法、装置、设备和存储介质,以解决相关技术中客户端存在多个异步消息消费时,会导致客户端和下游服务的稳定性降低的问题
[0033] Compared with related technologies, the NSQ-based consumption rate limiting method, apparatus, device, and storage medium provided in this embodiment establishes a connection between the client and the server based on the client's initial configuration; obtains messages to be consumed pushed by the server based on the consumer configuration and stores the messages to be consumed in the internal queue corresponding to the consumer; obtains the preset consumption rate in the consumer configuration and extracts the messages to be consumed from the internal queue at the preset consumption rate. This solves the problem of reduced stability of the client and downstream services when the client has multiple asynchronous message consumptions.
Smart Images

Figure CN116455952B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of communication technology, and in particular to a consumption current limiting method, apparatus, device and storage medium based on NSQ. Background Technology
[0002] For large-scale software architectures involving numerous application domains, both decoupling business events between domains and shielding performance differences between different domains necessitate asynchronous processing of a large number of business events within each domain. Existing approaches mostly utilize mature distributed message queues to decouple message producers and consumers, smoothing out traffic spikes and dips.
[0003] NSQ, a distributed real-time messaging platform developed in Go, can be used for real-time messaging services in large-scale systems and can process hundreds of millions of messages per day. To cope with the large number of messages generated by different applications, NSQ also provides a flow control mechanism, which allows the server to push messages to clients for consumption based on the overall consumption capacity of the clients. However, when multiple asynchronous message consumption occurs on a client, it can lead to a decrease in the stability of the client and downstream services.
[0004] There is currently no effective solution to the problem that the stability of the client and downstream services is reduced when the client consumes multiple asynchronous messages. Summary of the Invention
[0005] This embodiment provides a consumption rate limiting method, apparatus, device, and storage medium based on NSQ to solve the problem that the stability of the client and downstream services is reduced when the client has multiple asynchronous message consumptions in related technologies.
[0006] Firstly, this embodiment provides a consumption rate limiting method based on NSQ, applicable to the client side, wherein the client includes several consumers, and the method includes:
[0007] Based on the initial configuration of the client, a connection is established between the client and the server;
[0008] Based on the consumer configuration, obtain the message to be consumed pushed by the server, and store the message to be consumed in the internal queue of the corresponding consumer.
[0009] Obtain the preset consumption rate in the consumer configuration, and extract the message to be consumed from the internal queue at the preset consumption rate.
[0010] In some embodiments, establishing a connection between the client and the server based on the client's initial configuration includes:
[0011] Based on the consumption theme in the initial configuration, obtain the address of the NSQ service corresponding to the server.
[0012] Based on the address of the NSQ service, a connection is established between each consumer and the server.
[0013] In some embodiments, obtaining the message to be consumed pushed by the server based on the consumer configuration includes:
[0014] The consumer configuration is set through the dynamic configuration center, and the consumer is initialized according to the consumer configuration;
[0015] The system listens for and retrieves messages to be consumed pushed by the server through the NSQ message queue; wherein the server pushes the messages to be consumed based on the consumer's response instruction.
[0016] In some embodiments, obtaining the preset consumption rate in the consumer configuration and retrieving the message to be consumed from the internal queue at the preset consumption rate includes:
[0017] Based on the consumer configuration, obtain the preset consumption rate of the consumer configuration;
[0018] The consumer uses a scheduling service to extract messages to be consumed from the internal queue at the preset consumption rate and submits them to the execution thread pool for processing.
[0019] In some embodiments, the method further includes:
[0020] When an error occurs while acquiring the message to be consumed, retrieve the backoff policy and preset number of retries from the consumer configuration;
[0021] Based on the backoff strategy, the retry interval for obtaining messages to be consumed is calculated, and the server is notified to retry pushing the messages to be consumed according to the preset number of retries and the retry interval.
[0022] In some embodiments, calculating the retry interval for obtaining messages to be consumed based on the backoff strategy includes:
[0023] Based on the backoff strategy, obtain the retry time interval of a fixed duration; or...
[0024] The retry interval is calculated based on the current number of retries and the exponential multiple in the backoff strategy.
[0025] In some embodiments, the method further includes:
[0026] The server is continuously notified to retry pushing the message to be consumed until the current number of retries reaches the preset number of retries, at which point the pushing stops and an alarm is triggered.
[0027] Secondly, this embodiment provides a consumption rate limiting device based on NSQ, applicable to the client side. The client includes several consumers, and the device includes: an initialization module, a message push module, and a consumption rate limiting module.
[0028] The initialization module is used to establish a connection between the client and the server based on the initial configuration of the client.
[0029] The message push module is used to obtain the message to be consumed pushed by the server based on the consumer configuration, and store the message to be consumed in the internal queue of the corresponding consumer.
[0030] The consumption rate limiting module is used to obtain the preset consumption rate in the consumer configuration and extract the message to be consumed from the internal queue at the preset consumption rate.
[0031] Thirdly, this embodiment provides a computer device including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the NSQ-based consumption throttling method described in the first aspect above.
[0032] Fourthly, this embodiment provides a storage medium storing a computer program that, when executed by a processor, implements the NSQ-based consumption limiting method described in the first aspect above.
[0033] Compared with related technologies, the NSQ-based consumption rate limiting method, apparatus, device, and storage medium provided in this embodiment establishes a connection between the client and the server based on the client's initial configuration; obtains messages to be consumed pushed by the server based on the consumer configuration and stores the messages to be consumed in the internal queue corresponding to the consumer; obtains the preset consumption rate in the consumer configuration and extracts the messages to be consumed from the internal queue at the preset consumption rate. This solves the problem of reduced stability of the client and downstream services when the client has multiple asynchronous message consumptions.
[0034] Details of one or more embodiments of this application are set forth in the following drawings and description to make other features, objects and advantages of this application more readily apparent. Attached Figure Description
[0035] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0036] Figure 1 This is a hardware structure block diagram of a terminal using an NSQ-based consumption rate limiting method in one embodiment;
[0037] Figure 2 This is a flowchart of a consumption rate limiting method based on NSQ in one embodiment;
[0038] Figure 3 This is a flowchart of a preferred embodiment of a consumption rate limiting method based on NSQ;
[0039] Figure 4 This is a schematic diagram illustrating the working principle of the consumer in a preferred embodiment;
[0040] Figure 5 This is a structural block diagram of a consumption current limiting device based on NSQ in one embodiment.
[0041] In the diagram: 102, processor; 104, memory; 106, transmission device; 108, input / output device; 10, initialization module; 20, message push module; 30, consumption rate limiting module. Detailed Implementation
[0042] To better understand the purpose, technical solution, and advantages of this application, the application is described and illustrated below in conjunction with the accompanying drawings and embodiments.
[0043] Unless otherwise defined, the technical or scientific terms used in this application shall have the general meaning as understood by one of ordinary skill in the art to which this application pertains. Words such as “a,” “an,” “an,” “the,” “the,” and “these,” used in this application, do not indicate quantitative limitation and may be singular or plural. The terms “comprising,” “including,” “having,” and any variations thereof used in this application are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or device that comprises a series of steps or modules (units) is not limited to the listed steps or modules (units) but may include steps or modules (units) not listed, or may include other steps or modules (units) inherent to such processes, methods, products, or devices. The terms “connected,” “linked,” and “coupled,” used in this application, are not limited to physical or mechanical connections but may include electrical connections, whether direct or indirect. The term “multiple” used in this application refers to two or more. The "and / or" operator describes the relationship between related objects, indicating that three relationships can exist. For example, "A and / or B" can represent three cases: A alone, A and B simultaneously, and B alone. Typically, the character " / " indicates that the objects before and after it are in an "or" relationship. The terms "first," "second," and "third," etc., used in this application are merely for distinguishing similar objects and do not represent a specific ordering of the objects.
[0044] The method embodiments provided in this example can be executed on a terminal, computer, or similar computing device. For example, it can run on a terminal. Figure 1 This is a hardware structure block diagram of the terminal using the NSQ-based consumption rate limiting method in this embodiment. For example... Figure 1 As shown, a terminal may include one or more ( Figure 1 Only one is shown in the diagram. A processor 102 and a memory 104 for storing data are also included. The processor 102 may be, but is not limited to, a microprocessor (MCU) or a programmable logic device (FPGA). The terminal may also include a transmission device 106 for communication functions and an input / output device 108. Those skilled in the art will understand that… Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the terminal described above. For example, the terminal may also include components that are larger than... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown are illustrated.
[0045] The memory 104 can be used to store computer programs, such as application software programs and modules, like the computer program corresponding to the NSQ-based consumption limiting method in this embodiment. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, thereby implementing the above-described method. The memory 104 may include high-speed random access memory and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to the terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0046] The transmission device 106 is used to receive or send data via a network. This network includes a wireless network provided by the terminal's communication provider. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 can be a Radio Frequency (RF) module used for wireless communication with the Internet.
[0047] For large-scale software architectures involving numerous application domains, both decoupling business events between domains and shielding performance differences between different domains necessitate asynchronous processing of a large number of business events within each domain. Existing approaches mostly utilize mature distributed message queues to decouple message producers and consumers, smoothing out traffic spikes and dips.
[0048] NSQ, a distributed real-time messaging platform developed in Go, can be used for real-time messaging services in large-scale systems and can process hundreds of millions of messages per day. To handle the large volume of messages generated by different applications, NSQ also provides a flow control mechanism. Based on the overall consumption capacity of the client, the server pushes messages to the client for consumption. As long as the client's device performance allows, the server will continue to push messages. However, the client's consumption performance depends not only on its own device performance but also on the downstream services and databases it relies on during consumption. This may lead to resource exhaustion of downstream services. Furthermore, if the client has multiple asynchronous message consumers or resource contention with other real-time RPC (Remote Procedure Call) calls, it will also affect the client's stability. Therefore, when a client has multiple asynchronous message consumers, it will lead to reduced stability of the client and downstream services.
[0049] To address the above issues, the following embodiments provide a consumption rate limiting method, apparatus, device, and storage medium based on NSQ, which enables each consumer on the client to temporarily store messages to be consumed pushed by the server through an internal queue, and to extract messages to be consumed from the internal queue at a preset consumption rate, thereby achieving consumption rate limiting on the client side.
[0050] This embodiment provides a consumption rate limiting method based on NSQ. This method is applicable to the client side, where the client includes several consumers. Figure 2 This is a flowchart of the method in this embodiment, as follows: Figure 2 As shown, the method includes the following steps:
[0051] Step S210: Based on the initial configuration of the client, establish a connection between the client and the server.
[0052] Specifically, the client configures the listening for asynchronous messages to obtain the initial client configuration, which includes, but is not limited to, the NSQ consumer topics to be listened to and the channel names of each consumer, for use in registering NSQ consumers on the client.
[0053] The NSQ Look Up service is used to locate the address of the corresponding NSQ queue based on the consumption topic of each consumer, thereby establishing a connection between each consumer in the client and the corresponding NSQ queue on the server, and thus establishing a connection between the client and the server. It should be noted that the aforementioned server and client include, but are not limited to, servers or systems.
[0054] Step S220: Based on the consumer configuration, obtain the messages to be consumed pushed by the server and store the messages to be consumed in the internal queue of the corresponding consumer.
[0055] Specifically, consumers are configured uniformly in the dynamic configuration center, and the dynamic configuration center supports dynamic adjustments to ensure real-time control over the configuration of each consumer and obtain the consumer configuration.
[0056] Consumer configuration includes, but is not limited to, preset consumption rate, preset retries, response command (rdy), backoff strategy, and number of consumer threads. The preset retries are the maximum number of retries preset by the server in case of consumption errors. The response command is an rdy response sent to the server when the consumer and the server's NSQ service have established a connection and are ready to receive messages. It includes an rdy_count representing the number of messages ready to be received, and is a rate-limiting method based on the overall consumption capacity of the client.
[0057] After establishing the connection between the server and the client, the client's consumer notifies the server to prepare. The server pushes the messages to be consumed to the client's consumer through the NSQ queue. Furthermore, the processing links of each consumer are configured independently and do not interfere with each other. Each consumer is configured with an internal queue to store the acquired messages to be consumed in the corresponding consumer's internal queue.
[0058] Step S230: Obtain the preset consumption rate in the consumer configuration, and extract messages to be consumed from the internal queue at the preset consumption rate.
[0059] Specifically, the preset consumption rate in the consumer configuration is the preset rate at which the consumer extracts messages to be consumed from the internal queue at a uniform speed. This rate can be adjusted based on the downstream service capacity and the number of database connections. Furthermore, the time period for uniform speed scheduling is calculated based on the preset consumption rate. The calculated period is used for timed and speed-based scheduling, that is, the consumer extracts messages to be consumed from the internal queue at a fixed time and speed.
[0060] In the above steps, each consumer on the client side temporarily stores messages to be consumed pushed by the server through an internal queue, and extracts messages to be consumed from the internal queue at a preset consumption rate. Compared with the rate limiting mechanism of the original NSQ service based on the overall consumption capacity of the client in the existing technology, this embodiment implements a producer-consumer mode on the client side based on the NSQ service. It can temporarily store messages to be consumed through an internal queue and consume them at a uniform rate according to the configured preset consumption rate. There will be no pressure on downstream services due to a short-term increase in consumption capacity. Moreover, the consumption speed can be conveniently controlled in the consumer configuration based on the downstream service capacity and the number of database connections.
[0061] In some embodiments, step S210 above establishes a connection between the client and the server based on the client's initial configuration, which can be achieved through the following steps:
[0062] Step S211: Based on the consumption topic in the initial configuration, obtain the address of the corresponding NSQ service on the server side.
[0063] Step S212: Based on the address of the NSQ service, establish a connection between each consumer and the server.
[0064] Specifically, the initial configuration includes, but is not limited to, the NSQ consumer topics to be listened to and the channel names for each consumer, used for NSQ consumer registration on the client side. Based on each consumer's consumer topic, the NSQ Look-Up service searches for the corresponding NSQ queue. The NSQ service returns a list of NSQ queues for the corresponding consumer topic. Then, the consumer only needs to iterate through the producer list in the returned JSON string, concatenate the broadcast_address and tcp_port or http_port to obtain the NSQ queue address to establish a connection with.
[0065] Consumers will establish connections with these NSQ queues one by one. After the NSQ queue receives the message for the corresponding consumption topic written by the server, it will push the message to be consumed to the consumer that has established a connection.
[0066] In this embodiment, a connection is established between the client and the server so that the server can push messages to be consumed to the client's consumer via the NSQ queue, thereby decoupling the message producer and consumer and smoothing out traffic peaks and valleys.
[0067] In some embodiments, the process of obtaining the message to be consumed pushed by the server based on the consumer configuration in step S220 above can be implemented through the following steps:
[0068] Step S221: Configure the consumer configuration through the dynamic configuration center and initialize the consumer according to the consumer configuration.
[0069] Specifically, consumer configuration includes, but is not limited to, preset consumption rate, preset retries, response command (rdy), backoff strategy, and number of consumption threads. The preset retries are the maximum number of retries preset for the server in case of consumption errors. The response command is an rdy response sent to the server when the consumer and server's NSQ service have established a connection and are ready to receive messages. It includes an rdy_count representing the number of messages ready to be received and is a rate-limiting method based on the overall consumption capacity of the client. The backoff strategy is the strategy for calculating the server's retry interval in case of consumption errors. The number of consumption threads is the number of threads that extract messages to be consumed and process them in the thread pool.
[0070] Consumers are configured uniformly in a dynamic configuration center, which supports dynamic adjustments to ensure real-time control over the configuration of each consumer, thereby obtaining the consumer configuration for initialization. The dynamic configuration center can be implemented through the Apollo configuration center's dynamic activation mechanism.
[0071] After the client starts, it loads the consumer configurations for each consumer sequentially and runs a message listening task on this system. Each consumer includes several components: an internal queue, a scheduling service, a retry mechanism, and message acknowledgment, forming a complete message consumption processing chain. Different consumers do not interfere with each other and each performs consumption rate limiting independently. The internal queue component receives messages to be consumed, the scheduling service component schedules messages to be consumed at a uniform rate, the retry mechanism component handles post-processing of consumption exceptions and executes backoff strategies, and the message acknowledgment component responds to messages. Initially, the consumer acknowledgment mode is set to manual acknowledgment; when there are no consumption exceptions, the message acknowledgment component provides a unified acknowledgment.
[0072] Step S222 involves listening to and obtaining messages to be consumed pushed by the server through the NSQ message queue; wherein the server pushes the messages to be consumed based on the consumer's response instructions.
[0073] Specifically, by monitoring the pending consumption information of the corresponding consumption topic, the server pushes the messages to be consumed to the internal queue of the client's consumer via the NSQ queue. When consuming NSQ messages, the server pushes the messages to be consumed to the consumer based on the consumer's acknowledgment instructions (which control the number of messages the server pushes to the consumer each time) and the message ack status.
[0074] In this embodiment, the consumer configuration can be dynamically adjusted in the dynamic configuration center, and the adjustment can be made at any time and take effect immediately. Compared with the prior art, which requires restarting the client to adjust the consumer configuration, this embodiment does not require code adjustment or server restart.
[0075] In some embodiments, the process of obtaining the preset consumption rate in the consumer configuration and retrieving messages to be consumed from the internal queue at the preset consumption rate in step S230 can be implemented through the following steps:
[0076] Step S231: Based on the consumer configuration, obtain the preset consumption rate of the consumer configuration.
[0077] Step S232: Through the scheduling service in the consumer, messages to be consumed are extracted from the internal queue at a preset consumption rate and submitted to the execution thread pool for processing.
[0078] Specifically, a preset consumption rate is obtained from the consumer configuration, which can be adjusted based on the downstream service capacity and the number of database connections.
[0079] Furthermore, based on the preset consumption rate, the time period for uniform scheduling is calculated. Scheduling is performed at a fixed time and speed according to the calculated period, meaning messages to be consumed are retrieved from the internal queue at a fixed time and speed. The scheduling service component, obtained after loading the consumer configuration, performs the scheduling of messages in the internal queue, retrieving messages to be consumed at the preset consumption rate and submitting them to the execution thread pool for processing.
[0080] Through the scheduling service of each consumer in this embodiment, the information to be consumed can be extracted from the internal queue at a uniform speed according to the configured preset consumption rate. There will be no problem of increased consumption capacity in a short period of time putting pressure on downstream services, thus providing stable rate limiting capabilities.
[0081] In some embodiments, when a consumer makes an abnormal purchase, the method further includes an avoidance strategy, specifically configured in the consumer settings, to address scenarios involving abnormal purchases. The method includes the following steps:
[0082] When an error occurs while retrieving messages to be consumed, the backoff policy and preset retry count in the consumer configuration are retrieved. Based on the backoff policy, the retry interval for retrieving messages to be consumed is calculated, and the server is notified to retry pushing messages to be consumed according to the preset retry count and retry interval.
[0083] Specifically, the backoff strategy and preset retry count are obtained from the consumer configuration. The backoff strategy is the strategy for calculating the server retry time interval when a consumption anomaly occurs; the preset retry count is the maximum number of retries preset by the server when a consumption anomaly occurs.
[0084] The retry mechanism component in the consumer performs post-consumption exception handling and backoff strategy execution. Based on the backoff strategy, the server retry interval is calculated, and the server is notified to retry pushing messages to be consumed.
[0085] Furthermore, the retry interval of the backoff strategy includes both fixed and exponential intervals, which can be adjusted according to specific business characteristics. This includes the following steps:
[0086] Based on the backoff strategy, obtain a fixed retry interval; or, based on the current number of retries and the exponential multiple in the backoff strategy, calculate the retry interval.
[0087] Specifically, the fixed-duration retry interval is used to retry after each consumption failure at a configured fixed interval. The exponential retry interval is used to calculate the next retry interval based on the current number of retries and the exponential multiple in the backoff strategy after a consumption failure.
[0088] Specifically, based on the initial values of the initial interval period (in seconds) and the maximum interval time (in seconds) given in the backoff strategy, and combined with the current number of retries (attempts) and the exponential multiplier, the time interval for the next retry is calculated. The following is an example of calculating an exponential retry time interval:
[0089] f(attempts, interval, multiplier) = interval * multiplier^(attempts-1). Assuming the initial interval period is 60 seconds, the maximum interval time is 21600 seconds, the exponent multiplier is 2, the second retry interval is 120 seconds, the third retry interval is 240 seconds, and so on.
[0090] Furthermore, the above retry mechanism is also limited by a preset number of retries, specifically including the following steps:
[0091] The server is continuously notified to retry pushing messages to be consumed until the current number of retries reaches the preset number of retries, at which point the pushing stops and an alarm is triggered.
[0092] Specifically, when the server is notified to push retry messages based on the fixed duration and exponential retry interval in the backoff strategy, it is limited by the preset number of retries. When the current number of retries reaches the preset number of retries, the push is stopped and the business delegation interface is called back for failure handling, as well as alarm handling for the user.
[0093] This embodiment provides a method for handling consumption anomalies through a consumer retry mechanism component, which supports limiting the number of retries and selecting the retry interval in the backoff strategy. It can perform automatic retry and backoff strategy processing operations for abnormal scenarios.
[0094] The present embodiment will now be described and illustrated through preferred embodiments.
[0095] Figure 3 This is a flowchart of the NSQ-based consumption throttling method in this preferred embodiment, as shown below. Figure 3 As shown, the method includes the following steps:
[0096] Step S310: Configure the asynchronous message listener to obtain the initial configuration of the client and register the NSQ consumer.
[0097] Step S320: Configure the consumer through the dynamic configuration center, initialize the consumer, and establish a connection with the server based on the consumer theme in the initial configuration.
[0098] Step S330: Listen to and obtain the messages to be consumed pushed by the server through the NSQ message queue, and temporarily store them in the consumer's internal queue; wherein, the server pushes the messages to be consumed based on the consumer's response instructions.
[0099] Step S340: Based on the preset consumption rate in the consumer configuration, the consumer's scheduling service extracts messages to be consumed from the internal queue at a uniform speed for processing.
[0100] Step S350: When an error occurs in obtaining a message to be consumed, the retry interval for obtaining the message to be consumed is calculated based on the backoff policy and preset retry count in the consumer configuration, and the server is notified to retry pushing the message to be consumed within the retry interval.
[0101] Step S360: Stop pushing and issue an alarm when the current number of retries reaches the preset number of retries.
[0102] It should be noted that the steps shown in the above process or the flowchart in the accompanying figures can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here. For example, steps S350 and S360 can be executed when the consumer receives an exception for a message to be consumed, and can occur at any point during the consumer's consumption process.
[0103] Figure 4 This is a schematic diagram illustrating the working principle of the consumer in this preferred embodiment, such as... Figure 4 As shown, the server writes messages to be consumed to the NSQ message queue via the NSQ service and pushes them to the consumer's internal queue. In the consumer, the scheduling service retrieves messages to be consumed from the internal queue and submits them to the execution thread pool for multi-threaded processing, determining whether consumption was successful. If consumption is successful, an ACK message is sent to the NSQ service; if consumption fails, it first checks if the current retries have reached the preset retries. If the preset retries have been reached, retries are stopped and an alarm is recorded; if the preset retries have not been reached, the retry interval is calculated based on the backoff strategy, and the server is notified again to push messages to be consumed via the NSQ service.
[0104] This preferred embodiment implements a producer-consumer model on the client side based on the NSQ service. Messages to be consumed are temporarily stored in an internal queue, and consumption is performed at a uniform rate according to a pre-configured consumption rate. This avoids putting pressure on downstream services due to short-term increases in consumption capacity. Furthermore, consumer configurations are dynamically adjusted in a dynamic configuration center, allowing for immediate adjustments and effects. Compared to existing technologies that require restarting the client to adjust consumer configurations, this embodiment requires no code modifications or server restarts. Additionally, it provides methods for handling consumption anomalies, including limiting the number of retries and selecting the retry interval in the backoff strategy. Automatic retries and backoff strategies are implemented for abnormal scenarios.
[0105] This embodiment also provides a NSQ-based consumption current limiting device, which is used to implement the above embodiments and preferred embodiments; details already described will not be repeated. The terms "module," "unit," "subunit," etc., used below refer to combinations of software and / or hardware that implement a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0106] Figure 5 This is a structural block diagram of the NSQ-based consumer current limiting device in this embodiment, as shown below. Figure 5 As shown, the device is applicable to the client side, and the client includes several consumers. The device includes: an initialization module 10, a message push module 20, and a consumption rate limiting module 30.
[0107] Initialization module 10 is used to establish a connection between the client and the server based on the initial configuration of the client.
[0108] The message push module 20 is used to obtain messages to be consumed pushed by the server based on the consumer configuration, and store the messages to be consumed in the internal queue of the corresponding consumer.
[0109] The consumption rate limiting module 30 is used to obtain the preset consumption rate in the consumer configuration and extract messages to be consumed from the internal queue at the preset consumption rate.
[0110] The device provided in this embodiment allows each consumer on the client to temporarily store messages to be consumed pushed by the server through an internal queue, and to extract messages to be consumed from the internal queue at a preset consumption rate. Compared with the rate limiting mechanism of the original NSQ service based on the overall consumption capacity of the client in the prior art, this embodiment implements a producer-consumer mode on the client based on the NSQ service. It can temporarily store messages to be consumed through an internal queue and consume them at a uniform rate according to the configured preset consumption rate. There will be no pressure on downstream services due to a short-term increase in consumption capacity, and the consumption speed can be conveniently controlled in the consumer configuration based on the downstream service capacity and the number of database connections.
[0111] It should be noted that the above modules can be functional modules or program modules, and can be implemented through software or hardware. For modules implemented through hardware, the above modules can reside in the same processor; or the above modules can be located in different processors in any combination.
[0112] This embodiment also provides a computer device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0113] Optionally, the computer device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0114] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementations, and will not be repeated in this embodiment.
[0115] Furthermore, in conjunction with the NSQ-based consumption rate limiting method provided in the above embodiments, this embodiment can also provide a storage medium for implementation. This storage medium stores a computer program; when executed by a processor, the computer program implements any of the NSQ-based consumption rate limiting methods described in the above embodiments.
[0116] It should be understood that the specific embodiments described herein are merely illustrative of the application and not intended to limit it. All other embodiments derived by those skilled in the art based on the embodiments provided in this application without inventive effort are within the scope of protection of this application.
[0117] Obviously, the accompanying drawings are merely some examples or embodiments of this application. Those skilled in the art can apply this application to other similar situations based on these drawings without any creative effort. Furthermore, it is understood that although the work done in this development process may be complex and lengthy, for those skilled in the art, certain design, manufacturing, or production modifications made based on the technical content disclosed in this application are merely conventional technical means and should not be considered as insufficient disclosure of this application.
[0118] The term "embodiment" in this application refers to a specific feature, structure, or characteristic described in connection with an embodiment that may be included in at least one embodiment of this application. The appearance of this phrase in various places in the specification does not necessarily imply the same embodiment, nor does it imply that it is mutually exclusive with or independent of other embodiments. It will be clearly or implicitly understood by those skilled in the art that the embodiments described in this application may be combined with other embodiments without conflict.
[0119] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of patent protection. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the appended claims.
Claims
1. A consumption throttling method based on NSQ, characterized in that, Applicable to the client side, wherein the client includes several consumers, the method includes: Based on the initial configuration of the client, a connection is established between the client and the server; Based on the consumer configuration, obtain the message to be consumed pushed by the server, and store the message to be consumed in the internal queue of the corresponding consumer. Obtain the preset consumption rate in the consumer configuration, and retrieve the messages to be consumed from the internal queue at the preset consumption rate; wherein, the process includes: Based on the consumer configuration, obtain the preset consumption rate of the consumer configuration; The consumer uses a scheduling service to extract messages to be consumed from the internal queue at the preset consumption rate and submits them to the execution thread pool for processing.
2. The consumption throttling method based on NSQ according to claim 1, characterized in that, The step of establishing a connection between the client and the server based on the initial configuration of the client includes: Based on the consumption theme in the initial configuration, obtain the address of the NSQ service corresponding to the server. Based on the address of the NSQ service, a connection is established between each consumer and the server.
3. The consumption throttling method based on NSQ according to claim 1, characterized in that, The step of obtaining the message to be consumed pushed by the server based on the consumer configuration includes: The consumer configuration is set through the dynamic configuration center, and the consumer is initialized according to the consumer configuration; The server listens for and retrieves messages to be consumed pushed by the server through the NSQ message queue; the server pushes the messages to be consumed based on the consumer's response instruction.
4. The consumption throttling method based on NSQ according to claim 1, characterized in that, Also includes: When an error occurs while acquiring the message to be consumed, retrieve the backoff policy and preset number of retries from the consumer configuration; Based on the backoff strategy, the retry interval for obtaining messages to be consumed is calculated, and the server is notified to retry pushing the messages to be consumed according to the preset number of retries and the retry interval.
5. The consumption throttling method based on NSQ according to claim 4, characterized in that, The calculation of the retry interval for obtaining messages to be consumed based on the backoff strategy includes: Based on the backoff strategy, a fixed retry interval is obtained; or... The retry interval is calculated based on the current number of retries and the exponential multiple in the backoff strategy.
6. The consumption throttling method based on NSQ according to claim 4, characterized in that, Also includes: The server is continuously notified to retry pushing the message to be consumed until the current number of retries reaches the preset number of retries, at which point the pushing stops and an alarm is triggered.
7. A consumption current limiting device based on NSQ, characterized in that, Applicable to the client side, the client includes several consumers, and the device includes: an initialization module, a message push module, and a consumption rate limiting module; The initialization module is used to establish a connection between the client and the server based on the initial configuration of the client. The message push module is used to obtain the message to be consumed pushed by the server based on the consumer configuration, and store the message to be consumed in the internal queue of the corresponding consumer. The consumption rate limiting module is used to obtain a preset consumption rate in the consumer configuration, and to extract the messages to be consumed from the internal queue at the preset consumption rate; wherein, it includes: Based on the consumer configuration, obtain the preset consumption rate of the consumer configuration; The consumer uses a scheduling service to extract messages to be consumed from the internal queue at the preset consumption rate and submits them to the execution thread pool for processing.
8. A computer device, comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to run the computer program to perform the NSQ-based consumption throttling method according to any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the steps of the NSQ-based consumption throttling method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Message queue exception handling method and device, computer equipment and storage medium
CN114237823A
Real-time protection system and method for distributed middleware and computer readable storage medium
CN115460086A