Load balancing method and apparatus for long connection, electronic device, and storage medium
By obtaining server instance runtime load information through hash calculation and load balancer, load balancing information is constructed, and clients are instructed to establish long-lived connections with the most load-balanced servers. This solves the problem of uneven load distribution across long-lived connections and improves the stability and load balancing effect of the distributed system.
Patent Information
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- BEIJING ZITIAO NETWORK TECH CO LTD
- Filing Date
- 2025-05-30
- Publication Date
- 2026-05-07
AI Technical Summary
In existing technologies, load balancing of long-lived connections is difficult to guarantee fairness, which leads to uneven load distribution when the distributed system is undergoing rolling upgrades and service expansion, affecting system stability.
By using hash calculations and load balancers, the system obtains the runtime load information of server instances in the distributed system, uses hash rings to construct load balancing information, and instructs clients to establish long-lived connections with the most load-balanced server instances to avoid frequent reconnections and improve the fairness of load balancing.
It achieves load balancing for long-lived connections, improving the stability and load balancing effect of distributed systems, especially in high-concurrency situations, avoiding load imbalance and instability in business data transmission.
Smart Images

Figure CN2025098697_07052026_PF_FP_ABST
Abstract
Description
Load balancing methods, devices, electronic equipment, and storage media for long-lived connections
[0001] Cross-reference to related applications
[0002] This application claims priority to Chinese Patent Application No. 202411545640.0, entitled "Load Balancing Method, Apparatus, Electronic Device and Storage Medium for Long Connections", filed on October 31, 2024, which is incorporated herein by reference in its entirety. Technical Field
[0003] This disclosure relates to the field of cloud computing technology, and in particular to a load balancing method, apparatus, electronic device, and storage medium for long-lived connections. Background Technology
[0004] Currently, in distributed systems, persistent connections established between clients and servers are called long-lived connections. These connections remain open for a period of time to achieve stable data exchange, offering higher communication efficiency compared to short-lived connections that require frequent connection and disconnection. Furthermore, functional services in a distributed system are deployed as service instances. Different clients establish long-lived connections with corresponding service instances based on load balancing technology to invoke the corresponding functional services. Summary of the Invention
[0005] This disclosure provides a load balancing method, apparatus, electronic device, and storage medium for long-lived connections.
[0006] In a first aspect, embodiments of this disclosure provide a load balancing method for long-lived connections, including:
[0007] In response to receiving a short connection request from a target client, the system obtains the runtime load information of multiple server instances providing the target service in a distributed system, wherein the target service is invoked via a long connection; based on the runtime load information of the server instances, load balancing information is obtained, wherein the load balancing information is used to at least characterize the target server instance among the multiple server instances; the load balancing information is sent to the target client to instruct the target client to establish a long connection with the target server instance for the target service.
[0008] Secondly, embodiments of this disclosure provide a load balancing device for long-lived connections, comprising:
[0009] The response module is used to respond to short connection requests sent by the target client and obtain the running load information of multiple server instances providing the target service in the distributed system, wherein the target service is invoked through a long connection;
[0010] The processing module is used to obtain load balancing information based on the running load information of the server instance, wherein the load balancing information is used to characterize at least the target server instance among the plurality of server instances.
[0011] The output module is used to send the load balancing information to the target client to instruct the target client to establish a long connection with the target server instance for the target service.
[0012] Thirdly, embodiments of this disclosure provide an electronic device, including: a processor and a memory;
[0013] The memory stores computer-executed instructions;
[0014] The processor executes computer execution instructions stored in the memory, causing the at least one processor to perform the load balancing method for long connections as described in the first aspect and various possible designs of the first aspect.
[0015] Fourthly, embodiments of this disclosure provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the load balancing method for long connections as described in the first aspect and various possible designs of the first aspect.
[0016] Fifthly, embodiments of this disclosure provide a computer program product, including a computer program that, when executed by a processor, implements the load balancing method for long connections as described in the first aspect and various possible designs of the first aspect. Attached Figure Description
[0017] To more clearly illustrate the technical solutions in the embodiments of this disclosure or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 is an application scenario diagram of the load balancing method for long connections provided in the embodiments of this disclosure;
[0019] Figure 2 is a schematic flowchart of a load balancing method for long connections provided in an embodiment of this disclosure;
[0020] Figure 3 is a flowchart of the specific implementation of step S101 in the embodiment shown in Figure 2;
[0021] Figure 4 is a schematic diagram of a distributed triggering reconnection process provided in an embodiment of this disclosure;
[0022] Figure 5 is a flowchart of the specific implementation of step S102 in the embodiment shown in Figure 2;
[0023] Figure 6 is a schematic diagram of the load balancing method for long connections provided in an embodiment of this disclosure.
[0024] Figure 7 is a schematic diagram of a hash ring provided in an embodiment of this disclosure;
[0025] Figure 8 is a flowchart of a specific implementation of step S204 in the embodiment shown in Figure 6;
[0026] Figure 9 is a flowchart of another specific implementation of step S204 in the embodiment shown in Figure 6;
[0027] Figure 10 is a structural block diagram of a load balancing device for long connections provided in an embodiment of this disclosure;
[0028] Figure 11 is a schematic diagram of the structure of an electronic device provided in an embodiment of this disclosure;
[0029] Figure 12 is a schematic diagram of the hardware structure of the electronic device provided in an embodiment of this disclosure. Detailed Implementation
[0030] In existing technologies, the long duration of long-lived connections makes it impossible to guarantee the fairness of load balancing through conventional load balancing techniques, thus affecting the operational stability of distributed systems.
[0031] To make the objectives, technical solutions, and advantages of the embodiments of this disclosure clearer, the technical solutions of the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this disclosure, and not all embodiments. Based on the embodiments of this disclosure, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this disclosure.
[0032] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this disclosure are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, use and processing of the relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and corresponding operation portals are provided for users to choose to authorize or refuse.
[0033] The application scenarios of the embodiments of this disclosure are explained below:
[0034] Figure 1 illustrates an application scenario of the load balancing method for long connections provided in this embodiment of the present disclosure. This load balancing method for long connections can be applied to distributed service application scenarios, and more specifically, to distributed systems. The execution entity in this embodiment can be an entity or virtual execution device with computing capabilities within the distributed system, such as virtual or physical terminal devices, network devices, servers, etc. In some embodiments, the aforementioned execution device can implement the load balancing method for long connections provided in this embodiment by running various computer-executable instructions or computer programs. For example, computer-executable instructions can be program-level commands, machine instructions, or software instructions. Computer programs can be native programs or software modules in an operating system; they can be local applications, i.e., programs that need to be installed in the operating system to run; or they can be small programs embedded in any APP, i.e., programs that run based on a browser environment. In summary, the aforementioned computer-executable instructions can be any form of instruction, and the aforementioned computer programs can be any form of application, module, or plugin. The specific implementation can be configured as needed. Furthermore, in implementing the load balancing method for long connections provided in this embodiment, the execution device can execute the method by running locally executed computer-executable instructions or computer programs, or by calling computer-executable instructions or computer programs located in an external server. In some embodiments, the server can be an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud storage, cloud communication, cloud databases, cloud computing, cloud functions, network services, middleware services, domain name services, security services, content delivery networks (CDNs), and big data and artificial intelligence platforms. The cloud services can be interactive processing services that the execution device can call.
[0035] Referring to Figure 1, in a distributed system, the deployment of a target service is based on service instances, such as service instances R1, R2, and R3 shown in the figure. This target service, for example, provides real-time communication functions (e.g., voice calls, video conferencing). Clients on the target terminal device side establish long-lived connections with these service instances. For example, after establishing a long-lived connection with service instance R2 as shown in the figure, service instance R2 forwards and processes communication data, thereby enabling real-time communication functions such as voice calls and video conferencing with other terminal devices. In this process, the service instances need to provide services to a large number of terminal devices, meaning they need to establish long-lived connections with a large number of terminal clients and transmit data. Therefore, load balancing technology is needed to adjust the load so that each service instance can handle service requests evenly, avoiding problems such as excessive or insufficient load on a single service instance, which could affect the stability and efficiency of the distributed system.
[0036] In existing technologies, persistent connections established between clients and servers are called long-lived connections. These connections remain open for a period of time to achieve stable data exchange, offering higher communication efficiency compared to short connections that require frequent connection and disconnection. However, because long-lived connections are established for a longer period, load balancing only occurs when the connection is created, unlike short connections which perform load balancing with each request. This makes it more difficult to guarantee the fairness of load balancing. Consequently, in scenarios such as rolling server upgrades and service expansion, some service instances accumulate a large number of connections, while others have only a few or no connections, resulting in uneven load distribution. On the other hand, businesses that require long-lived connections typically have high real-time requirements for data transmission, thus needing to avoid frequent interruptions. For these reasons, existing solutions struggle to achieve load balancing for long-lived connections, impacting the stability of distributed systems.
[0037] This disclosure provides a load balancing method for long-lived connections to solve the above-mentioned problems.
[0038] Referring to Figure 2, Figure 2 is a schematic flowchart of a load balancing method for long-lived connections provided in an embodiment of this disclosure. The method of this embodiment can be applied to computing devices in a distributed network. This load balancing method for long-lived connections includes:
[0039] Step S101: In response to receiving a short connection request from the target client, obtain the running load information of multiple server instances providing the target service in the distributed system, and call the target service through a long connection.
[0040] For example, referring to the application scenario diagram shown in Figure 1, the execution entity of the method provided in this embodiment is, for example, a load balancer deployed in a distributed system. This load balancer can be deployed as a service within the distributed system. When a target client needs to call a target service, it sends a call request for the target service to the load balancer. This call request is a short-connection request. The load balancer then responds to the short-connection request, determines a load balancing strategy, and sends an instruction to the target client to instruct the target client to establish a long-lived connection with the load balancing result, i.e., the target service instance, to implement the target service. The target service may be, for example, a service that requires a long-lived connection, such as voice calls or video conferencing.
[0041] Specifically, firstly, after receiving a short-connection request from the target client, the load balancer obtains the server instances in the distributed system that provide the target service. For example, if the target service is a video communication service, multiple server instances capable of providing this service will be deployed in the distributed system. Then, by accessing each server instance corresponding to the target service, the load information for each server instance is obtained. Alternatively, the load information can be obtained by accessing an additionally deployed status server used to detect and store the load information for each server instance. The load information characterizes the workload and operating status of the server instances. The workload includes, for example, the current number of connections to the server instance and the percentage of current long connections to the maximum number of connections. The operating status indicates whether the server instance is currently running normally. The operating status can include values such as 0, 1, and 2, where 0 indicates the server instance is disconnected, 1 indicates the server instance is fully loaded (cannot create new connections with clients), and 2 indicates the server instance is not fully loaded (can create new connections with clients).
[0042] In one possible implementation, as shown in Figure 3, the specific implementation of step S101 includes:
[0043] Step S1011: In response to receiving a short connection request sent by the target client, obtain the client identifier of the target client;
[0044] Step S1012: Perform hash calculation based on the client identifier of the target client to obtain the hash time point within the target period;
[0045] Step S1013: Perform hash calculation based on the current system time to obtain the hash time range within the target period;
[0046] Step S1014: If the hash time point falls within the hash time range, obtain the running load information of multiple server instances providing the target service in the distributed system.
[0047] For example, after establishing a long connection between a client and a service instance, to ensure real-time load balancing, the client needs to actively reconnect, i.e., disconnect the current long connection and recreate a long connection that better meets the load balancing requirements. However, when multiple clients reconnect simultaneously or within a short period, a "herding effect" occurs, causing a large number of long connections to be created on the same or a few service instances, resulting in load imbalance. In this embodiment, after receiving a short connection request from the target client, the load balancer first obtains the client identifier of the target client and performs a hash calculation based on the client identifier to obtain a hash time point within the target period, which can be called the first hash time point. For example, if the target period is 600 seconds, the first hash time point within the target period is a value between 0 and 599. The process of performing a hash calculation on an identifier to obtain a value within a fixed range is existing technology and will not be described further here. Next, the current system time is read and hashed to obtain another hash time point within the target period, which can be called the second hash time point. Then, a time range, i.e., the hash time range, can be determined with this second hash time point as the center, start point, or end point, and a preset time length. Finally, it is compared whether the first hash time point falls within the hash time range. If it falls within the hash time range, the running load information of multiple server instances providing the target service in the distributed system is obtained, and the subsequent steps of instructing the target client to establish (rebuild) a long connection with the target server instance are executed; if it does not fall within the hash time range, the above short connection request is not responded to.
[0048] Figure 4 is a schematic diagram of a distributed triggering reconnection process provided in an embodiment of this disclosure. The process will be further described below with reference to Figure 4. As shown in Figure 4, exemplarily, after receiving a short connection request sent by client C1, a hash calculation is performed based on the client identifier of client C1 (e.g., C1), i.e., Hash(C1), to obtain the corresponding hash time point t1. Through the above hash calculation algorithm, it can be guaranteed that the hash time point t1 is within [T1, T2] (i.e., the target period). Then, based on the current system time Sys_t1, a hash calculation is performed, i.e., Hash(Sys_t1), to obtain the corresponding hash time point t2. Then, exemplarily, with hash time point t2 as the center, the range is expanded by 5 seconds before and after to obtain the hash time range [t2-5, t2+5]. Finally, it is determined whether the hash time point t1 falls within the hash time range [t2-5, t2+5]. If it does not fall within this range, no response is given to the short connection request. Upon receiving the next short-connection request, the system recalculates the hash time point t3 and the corresponding hash time range [t3-5, t3+5] based on the current system time Sys_t1. It then checks again whether the hash time point t1 falls within this range. This process continues until a hash time point falls within the range. At this point, the system retrieves the runtime load information of multiple server instances providing the target service in the distributed system, thus establishing a long-lived connection between the target client and the corresponding target server instance. For example, as shown in the diagram, when the system time is t3, the hash time point t1 falls within the hash time range [t3-5, t3+5].
[0049] In this embodiment, by utilizing the randomness of hash calculation, the triggering time for each client to create a long connection can be evenly distributed within a time period, thereby avoiding the problem of load imbalance when multiple clients request reconnection at the same time and improving system stability.
[0050] Step S102: Obtain load balancing information based on the running load information of the server instance. The load balancing information is used to characterize the target server instance among multiple server instances.
[0051] For example, the load balancer then obtains load balancing information based on the operating load information of the server instances. In one possible implementation, the load balancing information includes the instance identifier of the target server instance, such as the network address information of the target server instance, to indicate the target server instance. This load balancing information is determined by the load balancer after calculation based on the operating load information of each server instance. Specifically, for example, a server instance that is currently not operating at full load is randomly selected as the target server instance; or, for another example, the server instance with the fewest long connections is determined as the target server instance, thereby generating the load balancing information.
[0052] In one possible implementation, as shown in Figure 5, step S102 is specifically implemented as follows:
[0053] Step S1021: Obtain at least two candidate server instances corresponding to the target client according to the hash algorithm, and determine the target server instance from the at least two candidate server instances in combination with the running load information;
[0054] Step S1022: Generate load balancing information based on the instance identifier of the target server instance.
[0055] For example, after receiving a short connection request from a target client, the load balancer uses this request to hash the client's identifier. The client identifier is then randomly mapped to one of multiple server instances. Next, at least one server instance near the targeted server instance is selected as a pool of candidate server instances. "Nearby" here refers to a short communication distance between the physical devices hosting the server instances, such as being located in the same data center. Alternatively, it could refer to adjacent instance identifiers. For example, if the targeted server instance has an instance identifier of s_01, then server instances with identifiers of s_02 and s_03 are also selected as candidate server instances along with s_01. Then, the load balancing information of each candidate server instance is obtained, such as the number of long connections. For instance, the candidate server instance with the fewest long connections is selected as the target server instance. Finally, load balancing information is generated based on the instance identifier of the target server instance.
[0056] Step S103: Send load balancing information to the target client to instruct the target client to establish a long connection with the target server instance for the target service.
[0057] For example, after determining load balancing information, the load balancer sends this information back to the target client to establish a long-lived connection between the target client and the target server instance indicated by the load balancing information. In one possible implementation, the load balancer sends a connection instruction containing the load balancing information to the target client. Upon receiving the connection instruction, the target client creates a corresponding long-lived connection with the target server instance using the load balancing information. The connection instruction may also include other instructions for creating the long-lived connection, which can be configured according to business needs and will not be elaborated here. Therefore, by responding to the short-connection request sent by the target client, determining the load balancing information representing the target server instance, and then returning the load balancing information to the target client to instruct the target client to establish a load-balanced long-lived connection with the target server instance, the fairness and effectiveness of load balancing are improved.
[0058] Furthermore, in one possible implementation, after step S102 is completed and the load balancing information is obtained, to avoid excessively frequent reconnections of long-lived connections, the operating load of the target server instance determined by the load balancing information can be compared with the operating load of the current server instance currently connected to by the target client, and combined with the global average operating load. Reconnection is only performed under certain conditions, thereby ensuring the operational stability of services using long-lived connections while maintaining load balancing to a certain extent. Specifically, before step S103, the following steps are also included:
[0059] Step S103A: Obtain the fifth load of the first server instance currently establishing a long connection with the target client, and the sixth load of the target server instance indicated by the load balancing information. The fifth load represents the number of long connections or long connection load of the first server instance currently establishing a long connection with the target client, and the sixth load represents the number of long connections or long connection load of the target server instance. The long connection load is the ratio of the current number of long connections to the maximum number of long connections. The following example illustrates the case of the number of long connections.
[0060] Accordingly, the specific implementation method of step S103 is as follows:
[0061] Step S103B: If the fifth load exceeds the third tolerance of the average load and the sixth load does not exceed the pre-deducted average number of long connections, then send load balancing information to the target client; or, if the sixth load does not exceed the fourth tolerance of the average load and the sixth load exceeds the pre-increased average number of long connections, then send load balancing information to the target client.
[0062] For example, the average load is the average number of long connections or the average long connection load across all service instances corresponding to the target service. Tolerance can be a percentage value; for example, the third tolerance is an upward tolerance, such as 110%; and the fourth tolerance is a downward tolerance, such as 90%. Pre-deducting the average long connection count means adding 1 to the average connection count; for example, if the average connection count is 100, then the pre-deducted average long connection count is 99 (i.e., 100-1). Pre-increasing the average long connection count means adding 1 to the average connection count; for example, if the average connection count is 100, then the pre-increasing average long connection count is 101 (i.e., 100+1).
[0063] Based on the above description, in one possible scenario, the fifth load exceeds the third tolerance level of the average load, for example, the fifth load exceeds 110% of the average load, meaning the fifth load is greater than 110% of the average load. Simultaneously, the sixth load does not exceed the pre-deducted average number of long connections, meaning the sixth load is less than the pre-deducted average number of long connections. In this case, the load level of the first server instance connected to the target client is much higher than the average load level, while the load level of the target server instance is lower than the average load level. In this situation, sending load balancing information to the target client to reconnect the long connection between the target client and the target server instance can effectively improve the load balancing effect.
[0064] In another possible scenario, the sixth load factor does not exceed the fourth tolerance level of the average load factor. For example, the sixth load factor may not exceed 90% of the average load factor (i.e., the sixth load factor is less than 90% of the average load factor). Simultaneously, the sixth load factor exceeds the pre-increased average number of long-lived connections (i.e., the sixth load factor is greater than the pre-increased average number of long-lived connections). In this case, the load level of the target server instance is significantly lower than the average load level, while the load level of the current first server instance is higher than the average load level. In this situation, sending load balancing information to the target client to reconnect the long-lived connections between the target client and the target server instance can effectively improve the load balancing effect. For example, if neither of the above two conditions is met, load balancing information is not sent to the target client, i.e., the long-lived connections of the target client are not reconnected, in order to improve the stability of business data transmission.
[0065] In this embodiment, the relationship between the fifth load, the sixth load, and the average load is compared to determine whether to trigger the reconstruction of long connections. This improves the benefits of long connection reconstruction and avoids the problem of business data transmission being affected by excessively frequent long connection reconnection. It is particularly suitable for scenarios where long connections of clients are controlled to reconnect after the distributed system is expanded, thereby achieving load balancing and improving the overall load balancing effect of the distributed system.
[0066] Referring to Figure 6, which is a schematic flowchart of a load balancing method for long connections provided in this embodiment, this embodiment further refines step S102 based on the embodiment shown in Figure 2. The load balancing method for long connections includes:
[0067] Step S201: In response to receiving a short connection request from the target client, obtain the running load information of multiple server instances providing the target service in the distributed system, and call the target service through a long connection.
[0068] Step S202: Based on the first hash algorithm, for multiple server instances of the target server, construct a hash ring consisting of a first number of virtual hash node groups, wherein each virtual hash node group includes a second number of virtual hash nodes, and each virtual hash node in the same virtual hash node group corresponds to a different server instance, and the second number is the number of multiple server instances.
[0069] Step S203: Based on the client identifier of the target client and the hash ring, obtain an ordered candidate set, wherein the ordered candidate set includes the target virtual hash node mapped by the client identifier within the hash ring, and the target number of virtual hash nodes near the target hash node.
[0070] Step S204: Obtain load balancing information based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set.
[0071] For example, in this embodiment, a load balancer deployed in a distributed system is used as the execution entity. Specifically, after obtaining the runtime load information of multiple server instances providing the target service in the distributed system, the load balancer first constructs a hash ring composed of a first number of virtual hash node groups based on a first hash algorithm. For example, the first hash algorithm is the Ketama Hash algorithm. The hash ring constructed by this algorithm contains several virtual hash node groups, and each virtual hash node group consists of several virtual hash nodes. Each virtual hash node in the same virtual hash node group corresponds to a different server instance. Figure 7 is a schematic diagram of a hash ring provided in an embodiment of this disclosure. As shown in Figure 7, the hash ring includes three (first quantity) virtual hash node groups: virtual hash node group P1, virtual hash node group P2, and virtual hash node group P3. Virtual hash node group P1 includes p1_addr1, p1_addr2, p1_addr3, and p1_addr4, totaling four (second quantity) virtual hash nodes. Similarly, virtual hash node group P2 includes p2_addr1, p2_addr2, p2_addr3, and p2_addr4, totaling four virtual hash nodes. Virtual hash node group P3 includes p3_addr1, p3_addr2, p3_addr3, and p3_addr4, totaling four virtual hash nodes. The entire hash ring has a total of 12 virtual hash nodes. As shown in the diagram, each virtual hash node in the aforementioned virtual hash node group corresponds to a server instance. For example, p1_addr1, p2_addr1, and p3_addr1 all correspond to server instance in_1 (shown in the diagram as [p1_addr1, p2_addr1, p3_addr1] -> in_1); p1_addr2, p2_addr2, and p3_addr2 all correspond to server instance in_2 (shown in the diagram as [p1_addr2, p2_addr2, p3_addr2] -> in_1). The first number represents the number of server instances corresponding to the target service. The virtual hash nodes generated based on the first hash algorithm are randomly distributed on the hash ring. The second number represents the number of server instances corresponding to the target service.Meanwhile, the hash ring has a preset search direction. In the process of determining the ordered candidate set based on the hash ring, after the target virtual hash node is determined, the search can be further conducted based on this search direction to determine other virtual hash nodes in the ordered candidate set.
[0072] After obtaining the client identifier of the target client, a hash calculation is performed on it using the first hash algorithm to obtain a virtual hash node on the hash ring corresponding to the client identifier, i.e., the target virtual hash node. Then, based on the hash ring, a target number of virtual hash nodes near the target virtual hash node are obtained, forming an ordered candidate set. For example, the target number of virtual hash nodes after the target hash node; or the target number of virtual hash nodes before the target hash node; or 0.5 times the target number of virtual hash nodes on both sides of the target hash node. Here, "nearby" refers to a short communication distance between the physical devices where the server instances reside, such as being in the same data center. Alternatively, it could mean that the instance identifiers of the server instances are adjacent. Finally, based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set, and according to preset judgment rules, the server instance corresponding to the virtual hash node whose running load information best matches the judgment rules is determined as the target server instance, thus obtaining load balancing information.
[0073] In one possible implementation, as shown in Figure 8, step S204 is specifically implemented as follows:
[0074] Step S2041: Based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set, obtain the first load and the second load, wherein the first load represents the number of long connections of the server instance corresponding to the target virtual hash node, and the second load represents the minimum number of long connections of the server instances corresponding to each virtual hash node in the ordered candidate set.
[0075] Step S2042: If the first load does not exceed the first tolerance of the second load, then the server instance corresponding to the target virtual hash node is determined as the target server instance; if the first load exceeds the first tolerance of the second load, then the server instance corresponding to the second load is determined as the target server instance.
[0076] Step S2043: Generate load balancing information based on the instance identifier of the target server instance.
[0077] For example, in the steps of this embodiment, the corresponding load is obtained through the running load information of the server instance corresponding to the virtual hash node. The load is the number of long connections currently connected to the server instance. Specifically, by comparing the running load information of the server instances corresponding to each virtual hash node, a first load and a second load are determined. The first load represents the number of long connections of the server instance corresponding to the target virtual hash node, and the second load represents the minimum number of long connections of the server instances corresponding to each virtual hash node in the ordered candidate set. Then, if the first load does not exceed the first tolerance of the second load, the server instance corresponding to the target virtual hash node is determined as the target server instance. For example, if the number of long connections corresponding to the first load is 100 and the first tolerance is 120%, then if the number of long connections corresponding to the second load is 110, the first load does not exceed the first tolerance of the second load, and the server instance corresponding to the target virtual hash node is determined as the target server instance; if the number of long connections corresponding to the second load is 130, the first load exceeds the first tolerance of the second load, and the server instance corresponding to the second load is determined as the target server instance. By following the steps in this embodiment, the server instance with the lowest operating load can be selected as the target server instance, and corresponding load balancing information can be generated, thereby improving the load balancing effect.
[0078] In another possible implementation, as shown in Figure 9, the specific implementation of step S204 includes:
[0079] Step S2044: Obtain the third load of the server instance corresponding to the current virtual hash node.
[0080] Step S2045: If the third load does not exceed the second tolerance of the fourth load, then the server instance corresponding to the current virtual hash node is determined as the target server instance, where the fourth load is the number of long connections of the server instance corresponding to the next virtual hash node of the current virtual hash node.
[0081] Step S2046: If the third load exceeds the second tolerance of the fourth load, then set the next virtual hash node as the current virtual hash node and return to execute step S2044.
[0082] In this embodiment, for each virtual hash node in the ordered candidate set, starting from the first virtual hash node, the first virtual hash node is first set as the current virtual hash node, and its corresponding load is obtained, i.e., the third load, which represents the number of long connections of the current virtual hash node. Then, the fourth load corresponding to the next virtual hash node is obtained and compared with the third load, which is the number of long connections of the server instance corresponding to the next virtual hash node. If the third load does not exceed the second tolerance of the fourth load, the server instance corresponding to the current virtual hash node is directly determined as the target server instance, and no further detection is performed, thereby improving detection efficiency. If the third load exceeds the second tolerance of the fourth load, the next virtual hash node is set as the current virtual hash node, and the execution returns to step S2044, repeating the above steps until the target server instance is determined. Here, the second tolerance is the floating tolerance, for example, 110%.
[0083] Furthermore, in one possible implementation, the runtime load information includes the number of long connections to the server instances. To improve the accuracy of the runtime load information, before calculating the runtime load information of the server instances corresponding to each virtual hash node in the ordered candidate set, the number of long connections for each server instance is first increased by 1 to obtain a pre-increased number of long connections, and subsequent processing is performed based on this pre-increased number of long connections. After processing, the number of long connections for the server instances other than those corresponding to the hit virtual hash node is subtracted by 1 to obtain the actual number of long connections. Specifically, for example, the ordered candidate set includes virtual hash nodes p1, p2, and p3. The actual number of long connections for the server instance corresponding to virtual hash node p1 is n1, the actual number of long connections for the server instance corresponding to virtual hash node p2 is n1, and the actual number of long connections for the server instance corresponding to virtual hash node p3 is n3. Next, the number of long connections for each server instance is incremented by 1 to obtain the pre-incremented number of long connections. That is, the pre-incremented number of long connections for virtual hash node p1 is n1+1, the pre-incremented number of long connections for virtual hash node p2 is n2+1, and the pre-incremented number of long connections for virtual hash node p3 is n3+1. Then, based on the pre-incremented number of long connections for each virtual hash node (running load information, i.e., load), a judgment is made. For example, after determining that the server instance corresponding to virtual hash node p3 is the target server instance, the pre-incremented number of long connections for virtual hash nodes p1 and p2 is subtracted by 1 to obtain the actual number of long connections. That is, the actual number of long connections for virtual hash node p1 is still n1, and the actual number of long connections for virtual hash node p2 is still n2.
[0084] The above-described scheme in this embodiment can extract and estimate the load situation of the server instance after reconnection, thereby ensuring that the estimation of the instance level is accurate under high-concurrency request load balancing, and providing load balancing effect under high-concurrency conditions.
[0085] Step S205: Send load balancing information to the target client to instruct the target client to establish a long connection with the target server instance for the target service.
[0086] In this embodiment, the implementation of steps S201 and S205 is the same as that of steps S101 and S103 in the embodiment shown in FIG2 of this disclosure, and will not be described in detail here.
[0087] Corresponding to the load balancing method for long connections in the above embodiments, Figure 10 is a structural block diagram of a load balancing device for long connections provided in this disclosure embodiment. The method described in the above embodiments can be executed by this load balancing device for long connections. This device can be implemented by software and / or hardware, and can be integrated into an electronic device with certain data processing capabilities. The electronic device can include, but is not limited to, mobile terminals with big data processing capabilities, as well as fixed terminals with big data processing capabilities such as desktop computers and supercomputers.
[0088] For ease of illustration, only the parts relevant to embodiments of this disclosure are shown. Referring to FIG10, the load balancing device 3 for long connections includes:
[0089] The response module 31 is used to respond to the short connection request sent by the target client, obtain the running load information of multiple server instances providing the target service in the distributed system, and the target service is called through a long connection;
[0090] Processing module 32 is used to obtain load balancing information based on the running load information of the server instance. The load balancing information is used to characterize at least the target server instance among multiple server instances.
[0091] Output module 33 is used to send load balancing information to the target client to instruct the target client to establish a long connection with the target server instance for the target service.
[0092] According to one or more embodiments of this disclosure, the processing module 32 is specifically configured to: construct a hash ring consisting of a first number of virtual hash node groups for multiple server instances of the target server based on a first hash algorithm, wherein each virtual hash node group includes a second number of virtual hash nodes, and each virtual hash node in the same virtual hash node group corresponds to a different server instance, the second number being the number of multiple server instances; obtain an ordered candidate set based on the client identifier of the target client and the hash ring, wherein the ordered candidate set includes the target virtual hash node mapped by the client identifier within the hash ring, and a target number of virtual hash nodes near the target hash node; and obtain load balancing information based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set.
[0093] According to one or more embodiments of this disclosure, when the processing module 32 obtains load balancing information based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set, it is specifically used to: obtain a first load and a second load based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set, wherein the first load represents the number of long connections of the server instance corresponding to the target virtual hash node, and the second load represents the minimum number of long connections of the server instances corresponding to each virtual hash node in the ordered candidate set; if the first load does not exceed a first tolerance of the second load, then the server instance corresponding to the target virtual hash node is determined as the target server instance; if the first load exceeds the first tolerance of the second load, then the server instance corresponding to the second load is determined as the target server instance; and generate load balancing information based on the instance identifier of the target server instance.
[0094] According to one or more embodiments of this disclosure, when the processing module 32 obtains load balancing information based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set, it is specifically used to: cyclically execute the following steps until the target server instance is determined: obtain the third load of the server instance corresponding to the current virtual hash node; if the third load does not exceed the second tolerance of the fourth load, then determine the server instance corresponding to the current virtual hash node as the target server instance, wherein the fourth load is the number of long connections of the server instance corresponding to the next virtual hash node of the current virtual hash node; if the third load exceeds the second tolerance of the fourth load, then set the next virtual hash node as the current virtual hash node.
[0095] According to one or more embodiments of this disclosure, the runtime load information includes the number of long connections connected to the server instance. After obtaining an ordered candidate set based on the client identifier and hash ring of the target client, the processing module 32 is further configured to: increment the number of long connections of the server instance corresponding to each virtual hash node in the ordered candidate set by 1 to obtain a pre-increased number of long connections of the server instance corresponding to each virtual hash node in the ordered candidate set; when obtaining load balancing information based on the runtime load information of the server instance corresponding to each virtual hash node in the ordered candidate set, the processing module 32 is specifically configured to: obtain load balancing information based on the pre-increased number of long connections of the server instance corresponding to each virtual hash node in the ordered candidate set; after obtaining the load balancing information, the processing module 32 is further configured to: decrement the number of long connections of the server instance corresponding to the virtual hash node that did not match in the ordered candidate set by 1.
[0096] According to one or more embodiments of this disclosure, the response module 31 is specifically configured to: in response to receiving a short connection request sent by the target client, obtain the client identifier of the target client; perform a hash calculation based on the client identifier of the target client to obtain a hash time point within the target period; perform a hash calculation based on the current system time to obtain a hash time range within the target period; if the hash time point falls within the hash time range, obtain the running load information of multiple server instances providing the target service in the distributed system.
[0097] According to one or more embodiments of this disclosure, after obtaining load balancing information based on the running load information of the server instance, the processing module 32 is further configured to: obtain the fifth load of the first server instance currently establishing a long connection with the target client, and the sixth load of the target server instance indicated by the load balancing information; when the output module 33 sends the load balancing information to the target client, it is specifically configured to: if the fifth load exceeds the third tolerance of the average load and the sixth load does not exceed the pre-deducted average long connection number, then send the load balancing information to the target client; or, if the sixth load does not exceed the fourth tolerance of the average load and the sixth load exceeds the pre-increased average long connection number, then send the load balancing information to the target client.
[0098] According to one or more embodiments of this disclosure, the runtime load information includes the number of long connections connected to the server instance and / or the running status of the server instance; when the processing module obtains load balancing information based on the runtime load information of the server instance, it is specifically used to: obtain at least two candidate server instances corresponding to the target client according to a hash algorithm, and determine the target server instance from the at least two candidate server instances in combination with the runtime load information; and generate load balancing information based on the instance identifier of the target server instance.
[0099] The response module 31, processing module 32, and output module 33 are connected sequentially. The load balancing device 3 for long connections provided in this embodiment can execute the technical solution of the above method embodiment, and its implementation principle and technical effect are similar, so it will not be described again here.
[0100] Figure 11 is a schematic diagram of the structure of an electronic device provided in an embodiment of this disclosure. As shown in Figure 11, the electronic device 4 includes:
[0101] Processor 41, and memory 42 communicatively connected to processor 41;
[0102] Memory 42 stores instructions executed by the computer;
[0103] The processor 41 executes computer execution instructions stored in the memory 42 to implement the load balancing method for long connections in the embodiments shown in Figures 2-9.
[0104] Optionally, the processor 41 and the memory 42 are connected via a bus 43.
[0105] The relevant explanations can be understood by referring to the descriptions and effects of the steps in the embodiments corresponding to Figures 2-9, which will not be elaborated on here.
[0106] This disclosure provides a computer-readable storage medium storing computer-executable instructions. When executed by a processor, these instructions are used to implement the load balancing method for long connections provided in any of the embodiments corresponding to Figures 2-9 of this disclosure.
[0107] This disclosure provides a computer program product, including a computer program that, when executed by a processor, implements a load balancing method for long connections provided in any of the embodiments corresponding to Figures 2-9 of this disclosure.
[0108] To implement the above embodiments, this disclosure also provides an electronic device.
[0109] Referring to Figure 12, a schematic diagram of the structure of an electronic device 900 suitable for implementing embodiments of the present disclosure is shown. The electronic device 900 can be a terminal device or a server. The terminal device can include, but is not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, personal digital assistants (PDAs), tablet computers, portable media players (PMPs), and in-vehicle terminals (e.g., in-vehicle navigation terminals), as well as fixed terminals such as digital TVs and desktop computers. The electronic device shown in Figure 12 is merely an example and should not be construed as limiting the functionality and scope of the embodiments of the present disclosure.
[0110] As shown in Figure 12, the electronic device 900 may include a processing unit (e.g., a central processing unit, a graphics processing unit, etc.) 901, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 902 or a program loaded from a storage device 908 into a random access memory (RAM) 903. The RAM 903 also stores various programs and data required for the operation of the electronic device 900. The processing unit 901, ROM 902, and RAM 903 are interconnected via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.
[0111] Typically, the following devices can be connected to I / O interface 905: input devices 906 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 907 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 908 including, for example, magnetic tapes, hard disks, etc.; and communication devices 909. Communication device 909 allows electronic device 900 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 12 shows electronic device 900 with various devices, it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed alternatively.
[0112] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device 909, or installed from a storage device 908, or installed from a ROM 902. When the computer program is executed by a processing device 901, it performs the functions defined in the methods of embodiments of this disclosure.
[0113] It should be noted that the computer-readable medium described in this disclosure can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this disclosure, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in connection with an instruction execution system, apparatus, or device. In this disclosure, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium can be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wires, optical fibers, RF (radio frequency), etc., or any suitable combination thereof.
[0114] The aforementioned computer-readable medium may be included in the aforementioned electronic device; or it may exist independently and not assembled into the electronic device.
[0115] The aforementioned computer-readable medium carries one or more programs, which, when executed by the electronic device, cause the electronic device to perform the methods shown in the above embodiments.
[0116] Computer program code for performing the operations of this disclosure can be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, and C++, and conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a Local Area Network (LAN) or a Wide Area Network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0117] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0118] The units or modules described in the embodiments of this disclosure can be implemented in software or hardware. The names of the units or modules do not necessarily limit the specific unit itself.
[0119] The functions described above in this document can be performed, at least in part, by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: Field Programmable Gate Arrays (FPGAs), Application-Specific Integrated Circuits (ASICs), Application Standard Products (ASSPs), System-on-Chip (SoCs), Complex Programmable Logic Devices (CPLDs), and so on.
[0120] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. Machine-readable media can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0121] In a first aspect, according to one or more embodiments of this disclosure, a load balancing method for long-lived connections is provided, comprising:
[0122] In response to receiving a short connection request from a target client, the system obtains the runtime load information of multiple server instances providing the target service in a distributed system, wherein the target service is invoked via a long connection; based on the runtime load information of the server instances, load balancing information is obtained, wherein the load balancing information is used to at least characterize the target server instance among the multiple server instances; the load balancing information is sent to the target client to instruct the target client to establish a long connection with the target server instance for the target service.
[0123] According to one or more embodiments of this disclosure, obtaining load balancing information based on the runtime load information of the server instances includes: constructing a hash ring consisting of a first number of virtual hash node groups for multiple server instances of the target server based on a first hash algorithm, wherein each virtual hash node group includes a second number of virtual hash nodes, and each virtual hash node in the same virtual hash node group corresponds to a different server instance, the second number being the number of the multiple server instances; obtaining an ordered candidate set based on the client identifier of the target client and the hash ring, wherein the ordered candidate set includes the target virtual hash node mapped by the client identifier within the hash ring, and a target number of virtual hash nodes near the target hash node; and obtaining load balancing information based on the runtime load information of the server instances corresponding to each virtual hash node in the ordered candidate set.
[0124] According to one or more embodiments of this disclosure, obtaining load balancing information based on the operating load information of server instances corresponding to each virtual hash node in the ordered candidate set includes: obtaining a first load and a second load based on the operating load information of server instances corresponding to each virtual hash node in the ordered candidate set, wherein the first load represents the number of long connections of the server instance corresponding to the target virtual hash node, and the second load represents the minimum number of long connections possessed by the server instances corresponding to each virtual hash node in the ordered candidate set; if the first load does not exceed a first tolerance of the second load, then the server instance corresponding to the target virtual hash node is determined as the target server instance; if the first load exceeds the first tolerance of the second load, then the server instance corresponding to the second load is determined as the target server instance; and generating the load balancing information based on the instance identifier of the target server instance.
[0125] According to one or more embodiments of this disclosure, obtaining load balancing information based on the running load information of server instances corresponding to each virtual hash node in the ordered candidate set includes: cyclically executing the following steps until a target server instance is determined: obtaining the third load of the server instance corresponding to the current virtual hash node; if the third load does not exceed the second tolerance of the fourth load, then determining the server instance corresponding to the current virtual hash node as the target server instance, wherein the fourth load is the number of long connections of the server instance corresponding to the next virtual hash node of the current virtual hash node; if the third load exceeds the second tolerance of the fourth load, then setting the next virtual hash node as the current virtual hash node.
[0126] According to one or more embodiments of this disclosure, the runtime load information includes the number of long connections connected to the server instance. After obtaining an ordered candidate set based on the client identifier of the target client and the hash ring, the method further includes: incrementing the number of long connections of the server instance corresponding to each virtual hash node in the ordered candidate set by 1 to obtain a pre-increased number of long connections for the server instance corresponding to each virtual hash node in the ordered candidate set. Obtaining load balancing information based on the runtime load information of the server instance corresponding to each virtual hash node in the ordered candidate set includes: obtaining load balancing information based on the pre-increased number of long connections for the server instance corresponding to each virtual hash node in the ordered candidate set. After obtaining the load balancing information, the method further includes: decrementing the number of long connections of the server instance corresponding to the virtual hash node that did not match in the ordered candidate set by 1.
[0127] According to one or more embodiments of this disclosure, the step of obtaining the runtime load information of multiple server instances providing the target service in a distributed system in response to receiving a short connection request sent by a target client includes: obtaining the client identifier of the target client in response to receiving the short connection request sent by the target client; performing a hash calculation based on the client identifier of the target client to obtain a hash time point within a target period; performing a hash calculation based on the current system time to obtain a hash time range within the target period; and obtaining the runtime load information of multiple server instances providing the target service in a distributed system if the hash time point falls within the hash time range.
[0128] According to one or more embodiments of this disclosure, after obtaining load balancing information based on the running load information of the server instance, the method further includes: obtaining a fifth load of the first server instance currently establishing a long connection with the target client, and a sixth load of the target server instance indicated by the load balancing information; sending the load balancing information to the target client includes: if the fifth load exceeds a third tolerance level of the average load, and the sixth load does not exceed the pre-deducted average number of long connections, then sending the load balancing information to the target client; or, if the sixth load does not exceed a fourth tolerance level of the average load, and the sixth load exceeds the pre-increased average number of long connections, then sending the load balancing information to the target client.
[0129] According to one or more embodiments of this disclosure, the runtime load information includes the number of long connections connected to the server instance and / or the running status of the server instance; obtaining load balancing information based on the runtime load information of the server instance includes: obtaining at least two candidate server instances corresponding to the target client according to a hash algorithm, and determining the target server instance from the at least two candidate server instances in combination with the runtime load information; generating the load balancing information based on the instance identifier of the target server instance.
[0130] Secondly, according to one or more embodiments of this disclosure, a load balancing device for long-lived connections is provided, comprising:
[0131] The response module is used to respond to short connection requests sent by the target client and obtain the running load information of multiple server instances providing the target service in the distributed system, wherein the target service is invoked through a long connection;
[0132] The processing module is used to obtain load balancing information based on the running load information of the server instance, wherein the load balancing information is used to characterize at least the target server instance among the plurality of server instances.
[0133] The output module is used to send the load balancing information to the target client to instruct the target client to establish a long connection with the target server instance for the target service.
[0134] According to one or more embodiments of this disclosure, the processing module is specifically configured to: construct a hash ring consisting of a first number of virtual hash node groups for multiple server instances of the target server based on a first hash algorithm, wherein each virtual hash node group includes a second number of virtual hash nodes, and each virtual hash node in the same virtual hash node group corresponds to a different server instance, the second number being the number of the multiple server instances; obtain an ordered candidate set based on the client identifier of the target client and the hash ring, wherein the ordered candidate set includes the target virtual hash node mapped by the client identifier within the hash ring, and a target number of virtual hash nodes near the target hash node; and obtain load balancing information based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set.
[0135] According to one or more embodiments of this disclosure, when the processing module obtains load balancing information based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set, it is specifically configured to: obtain a first load and a second load based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set, wherein the first load represents the number of long connections of the server instance corresponding to the target virtual hash node, and the second load represents the minimum number of long connections of the server instances corresponding to each virtual hash node in the ordered candidate set; if the first load does not exceed a first tolerance of the second load, then the server instance corresponding to the target virtual hash node is determined as the target server instance; if the first load exceeds the first tolerance of the second load, then the server instance corresponding to the second load is determined as the target server instance; and generate the load balancing information based on the instance identifier of the target server instance.
[0136] According to one or more embodiments of this disclosure, when the processing module obtains load balancing information based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set, it is specifically used to: cyclically execute the following steps until a target server instance is determined: obtain the third load of the server instance corresponding to the current virtual hash node; if the third load does not exceed the second tolerance of the fourth load, then determine the server instance corresponding to the current virtual hash node as the target server instance, wherein the fourth load is the number of long connections of the server instance corresponding to the next virtual hash node of the current virtual hash node; if the third load exceeds the second tolerance of the fourth load, then set the next virtual hash node as the current virtual hash node.
[0137] According to one or more embodiments of this disclosure, the runtime load information includes the number of long connections connected to the server instance. After obtaining an ordered candidate set based on the client identifier of the target client and the hash ring, the processing module is further configured to: increment the number of long connections of the server instance corresponding to each virtual hash node in the ordered candidate set by 1 to obtain a pre-increased number of long connections of the server instance corresponding to each virtual hash node in the ordered candidate set; when obtaining load balancing information based on the runtime load information of the server instance corresponding to each virtual hash node in the ordered candidate set, the processing module is specifically configured to: obtain load balancing information based on the pre-increased number of long connections of the server instance corresponding to each virtual hash node in the ordered candidate set; after obtaining the load balancing information, the processing module is further configured to: decrement the number of long connections of the server instance corresponding to the virtual hash node that did not match in the ordered candidate set by 1.
[0138] According to one or more embodiments of this disclosure, the response module is specifically configured to: in response to receiving a short connection request sent by a target client, obtain the client identifier of the target client; perform a hash calculation based on the client identifier of the target client to obtain a hash time point within the target period; perform a hash calculation based on the current system time to obtain a hash time range within the target period; if the hash time point falls within the hash time range, obtain the operating load information of multiple server instances providing the target service in the distributed system.
[0139] According to one or more embodiments of this disclosure, after obtaining load balancing information based on the running load information of the server instance, the processing module is further configured to: obtain the fifth load of the first server instance currently establishing a long connection with the target client, and the sixth load of the target server instance indicated by the load balancing information; when the output module 33 sends the load balancing information to the target client, it is specifically configured to: if the fifth load exceeds the third tolerance of the average load and the sixth load does not exceed the pre-deducted average long connection number, then send the load balancing information to the target client; or, if the sixth load does not exceed the fourth tolerance of the average load and the sixth load exceeds the pre-increased average long connection number, then send the load balancing information to the target client.
[0140] According to one or more embodiments of this disclosure, the runtime load information includes the number of long connections connected to the server instance and / or the running status of the server instance; when the processing module obtains load balancing information based on the runtime load information of the server instance, it is specifically used to: obtain at least two candidate server instances corresponding to the target client according to a hash algorithm, and determine the target server instance from the at least two candidate server instances in combination with the runtime load information; and generate the load balancing information based on the instance identifier of the target server instance.
[0141] Thirdly, according to one or more embodiments of the present disclosure, an electronic device is provided, comprising: at least one processor and a memory;
[0142] The memory stores computer-executed instructions;
[0143] The at least one processor executes computer execution instructions stored in the memory, causing the at least one processor to perform the load balancing method for long connections as described in the first aspect and various possible designs of the first aspect above.
[0144] Fourthly, according to one or more embodiments of the present disclosure, a computer-readable storage medium is provided, wherein computer-executable instructions are stored therein, which, when executed by a processor, implement the load balancing method for long connections as described in the first aspect and various possible designs of the first aspect.
[0145] Fifthly, according to one or more embodiments of the present disclosure, a computer program product is provided, including a computer program that, when executed by a processor, implements the load balancing method for long connections as described in the first aspect and various possible designs of the first aspect.
[0146] The above description is merely a preferred embodiment of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described concept. For example, technical solutions formed by substituting the above features with (but not limited to) technical features disclosed in this disclosure that have similar functions.
[0147] Furthermore, while the operations are described in a specific order, this should not be construed as requiring these operations to be performed in the specific order shown or in a sequential order. In certain environments, multitasking and parallel processing may be advantageous. Similarly, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of this disclosure. Certain features described in the context of individual embodiments may also be implemented in combination in a single embodiment. Conversely, various features described in the context of a single embodiment may also be implemented individually or in any suitable sub-combination in multiple embodiments.
[0148] Although the subject matter has been described using language specific to structural features and / or methodological logic, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. Rather, the specific features and actions described above are merely illustrative examples of implementing the claims.
Claims
1. A load balancing method for long-lived connections, comprising: In response to receiving a short connection request from the target client, the system obtains the runtime load information of multiple server instances providing the target service in the distributed system, wherein the target service is invoked via a long connection; Based on the running load information of the server instances, load balancing information is obtained, and the load balancing information is used to characterize at least the target server instance among the plurality of server instances. The load balancing information is sent to the target client to instruct the target client to establish a long-lived connection with the target server instance for the target service.
2. The method according to claim 1, wherein obtaining load balancing information based on the running load information of the server instance includes: Based on the first hash algorithm, for multiple server instances of the target server, a hash ring is constructed consisting of a first number of virtual hash node groups, wherein each virtual hash node group includes a second number of virtual hash nodes, and each virtual hash node in the same virtual hash node group corresponds to a different server instance, and the second number is the number of the multiple server instances. Based on the client identifier of the target client and the hash ring, an ordered candidate set is obtained, wherein the ordered candidate set includes the target virtual hash node mapped by the client identifier within the hash ring, and the target number of virtual hash nodes near the target hash node; Load balancing information is obtained based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set.
3. The method according to claim 2, wherein obtaining load balancing information based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set includes: Based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set, a first load and a second load are obtained, wherein the first load represents the number of long connections of the server instance corresponding to the target virtual hash node, and the second load represents the minimum number of long connections of the server instances corresponding to each virtual hash node in the ordered candidate set. If the first load does not exceed the first tolerance level of the second load, then the server instance corresponding to the target virtual hash node is determined as the target server instance; if the first load exceeds the first tolerance level of the second load, then the server instance corresponding to the second load is determined as the target server instance. The load balancing information is generated based on the instance identifier of the target server instance.
4. The method according to claim 2, wherein obtaining load balancing information based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set includes: Repeat the following steps in sequence until the target server instance is determined: Get the third load of the server instance corresponding to the current virtual hash node; If the third load does not exceed the second tolerance of the fourth load, then the server instance corresponding to the current virtual hash node is determined as the target server instance, wherein the fourth load is the number of long connections of the server instance corresponding to the next virtual hash node of the current virtual hash node. If the third load exceeds the second tolerance of the fourth load, then the next virtual hash node is set as the current virtual hash node.
5. The method of claim 2, wherein the runtime load information includes the number of long-lived connections to the server instance, and after obtaining the ordered candidate set based on the client identifier of the target client and the hash ring, further comprising: Increment the number of long connections of the server instance corresponding to each virtual hash node in the ordered candidate set by 1 to obtain the pre-incremented number of long connections of the server instance corresponding to each virtual hash node in the ordered candidate set. The load balancing information obtained based on the running load information of the server instances corresponding to each virtual hash node in the ordered candidate set includes: Based on the pre-increased number of long connections for each server instance corresponding to each virtual hash node in the ordered candidate set, load balancing information is obtained. After obtaining the load balancing information, the following is also included: Decrement the number of long connections of the server instances corresponding to the virtual hash nodes that did not match in the ordered candidate set by 1.
6. The method according to claim 1, wherein obtaining the runtime load information of multiple server instances providing the target service in the distributed system in response to receiving a short connection request sent by the target client includes: In response to receiving a short connection request from the target client, obtain the client identifier of the target client; A hash calculation is performed based on the client identifier of the target client to obtain the hash time point within the target period; Based on the current system time, a hash calculation is performed to obtain the hash time range within the target period; If the hash time point falls within the hash time range, then the runtime load information of multiple server instances providing the target service in the distributed system is obtained.
7. The method according to claim 1, wherein after obtaining the load balancing information based on the running load information of the server instance, it further comprises: Obtain the fifth load of the first server instance currently establishing a long connection with the target client, and the sixth load of the target server instance indicated by the load balancing information; Sending the load balancing information to the target client includes: If the fifth load exceeds the third tolerance of the average load, and the sixth load does not exceed the pre-deducted average number of long connections, then the load balancing information is sent to the target client. or, If the sixth load does not exceed the fourth tolerance of the average load, and the sixth load exceeds the pre-increased average number of long connections, then the load balancing information is sent to the target client.
8. The method according to claim 1, wherein the operating load information includes the number of long connections connected to the server instance and / or the operating status of the server instance; The step of obtaining load balancing information based on the running load information of the server instance includes: The target server instance is determined from the at least two candidate server instances corresponding to the target client by obtaining at least two candidate server instances based on the running load information. The load balancing information is generated based on the instance identifier of the target server instance.
9. A load balancing device for long-lived connections, comprising: The response module is used to respond to short connection requests sent by the target client and obtain the running load information of multiple server instances providing the target service in the distributed system, wherein the target service is invoked through a long connection; The processing module is used to obtain load balancing information based on the running load information of the server instance, wherein the load balancing information is used to characterize at least the target server instance among the plurality of server instances. The output module is used to send the load balancing information to the target client to instruct the target client to establish a long connection with the target server instance for the target service.
10. An electronic device comprising: Processor and memory; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory, causing the processor to perform the load balancing method for long connections as described in any one of claims 1 to 8.
11. A computer-readable storage medium storing computer-executable instructions that, when executed by a processor, implement the load balancing method for long-lived connections as described in any one of claims 1 to 8.
12. A computer program product comprising a computer program, wherein the computer program, when executed by a processor, implements the load balancing method for long-lived connections as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Data pushing method and device, a data receiving method and device, equipment and system
CN110311967A
Load scheduling method and device for long connection communication, equipment and storage medium
CN112202918A
Remote procedure call load balancing method and device and data processing system
CN112350973A
Load balancing method and device, equipment and computer storage medium
CN113810304A
Load balancing method and device, equipment and storage medium
CN115134368A