Load balancing method and device, electronic equipment and computer readable storage medium
By broadcasting load information in the server cluster and judging the request processing capability based on its own load, other servers are recommended if they are insufficient, which solves the problems of load balancing complexity and resource waste in the prior art, and achieves efficient load balancing.
Patent Information
- Application Number
- CN202411714003.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-02-25
AI Technical Summary
The prior art requires additional load balancers or installing load balancers on each client when load balancing on the server side, resulting in complexity and waste of resources.
By broadcasting native load information between each server in the server cluster, and when receiving a client request, it determines whether the request can be processed based on its own load information. If it cannot, other servers are recommended to achieve load balancing.
It realizes efficient server-side load balancing without the need for additional load balancers and client installation, reducing system complexity and resource consumption.
Smart Images

Figure 00000000_0000_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a load balancing method, device, electronic device and computer-readable storage medium. Background Art
[0002] When a server provides services to a client, the client usually sends a service request to the server first, and the server responds to the service request and provides the corresponding service to the client. When there are many clients sending service requests, multiple servers are needed to provide the corresponding services. At this time, how to load balance these servers is a problem that needs to be solved.
[0003] In the prior art, a load balancing method based on HAproxy (free and open source software) and a load balancing method based on a client are provided.
[0004] Among them, HAproxy-based load balancing refers to using HAproxy as a load balancer on the server side. Each client sends a service request to the load balancer, which provides a consistent load balancing strategy to distribute these service requests to multiple servers, thereby completing load balancing. It can be seen that this method requires the addition of an additional load balancer.
[0005] Client-based load balancing means that a load balancer, ribbon or Feign, needs to be installed in each client. The client first selects a suitable server through the load balancer installed by itself, and then sends a request to the selected server. This method requires each client to install a load balancer before sending a request. Summary of the invention
[0006] In view of this, the purpose of the present application is to provide a load balancing method, device, electronic device and computer-readable storage medium, which does not require an additional load balancer to be added in front of each server, nor does it require a load balancer to be installed in each client.
[0007] In a first aspect, an embodiment of the present application provides a load balancing method, which is applied to a target server, wherein the target server is any server in a server cluster, and each server in the server cluster can provide a target service for a client, and the method includes:
[0008] At a first time interval, broadcasting local load information to other servers in the server cluster, and receiving local load information broadcasted by other servers, so that each server in the server cluster stores local load information of each server;
[0009] Upon receiving a target service request sent by a target client, judging whether the target server can currently provide the target service requested by the target service request to the target client according to the local load information of the target server itself; the target client is one of the clients;
[0010] If the target server is currently unable to provide the target service to the target client, a first recommended server is determined for the target client from other servers based on the local load information of each other server stored in the target server, and the communication address of the first recommended server is sent to the target client, so that the target client establishes a connection with the first recommended server based on the communication address.
[0011] In combination with the first aspect, an embodiment of the present application provides a first possible implementation of the first aspect, wherein the local load information includes: total memory, used memory, number of CPU cores, CPU usage, number of client connections, outgoing network bandwidth, and incoming network bandwidth.
[0012] In combination with the first aspect, an embodiment of the present application provides a second possible implementation of the first aspect, wherein, at a first time interval, broadcasting local load information to other servers in the server cluster, and receiving local load information broadcast by other servers, so that each server in the server cluster stores local load information of each server, includes:
[0013] Broadcasting local load information to other servers in the server cluster at a first time interval, and receiving local load information broadcasted by other servers;
[0014] For each received local load information, determining whether the local load information carries cluster identification information of the server cluster, so as to determine whether the local load information is information broadcasted by the server cluster;
[0015] If the local load information is information broadcast by the server cluster, judging whether the local load information is local load information of a server newly added to the server cluster according to the server identification information carried in the local load information;
[0016] If the local load information is the local load information of a server newly added to the server cluster, the local load information is added to the target server; if the local load information is the local load information of a server already added to the server cluster, the local load information is used as the latest local load information of the server already added to the server cluster, so that each server in the server cluster records the latest local load information of each server in the server cluster.
[0017] In combination with the first aspect, the embodiment of the present application provides a third possible implementation of the first aspect, wherein the method further includes:
[0018] For each local load information of other servers recorded in the target server, detecting the update duration of the local load information of the other servers at a second time interval, and determining whether the update duration is greater than a preset duration;
[0019] If the update duration is greater than a preset duration, the local load information of the other server is deleted in the target server, the other server is determined as a server that stops providing the target service, and the other server is removed from the server cluster.
[0020] In combination with the first aspect, an embodiment of the present application provides a fourth possible implementation of the first aspect, wherein at least one of the servers is pre-configured in each client, and the servers pre-configured in the target client include the target server; for any one of the clients, when the client sends the target service request, it selects a server from at least one of the servers pre-configured in the client, and sends the target service request to the selected server.
[0021] In combination with the first possible implementation manner of the first aspect, the embodiment of the present application provides a fifth possible implementation manner of the first aspect, wherein, judging, based on the local load information of the target server itself, whether the target server can currently provide the target service requested by the target service request for the client includes:
[0022] Calculate the CPU usage of the target server based on the current number of CPU cores and the CPU usage of the target server, and determine whether the CPU usage is greater than a first threshold;
[0023] If the CPU usage is greater than the first threshold, the memory usage of the target server is calculated based on the current total memory and used memory of the target server, and it is determined whether the memory usage is greater than a second threshold;
[0024] If the memory usage rate is greater than the second threshold, determining whether the outgoing network bandwidth of the target server is greater than a third threshold, and whether the incoming network bandwidth is greater than a fourth threshold;
[0025] If the outgoing network bandwidth is greater than the third threshold and the incoming network bandwidth is greater than the fourth threshold, determining whether the current number of client connections of the target server is greater than a fifth threshold;
[0026] If the number of client connections is greater than the fifth threshold, determining whether there is a first candidate server with a number of client connections less than a sixth threshold among other servers;
[0027] If there is a first candidate server among other servers whose number of client connections is less than the sixth threshold, it is determined that the target server cannot currently provide the target service for the target client, and the first recommended server is determined from the first candidate server.
[0028] In combination with the fifth possible implementation of the first aspect, the embodiment of the present application provides a sixth possible implementation of the first aspect, wherein the method further includes:
[0029] At a third time interval, detecting whether the current CPU usage rate of the target server is greater than a seventh threshold; the seventh threshold is greater than the first threshold;
[0030] If the CPU usage is greater than the seventh threshold, disconnecting from some clients and returning the corresponding second recommended server to the disconnected clients; if the CPU usage is not greater than the seventh threshold, determining whether the memory usage of the target server is greater than an eighth threshold; the eighth threshold is greater than the second threshold;
[0031] If the memory usage of the target server is greater than the eighth threshold, disconnecting from some clients and returning the corresponding second recommended server to the disconnected clients; if the memory usage of the target server is not greater than the eighth threshold, determining whether the outgoing network bandwidth of the target server is greater than the ninth threshold and whether the incoming network bandwidth is greater than the tenth threshold; the ninth threshold is greater than the third threshold, and the tenth threshold is greater than the fourth threshold;
[0032] If the outgoing network bandwidth of the target server is greater than the ninth threshold and the incoming network bandwidth is greater than the tenth threshold, disconnecting from some clients and returning the corresponding second recommended server to the disconnected clients; if the outgoing network bandwidth of the target server is not greater than the ninth threshold and, and / or, the incoming network bandwidth is not greater than the tenth threshold, determining whether the current number of client connections of the target server is greater than the eleventh threshold; the eleventh threshold is greater than the fifth threshold;
[0033] If the current number of client connections of the target server is greater than the eleventh threshold, disconnecting from some clients and returning corresponding second recommended servers to the disconnected clients; if the current number of client connections of the target server is not greater than the eleventh threshold, determining whether there is a second candidate server with a number of client connections less than the twelfth threshold among other servers;
[0034] If there is a second candidate server among other servers whose number of client connections is less than the twelfth threshold, disconnect some of the clients, determine a second recommended server from the second candidate server, and return the corresponding second recommended server to the disconnected clients.
[0035] In a second aspect, an embodiment of the present application further provides a load balancing device, which is applied to a target server, wherein the target server is any server in a server cluster, and each server in the server cluster can provide a target service for a client, and the device includes:
[0036] A broadcast module, configured to broadcast local load information to other servers in the server cluster at a first time interval, and receive local load information broadcast by other servers, so that each server in the server cluster stores local load information of each server;
[0037] a judgment module, configured to judge whether the target service end can currently provide the target service requested by the target service request to the target client according to the local load information of the target service end itself when receiving the target service request sent by the target client; the target client is one of the clients;
[0038] A recommendation module is used to determine a first recommended server for the target client from other servers based on the local load information of each other server stored in the target server if the target server is currently unable to provide the target service to the target client, and to send the communication address of the first recommended server to the target client, so that the target client establishes a connection with the first recommended server based on the communication address.
[0039] In combination with the second aspect, an embodiment of the present application provides a first possible implementation of the second aspect, wherein the local load information includes: total memory, used memory, number of CPU cores, CPU usage, number of client connections, outgoing network bandwidth, and incoming network bandwidth.
[0040] In combination with the second aspect, an embodiment of the present application provides a second possible implementation of the second aspect, wherein the broadcast module is used to broadcast local load information to other servers in the server cluster at a first time interval, and receive local load information broadcast by other servers, so that each server in the server cluster stores the local load information of each server, and is specifically used to:
[0041] Broadcasting local load information to other servers in the server cluster at a first time interval, and receiving local load information broadcasted by other servers;
[0042] For each received local load information, determining whether the local load information carries cluster identification information of the server cluster, so as to determine whether the local load information is information broadcasted by the server cluster;
[0043] If the local load information is information broadcast by the server cluster, judging whether the local load information is local load information of a server newly added to the server cluster according to the server identification information carried in the local load information;
[0044] If the local load information is the local load information of a server newly added to the server cluster, the local load information is added to the target server; if the local load information is the local load information of a server already added to the server cluster, the local load information is used as the latest local load information of the server already added to the server cluster, so that each server in the server cluster records the latest local load information of each server in the server cluster.
[0045] In combination with the second aspect, the embodiment of the present application provides a third possible implementation of the second aspect, wherein the device further includes:
[0046] A first detection module is used to detect, for each local load information of other servers recorded in the target server, an update duration of the local load information of the other servers according to a second time interval, and determine whether the update duration is greater than a preset duration;
[0047] The removal module is used to delete the local load information of the other server in the target server if the update duration is greater than the preset duration, determine the other server as a server that stops providing the target service, and remove the other server from the server cluster.
[0048] In a third aspect, an embodiment of the present application further provides an electronic device, comprising: a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate through the bus, and when the machine-readable instructions are executed by the processor, the steps in any possible implementation of the first aspect above are performed.
[0049] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps in any possible implementation of the first aspect described above are executed.
[0050] The load balancing method, device, electronic device and computer-readable storage medium provided in the embodiments of the present application are applied to a target server, the target server is any server in a server cluster, and each server in the server cluster can provide a target service for a client. The method includes: broadcasting local load information to other servers in the server cluster at a first time interval, and receiving local load information broadcast by other servers, so that each server in the server cluster stores local load information of each server; when receiving a target service request sent by a target client, judging whether the target server can currently provide the target service requested by the target service request to the target client according to the local load information of the target server itself; the target client is one of the clients; if the target server cannot currently provide the target service to the target client, then based on the local load information of each other server stored in the target server, determining a first recommended server for the target client from the other servers, and sending the communication address of the first recommended server to the target client, so that the target client establishes a connection with the first recommended server based on the communication address. In this embodiment, the local load information of each server in the server cluster is maintained by using a multicast mechanism (i.e., broadcast). When the target client sends a target service request, the target server determines whether to provide the target service to the target client based on the local load information. If the target server cannot currently provide the target service to the target client, the first recommended server is given based on the local load information of other servers to achieve load balancing between the servers in the server cluster. In this load balancing process, there is no need to add an additional load balancer in front of each server, nor is there a need to install a load balancer in each client.
[0051] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0053] Figure 1 A flow chart of a load balancing method provided in an embodiment of the present application is shown;
[0054] Figure 2 A schematic diagram of interaction between a client and a server cluster provided in an embodiment of the present application is shown;
[0055] Figure 3 A flow chart of a server cluster broadcasting local load information provided by an embodiment of the present application is shown;
[0056] Figure 4 A flowchart of determining whether a target service end can currently provide a target service requested by a target service request to a target client is shown in an embodiment of the present application;
[0057] Figure 5 A flowchart of a method for detecting whether a server cluster includes a server that stops providing a target service is shown in an embodiment of the present application;
[0058] Figure 6 A flow chart of load migration provided by an embodiment of the present application is shown;
[0059] Figure 7 A schematic diagram of the structure of a load balancing device provided in an embodiment of the present application is shown;
[0060] Figure 8 A schematic structural diagram of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0061] To make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all of the embodiments. The components of the embodiments of the present application generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present application.
[0062] Considering that the load balancing method based on HAproxy requires adding an additional load balancer before each server, the load balancing method based on the client requires each client to install a load balancer. Based on this, the embodiment of the present application provides a load balancing method, device, electronic device and computer-readable storage medium, so that when performing load balancing on the server, there is no need to add an additional load balancer in front of each server, and there is no need to install a load balancer in each client, which is described below through an embodiment.
[0063] To facilitate understanding of this embodiment, a load balancing method disclosed in the embodiment of this application is first introduced in detail. The method is applied to a target server, which is any server in a server cluster, and each server in the server cluster can provide a target service for a client.
[0064] In this embodiment, the server cluster includes at least two servers, and each server in the server cluster can provide a target service for the client. The target service can be any online service, such as a payment service, a session service, etc., which is not limited in this application.
[0065] like Figure 1 As shown, the load balancing method includes the following steps S101-S103:
[0066] S101: broadcasting local load information to other servers in the server cluster at a first time interval, and receiving local load information broadcasted by other servers, so that each server in the server cluster stores local load information of each server.
[0067] In this embodiment, each server in the server cluster broadcasts local load information to other servers in the server cluster at a first time interval, and receives local load information broadcast by other servers, so that each server in the server cluster stores the latest local load information of each server. Exemplarily, the first time interval is, for example, every 5 seconds.
[0068] like Figure 2 As shown, it is assumed that the server cluster contains three servers, namely server A, server B and server C. Taking server A as an example, server A broadcasts local load information to server B and server C at a first time interval, and receives the local load information broadcast by server B and server C, so that server A stores the latest local load information of server A, server B and server C.
[0069] In a possible implementation, the local load information includes: total memory, used memory, number of CPU cores, CPU usage, number of client connections, outgoing network bandwidth, and incoming network bandwidth.
[0070] In a possible implementation, Figure 3 As shown, when executing step S101, the following steps S1011-S1014 may be specifically performed:
[0071] S1011: broadcasting local load information to other servers in the server cluster at a first time interval, and receiving local load information broadcasted by other servers.
[0072] In this embodiment, the target server serves as any server in the server cluster. The target server broadcasts local load information to other servers in the server cluster except the target server at a first time interval, and receives local load information broadcasted by other servers.
[0073] S1012: For each received local load information, determine whether the local load information carries cluster identification information of the server cluster, so as to determine whether the local load information is information broadcasted by the server cluster.
[0074] In this embodiment, after receiving the local load information broadcasted by other servers, the target server determines whether the local load information carries the cluster identification information of the server cluster for each local load information broadcasted by other servers. When the local load information carries the cluster identification information of the server cluster, it indicates that the local load information is the information broadcasted by the server cluster; if the local load information does not carry the cluster identification information of the server cluster, it indicates that the local load information is not the information broadcasted by the server cluster.
[0075] S1013: If the local load information is information broadcast by the server cluster, determine whether the local load information is local load information of a server newly added to the server cluster according to the server identification information carried in the local load information.
[0076] In this embodiment, when the local load information is not the information broadcast by the server cluster, the target server can directly ignore (eg delete) the local load information.
[0077] When the local load information (such as local load information a) is the information broadcast by the server cluster, it is necessary to further determine whether the local load information (such as local load information a) is the local load information of the server newly added to the server cluster, that is, it is necessary to further determine whether the other servers corresponding to the local load information (such as local load information a) are the servers newly added to the server cluster.
[0078] Among them, the target server compares the server identification information carried in the local load information (for example, local load information a) with the server identification information carried in the local load information of other servers in the server cluster that it has stored. If, among the local load information of other servers that it has stored, there is a local load information that carries the same server identification information as the server identification information carried in the local load information a, it means that the local load information a is not the local load information of the server newly added to the server cluster, that is, it means that the other servers corresponding to the local load information a are not the servers newly added to the server cluster. If, among the local load information of other servers that it has stored, there is no local load information that carries the same server identification information as the server identification information carried in the local load information a, it means that the local load information a is the local load information of the server newly added to the server cluster, that is, it means that the other servers corresponding to the local load information a are the servers newly added to the server cluster.
[0079] S1014: If the local load information is the local load information of a server newly added to the server cluster, the local load information is added to the target server; if the local load information is the local load information of a server already added to the server cluster, the local load information is used as the latest local load information of the server already added to the server cluster, so that each server in the server cluster records the latest local load information of each server in the server cluster.
[0080] In this embodiment, if the local load information is the local load information of a server that has been added to the server cluster, the local load information (the latest local load information) is used to update the originally stored local load information of the server that has been added to the server cluster, so that each server in the server cluster records the latest local load information of each server in the server cluster.
[0081] S102: upon receiving a target service request sent by a target client, judging whether the target server can currently provide the target service requested by the target service request to the target client according to the local load information of the target server itself; the target client is one of the clients.
[0082] In a possible implementation, at least one server is pre-configured in each client. For any client, when sending a target service request, the client selects a server from at least one server pre-configured in the client, and sends the target service request to the selected server. The servers pre-configured in the target client include the target server, so the target client can send the target service request to the target server.
[0083] In this embodiment, for any client, when the client selects a server from at least one pre-configured server, the client may select the server ranked first according to the arrangement order of each server, and then send a target service request to the server ranked first. That is, at this time, the target server is ranked first among the pre-configured servers in the target client.
[0084] like Figure 2 As shown, assuming that the target server is server A, then, in this embodiment, when target server A receives the target service request sent by the target client, target server A determines whether target server A can currently provide the target service requested by the target service request to the target client based on its own local load information.
[0085] In a possible implementation, Figure 4 As shown, when executing step S102, the following steps S1021-S1026 may be specifically performed:
[0086] S1021: Calculate the CPU usage of the target server based on the current number of CPU cores and CPU usage of the target server, and determine whether the CPU usage is greater than a first threshold.
[0087] S1022: If the CPU usage is greater than the first threshold, the memory usage of the target server is calculated based on the current total memory and used memory of the target server, and it is determined whether the memory usage is greater than the second threshold.
[0088] like Figure 4 As shown, if the CPU usage is not greater than the first threshold, it means that the target server can provide the target service for the target client. At this time, the target server can directly respond to the target service request sent by the target client and provide the target service for the target client.
[0089] In this embodiment, the first threshold value and the second threshold value may be equal or unequal. For example, the first threshold value may be 30%, and the second threshold value may also be 30%.
[0090] S1023: If the memory usage rate is greater than the second threshold, determine whether the outbound network bandwidth of the target server is greater than the third threshold, and whether the inbound network bandwidth is greater than the fourth threshold.
[0091] like Figure 4 As shown, if the memory usage is not greater than the second threshold, the target server can provide the target service for the target client. At this time, the target server can directly respond to the target service request sent by the target client and provide the target service for the target client.
[0092] S1024: If the outgoing network bandwidth is greater than the third threshold and the incoming network bandwidth is greater than the fourth threshold, determine whether the current number of client connections to the target server is greater than a fifth threshold.
[0093] like Figure 4 As shown, if the outgoing network bandwidth is not greater than the third threshold, and / or the incoming network bandwidth is not greater than the fourth threshold, the target server can provide the target service for the target client. At this time, the target server can directly respond to the target service request sent by the target client and provide the target service for the target client.
[0094] The third threshold is greater than the fourth threshold. For example, the third threshold may be 80M and the fourth threshold may be 20M.
[0095] S1025: If the number of client connections is greater than the fifth threshold, determine whether there is a first candidate server with a number of client connections less than a sixth threshold among other servers.
[0096] Exemplarily, the fifth threshold may be 100.
[0097] like Figure 4 As shown, if the number of client connections to the target server is not greater than the fifth threshold, the target server can provide the target service for the target client. At this time, the target server can directly respond to the target service request sent by the target client and provide the target service for the target client.
[0098] S1026: If there is a first candidate server whose number of client connections is less than the sixth threshold among the other servers, it is determined that the target server cannot currently provide the target service for the target client, and a first recommended server is determined from the first candidate server.
[0099] In this embodiment, the sixth threshold is smaller than the fifth threshold. Exemplarily, the sixth threshold may be 90.
[0100] like Figure 4 As shown, if there is no first candidate server among other servers whose number of client connections is less than the sixth threshold, the target server is used to provide the target service for the target client. That is, at this time, the target server directly responds to the target service request sent by the target client and provides the target service for the target client.
[0101] If there is a first candidate server whose number of client connections is less than a sixth threshold among other servers except the target server, a first recommended server is determined from the first candidate server.
[0102] S103: If the target server cannot currently provide the target service for the target client, a first recommended server is determined for the target client from other servers based on the local load information of each other server stored in the target server, and the communication address of the first recommended server is sent to the target client, so that the target client establishes a connection with the first recommended server based on the communication address.
[0103] In this embodiment, if the target server is currently unable to provide the target service for the target client, a first recommended server is determined for the target client from other servers based on the local load information of each other server stored in the target server, the communication address of the first recommended server is sent to the target client, and the connection with the target server is disconnected, so that the target client establishes a connection with the first recommended server based on the communication address, and the target service is provided to the target client through the first recommended server. That is, at this time, after the first recommended server establishes a connection with the target client, it receives the target service request sent by the target client, responds to the target service request, and provides the target service to the target client.
[0104] In a possible implementation, Figure 5 As shown, the target server can also detect whether the server cluster includes a server that stops providing the target service according to the following steps S1041-S1042:
[0105] S1041: For each local load information of other servers recorded in the target server, detect the update duration of the local load information of the other servers at a second time interval, and determine whether the update duration is greater than a preset duration.
[0106] S1042: If the update duration is longer than the preset duration, the local load information of the other server is deleted in the target server, the other server is determined as a server that stops providing the target service, and the other server is removed from the server cluster.
[0107] Exemplarily, the second time interval may be every 5 seconds, and the preset duration may be 15 seconds.
[0108] In a possible implementation, when the target server has a heavy load, the target server may migrate the clients connected to it according to its own load conditions. Specifically, Figure 6 As shown, the load in the target server can be migrated through the following steps S1051-S1056:
[0109] S1051: Detect, at a third time interval, whether the current CPU usage of the target server is greater than a seventh threshold; the seventh threshold is greater than the first threshold.
[0110] In this embodiment, the third time interval may be 60 seconds, and the seventh threshold may be 70%.
[0111] S1052: If the CPU usage is greater than the seventh threshold, disconnect with some clients and return the corresponding second recommended server to the disconnected clients; if the CPU usage is not greater than the seventh threshold, determine whether the memory usage of the target server is greater than the eighth threshold; the eighth threshold is greater than the second threshold.
[0112] The eighth threshold may be equal to the second threshold or may not be equal to the second threshold. For example, the eighth threshold may also be 70%.
[0113] S1053: If the memory usage of the target server is greater than the eighth threshold, disconnect with some clients and return the corresponding second recommended server to the disconnected clients; if the memory usage of the target server is not greater than the eighth threshold, determine whether the outgoing network bandwidth of the target server is greater than the ninth threshold, and whether the incoming network bandwidth is greater than the tenth threshold; the ninth threshold is greater than the third threshold, and the tenth threshold is greater than the fourth threshold.
[0114] The ninth threshold is greater than the tenth threshold. For example, the ninth threshold may be 200M, and the tenth threshold may be 250M.
[0115] S1054: If the outgoing network bandwidth of the target server is greater than the ninth threshold and the incoming network bandwidth is greater than the tenth threshold, disconnect the connection with some clients and return the corresponding second recommended server to the disconnected part of the clients; if the outgoing network bandwidth of the target server is not greater than the ninth threshold and, and / or, the incoming network bandwidth is not greater than the tenth threshold, determine whether the current number of client connections to the target server is greater than the eleventh threshold; the eleventh threshold is greater than the fifth threshold.
[0116] Exemplarily, the eleventh threshold may be 1000.
[0117] S1054: If the current number of client connections to the target server is greater than the eleventh threshold, disconnect some of the clients and return the corresponding second recommended server to the disconnected clients; if the current number of client connections to the target server is not greater than the eleventh threshold, determine whether there is a second candidate server among other servers whose number of client connections is less than the twelfth threshold.
[0118] In this embodiment, the twelfth threshold value may be equal to or different from the sixth threshold value. Exemplarily, the twelfth threshold value may be 90.
[0119] S1056: If there is a second candidate server among other servers whose number of client connections is less than the twelfth threshold, disconnect the connection with some clients, determine a second recommended server from the second candidate server, and return the corresponding second recommended server to the disconnected part of the clients.
[0120] In this embodiment, when the target server disconnects from some clients and returns the corresponding second recommendation server to the disconnected clients, some clients will establish connections with the second recommendation server so that the second recommendation server provides target services for some clients.
[0121] Based on the same technical concept, the embodiment of the present application also provides a load balancing device, which is applied to a target server, and the target server is any server in a server cluster. Each server in the server cluster can provide a target service for a client, such as Figure 7 As shown, the device comprises:
[0122] The broadcast module 701 is used to broadcast the local load information to other servers in the server cluster at a first time interval, and receive the local load information broadcast by other servers, so that each server in the server cluster stores the local load information of each server;
[0123] The judgment module 702 is used to judge whether the target server can currently provide the target service requested by the target service request to the target client according to the local load information of the target server itself when receiving the target service request sent by the target client; the target client is one of the clients;
[0124] The recommendation module 703 is used to determine a first recommended server for the target client from other servers based on the local load information of each other server stored in the target server if the target server cannot currently provide the target service to the target client, and to send the communication address of the first recommended server to the target client, so that the target client establishes a connection with the first recommended server based on the communication address.
[0125] Optionally, the local load information includes: total memory, used memory, number of CPU cores, CPU usage, number of client connections, outgoing network bandwidth, and incoming network bandwidth.
[0126] Optionally, when the broadcast module 701 is used to broadcast the local load information to other servers in the server cluster at a first time interval, and receive the local load information broadcast by other servers, so that each server in the server cluster stores the local load information of each server, it is specifically used to:
[0127] Broadcasting local load information to other servers in the server cluster at a first time interval, and receiving local load information broadcasted by other servers;
[0128] For each received local load information, determining whether the local load information carries cluster identification information of the server cluster, so as to determine whether the local load information is information broadcasted by the server cluster;
[0129] If the local load information is information broadcast by the server cluster, judging whether the local load information is local load information of a server newly added to the server cluster according to the server identification information carried in the local load information;
[0130] If the local load information is the local load information of a server newly added to the server cluster, the local load information is added to the target server; if the local load information is the local load information of a server already added to the server cluster, the local load information is used as the latest local load information of the server already added to the server cluster, so that each server in the server cluster records the latest local load information of each server in the server cluster.
[0131] Optionally, the device further comprises:
[0132] A first detection module is used to detect, for each local load information of other servers recorded in the target server, an update duration of the local load information of the other servers according to a second time interval, and determine whether the update duration is greater than a preset duration;
[0133] The removal module is used to delete the local load information of the other server in the target server if the update duration is greater than the preset duration, determine the other server as a server that stops providing the target service, and remove the other server from the server cluster.
[0134] Optionally, each client is pre-configured with at least one of the servers, and the servers pre-configured in the target client include the target server; for any one of the clients, when the client sends the target service request, it selects a server from at least one of the servers pre-configured in the client, and sends the target service request to the selected server.
[0135] Optionally, when the determination module is used to determine whether the target server can currently provide the target service requested by the target service request to the target client according to the local load information of the target server itself, it is specifically used to:
[0136] Calculate the CPU usage of the target server based on the current number of CPU cores and the CPU usage of the target server, and determine whether the CPU usage is greater than a first threshold;
[0137] If the CPU usage is greater than the first threshold, the memory usage of the target server is calculated based on the current total memory and used memory of the target server, and it is determined whether the memory usage is greater than a second threshold;
[0138] If the memory usage rate is greater than the second threshold, determining whether the outgoing network bandwidth of the target server is greater than a third threshold, and whether the incoming network bandwidth is greater than a fourth threshold;
[0139] If the outgoing network bandwidth is greater than the third threshold and the incoming network bandwidth is greater than the fourth threshold, determining whether the current number of client connections of the target server is greater than a fifth threshold;
[0140] If the number of client connections is greater than the fifth threshold, determining whether there is a first candidate server with a number of client connections less than a sixth threshold among other servers;
[0141] If there is a first candidate server among other servers whose number of client connections is less than the sixth threshold, it is determined that the target server cannot currently provide the target service for the target client, and the first recommended server is determined from the first candidate server.
[0142] Optionally, the device further comprises:
[0143] A second detection module is used to detect whether the current CPU usage of the target server is greater than a seventh threshold at a third time interval; the seventh threshold is greater than the first threshold;
[0144] A first disconnection module is configured to disconnect some clients if the CPU usage is greater than the seventh threshold, and return the corresponding second recommended server to the disconnected clients; if the CPU usage is not greater than the seventh threshold, determine whether the memory usage of the target server is greater than an eighth threshold; the eighth threshold is greater than the second threshold;
[0145] A second disconnection module is configured to disconnect the connection with some clients and return the corresponding second recommended server to the disconnected clients if the memory usage of the target server is greater than the eighth threshold; if the memory usage of the target server is not greater than the eighth threshold, determine whether the outgoing network bandwidth of the target server is greater than the ninth threshold and whether the incoming network bandwidth is greater than the tenth threshold; the ninth threshold is greater than the third threshold, and the tenth threshold is greater than the fourth threshold;
[0146] A third disconnection module is used to disconnect the connection with some clients if the outgoing network bandwidth of the target server is greater than the ninth threshold and the incoming network bandwidth is greater than the tenth threshold, and return the corresponding second recommended server to the disconnected part of the clients; if the outgoing network bandwidth of the target server is not greater than the ninth threshold and, and / or, the incoming network bandwidth is not greater than the tenth threshold, determine whether the current number of client connections of the target server is greater than the eleventh threshold; the eleventh threshold is greater than the fifth threshold;
[0147] a fourth disconnection module, configured to disconnect some clients if the current number of client connections of the target server is greater than an eleventh threshold, and return a corresponding second recommended server to the disconnected clients; and to determine whether there is a second candidate server with a number of client connections less than a twelfth threshold among other servers if the current number of client connections of the target server is not greater than the eleventh threshold;
[0148] The fifth disconnection module is used to disconnect some clients if there is a second candidate server among other servers whose number of client connections is less than the twelfth threshold, determine the second recommended server from the second candidate server, and return the corresponding second recommended server to the disconnected part of the clients.
[0149] Figure 8 A structural diagram of an electronic device provided for an embodiment of the present application includes: a processor 801, a memory 802 and a bus 803, wherein the memory 802 stores machine-readable instructions executable by the processor 801. When the electronic device runs the above-mentioned information processing method, the processor 801 communicates with the memory 802 through the bus 803, and the processor 801 executes the machine-readable instructions to perform the method steps described in Example 1.
[0150] The embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method steps described in the first embodiment are executed.
[0151] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described devices, electronic devices, and computer-readable storage media can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0152] In the several embodiments provided in the present application, it should be understood that the disclosed methods, devices, electronic devices and computer-readable storage media can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation. For example, multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of devices or modules can be electrical, mechanical or other forms.
[0153] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0154] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0155] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present application can essentially be embodied in the form of a software product, or in other words, the part that contributes to the prior art or the part of the technical solution. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0156] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present application, which are used to illustrate the technical solutions of the present application, rather than to limit them. The protection scope of the present application is not limited thereto. Although the present application is described in detail with reference to the above-described embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-described embodiments within the technical scope disclosed in the present application, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present application, and should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A load balancing method, characterized in that: The method is applied to a target server, the target server is any server in a server cluster, each server in the server cluster can provide a target service for a client, and the method includes: At a first time interval, broadcasting local load information to other servers in the server cluster, and receiving local load information broadcasted by other servers, so that each server in the server cluster stores local load information of each server; Upon receiving a target service request sent by a target client, judging whether the target server can currently provide the target service requested by the target service request to the target client according to the local load information of the target server itself; the target client is one of the clients; If the target server is currently unable to provide the target service to the target client, a first recommended server is determined for the target client from other servers based on the local load information of each other server stored in the target server, and the communication address of the first recommended server is sent to the target client, so that the target client establishes a connection with the first recommended server based on the communication address.
2. The method according to claim 1, characterized in that: The local load information includes: total memory, used memory, number of CPU cores, CPU usage, number of client connections, outgoing network bandwidth, and incoming network bandwidth.
3. The method according to claim 1, characterized in that: The method of broadcasting the local load information to other servers in the server cluster at a first time interval, and receiving the local load information broadcasted by other servers, so that each server in the server cluster stores the local load information of each server, includes: Broadcasting local load information to other servers in the server cluster at a first time interval, and receiving local load information broadcasted by other servers; For each received local load information, determining whether the local load information carries cluster identification information of the server cluster, so as to determine whether the local load information is information broadcasted by the server cluster; If the local load information is information broadcast by the server cluster, judging whether the local load information is local load information of a server newly added to the server cluster according to the server identification information carried in the local load information; If the local load information is the local load information of a server newly added to the server cluster, the local load information is added to the target server; if the local load information is the local load information of a server already added to the server cluster, the local load information is used as the latest local load information of the server already added to the server cluster, so that each server in the server cluster records the latest local load information of each server in the server cluster.
4. The method according to claim 1, characterized in that: The method further comprises: For each local load information of other servers recorded in the target server, detecting the update duration of the local load information of the other servers at a second time interval, and determining whether the update duration is greater than a preset duration; If the update duration is greater than a preset duration, the local load information of the other server is deleted in the target server, the other server is determined as a server that stops providing the target service, and the other server is removed from the server cluster.
5. The method according to claim 1, characterized in that: At least one of the servers is pre-configured in each client, and the target server is included in the servers pre-configured in the target client; for any one of the clients, when sending the target service request, the client selects a server from at least one of the servers pre-configured in the client, and sends the target service request to the selected server.
6. The method according to claim 2, characterized in that: The determining, based on the local load information of the target server itself, whether the target server can currently provide the target service requested by the target service request to the client includes: Calculate the CPU usage of the target server based on the current number of CPU cores and the CPU usage of the target server, and determine whether the CPU usage is greater than a first threshold; If the CPU usage is greater than the first threshold, the memory usage of the target server is calculated based on the current total memory and used memory of the target server, and it is determined whether the memory usage is greater than a second threshold; If the memory usage rate is greater than the second threshold, determining whether the outgoing network bandwidth of the target server is greater than a third threshold, and whether the incoming network bandwidth is greater than a fourth threshold; If the outgoing network bandwidth is greater than the third threshold and the incoming network bandwidth is greater than the fourth threshold, determining whether the current number of client connections of the target server is greater than a fifth threshold; If the number of client connections is greater than the fifth threshold, determining whether there is a first candidate server with a number of client connections less than a sixth threshold among other servers; If there is a first candidate server among other servers whose number of client connections is less than the sixth threshold, it is determined that the target server cannot currently provide the target service for the target client, and the first recommended server is determined from the first candidate server.
7. The method according to claim 6, characterized in that: The method further comprises: At a third time interval, detecting whether the current CPU usage rate of the target server is greater than a seventh threshold; the seventh threshold is greater than the first threshold; If the CPU usage is greater than the seventh threshold, disconnecting from some clients and returning the corresponding second recommended server to the disconnected clients; if the CPU usage is not greater than the seventh threshold, determining whether the memory usage of the target server is greater than an eighth threshold; the eighth threshold is greater than the second threshold; If the memory usage of the target server is greater than the eighth threshold, disconnecting from some clients and returning the corresponding second recommended server to the disconnected clients; if the memory usage of the target server is not greater than the eighth threshold, determining whether the outgoing network bandwidth of the target server is greater than the ninth threshold and whether the incoming network bandwidth is greater than the tenth threshold; the ninth threshold is greater than the third threshold, and the tenth threshold is greater than the fourth threshold; If the outgoing network bandwidth of the target server is greater than the ninth threshold and the incoming network bandwidth is greater than the tenth threshold, disconnecting from some clients and returning the corresponding second recommended server to the disconnected clients; if the outgoing network bandwidth of the target server is not greater than the ninth threshold and, and / or, the incoming network bandwidth is not greater than the tenth threshold, determining whether the current number of client connections of the target server is greater than the eleventh threshold; the eleventh threshold is greater than the fifth threshold; If the current number of client connections of the target server is greater than the eleventh threshold, disconnecting from some clients and returning corresponding second recommended servers to the disconnected clients; if the current number of client connections of the target server is not greater than the eleventh threshold, determining whether there is a second candidate server with a number of client connections less than the twelfth threshold among other servers; If there is a second candidate server among other servers whose number of client connections is less than the twelfth threshold, disconnect some of the clients, determine a second recommended server from the second candidate server, and return the corresponding second recommended server to the disconnected clients.
8. A load balancing device, characterized in that: The device is applied to a target server, the target server is any server in a server cluster, each server in the server cluster can provide a target service for a client, and the device includes: A broadcast module, configured to broadcast local load information to other servers in the server cluster at a first time interval, and receive local load information broadcast by other servers, so that each server in the server cluster stores local load information of each server; a judgment module, configured to judge whether the target service end can currently provide the target service requested by the target service request to the target client according to the local load information of the target service end itself when receiving the target service request sent by the target client; the target client is one of the clients; A recommendation module is used to determine a first recommended server for the target client from other servers based on the local load information of each other server stored in the target server if the target server is currently unable to provide the target service to the target client, and to send the communication address of the first recommended server to the target client, so that the target client establishes a connection with the first recommended server based on the communication address.
9. An electronic device, characterized in that: include: A processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the method as described in any one of claims 1 to 7 are performed.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are executed.