Connection method and device, equipment and storage medium
By using client virtual identity and transmission carrier in the MQTT server cluster, dynamic load balancing and failover are achieved, and the load balancing and availability problems of MQTT server clusters are solved, and the stability and efficiency of Internet of Vehicles communication are improved.
Patent Information
- Application Number
- CN202411214781.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-30
- Publication Date
- 2024-12-24
AI Technical Summary
MQTT server clusters have poor load balancing and low availability in the Internet of Vehicles scenario, and cannot dynamically adjust the load, resulting in difficulty in transferring communications of clients and affecting utilization and availability.
By introducing client virtual identity and transmission carrier into the MQTT server cluster, dynamically manage the communication relationship between the client and the server, load balancing and failover are realized, ensuring that any server establishes communication with the client, and cancels invalid relationship within the preset time limit .
It improves the load balancing and availability of MQTT server cluster, ensures the stability and efficiency of client communication, dynamically adjusts the number of transmission carriers to match communication needs, and saves resources.
Smart Images

Figure CN119182774A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of communication technology, and in particular, relates to a connection method, device, equipment and storage medium. Background Art
[0002] In the prior art, the MQTT protocol is an IoT communication protocol based on the publish / subscribe model. In the IoV scenario, the MQTT protocol is widely used in IoV message access services between vehicles and servers, ensuring flexible, fast and secure access to massive vehicle systems.
[0003] However, in the related art, when a client based on the MQTT protocol communicates with an MQTT server, in order to avoid communication conflicts caused by multiple MQTT servers in the MQTT server cluster receiving connection requests with the same client identifier at the same time, each client is connected to a fixed MQTT server through its unique client identifier for Internet of Vehicles message communication, that is, the communication between the client and the MQTT server is one-to-one. Since the client can only connect to a fixed MQTT server, the MQTT server cannot dynamically adjust the load, resulting in poor load balancing of the MQTT server. At the same time, when the MQTT server fails or is overloaded and cannot complete communication with the client, it is impossible to automatically transfer the communication of the client communicating with the failed or overloaded MQTT server to a healthy or lightly loaded MQTT server, resulting in low utilization of the MQTT server cluster and low availability of the MQTT server. Therefore, there is an urgent need for a method to solve the problems of poor load balancing and low availability of MQTT services in the prior art. Summary of the invention
[0004] The embodiment of the present application provides an implementation solution different from the prior art to solve the technical problems of poor load balancing and low availability of the MQTT service in the prior art.
[0005] In the first aspect, the present application provides a connection method, applicable to a first MQTT server included in an MQTT server cluster, comprising: in response to obtaining first request data from a client, obtaining, from the MQTT server cluster, a client virtual identifier of the client and a first transmission carrier corresponding to the client virtual identifier and used to transmit communication data between the client and the first MQTT server; responding to the first request data to obtain corresponding response data; feeding back the response data and the client virtual identifier to the client via the first transmission carrier; and in response to not detecting that the client communicates with any server in the MQTT server cluster for a preset time period, releasing the correspondence between the client virtual identifier and the first transmission carrier in the MQTT server cluster.
[0006] In the second aspect, the present application provides a connection device, which is suitable for a first MQTT server included in an MQTT server cluster, including: an acquisition unit, which is used to obtain, in response to the first request data of the client, a client virtual identifier of the client and a first transmission carrier corresponding to the client virtual identifier, which is used to transmit communication data between the client and the first MQTT server, from the MQTT server cluster; a response unit, which is used to respond to the first request data and obtain corresponding response data; a feedback unit, which is used to feed back the response data and the client virtual identifier to the client through the first transmission carrier; a detection unit, which is used to release the correspondence between the client virtual identifier and the first transmission carrier in the MQTT server cluster in response to the failure to detect that the client communicates with any server in the MQTT server cluster for more than a preset time.
[0007] In a third aspect, the present application provides an electronic device comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute any method in the first aspect or any possible implementation manner of the first aspect by executing the executable instructions.
[0008] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the computer program implements any method in the first aspect or any possible implementation manner of the first aspect.
[0009] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the method described in the first aspect or any possible implementation manner of the first aspect.
[0010] The solution provided by the present application is to obtain the client virtual identifier of the client and the first transmission carrier corresponding to the client virtual identifier for transmitting the communication data between the client and the first MQTT server from the MQTT server cluster in response to the first request data obtained from the client; respond to the first request data to obtain corresponding response data; feedback the response data and the client virtual identifier to the client through the first transmission carrier; in response to the client not being detected to communicate with any server in the MQTT server cluster for more than a preset time, the corresponding relationship between the client virtual identifier and the first transmission carrier in the MQTT server cluster is released. It is realized that any server in the MQTT server cluster can establish communication with the client, so that the MQTT server can dynamically adjust the load, so that the load balancing of the MQTT server is better, and when the MQTT server fails or is overloaded, the client currently communicating with the MQTT server can be transferred to a healthy MQTT server, thereby improving the utilization rate of the MQTT server cluster and the availability of the MQTT server. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following is a brief introduction to the drawings required for use in the embodiments or the prior art descriptions. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work. In the drawings:
[0012] Figure 1 A schematic diagram of a connection method provided in an embodiment of the present application;
[0013] Figure 2 A schematic diagram of the structure of a connection device provided in one embodiment of the present application;
[0014] Figure 3 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0015] The embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present application, but cannot be understood as limiting the present application.
[0016] The terms "first" and "second" etc. in the specification, claims and drawings of the embodiments of the present application are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein, for example. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, the process, method, system, product or equipment comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or equipment.
[0017] First, some terms in the embodiments of the present application are explained below to facilitate understanding by those skilled in the art.
[0018] MQTT server cluster: contains multiple MQTT servers, and the multiple MQTT servers can communicate with each other.
[0019] MQTT server: refers to a server that communicates with clients based on the MQTT protocol.
[0020] Car computer: refers to the abbreviation of the in-vehicle infotainment product installed in the car. The car computer can realize the information communication between people and cars, cars and the outside world (cars and cars)
[0021] In the prior art, the MQTT protocol is an IoT communication protocol based on the publish / subscribe model. In the IoV scenario, the MQTT protocol is widely used in IoV message access services between vehicles and servers, ensuring flexible, fast and secure access to massive vehicle systems.
[0022] However, in the related art, when a client based on the MQTT protocol communicates with an MQTT server, in order to avoid communication conflicts caused by multiple MQTT servers in the MQTT server cluster receiving connection requests with the same client identifier at the same time, each client is connected to a fixed MQTT server through its unique client identifier for vehicle network message communication, that is, the communication between the client and the MQTT server is one-to-one. Since the client can only connect to a fixed MQTT server, the MQTT server cannot dynamically adjust the load, resulting in poor load balancing of the MQTT server. At the same time, when the MQTT server fails or is overloaded and cannot complete communication with the client, the communication of the client communicating with the failed or overloaded MQTT server cannot be automatically transferred to a healthy or lightly loaded MQTT server, resulting in low utilization of the MQTT server cluster and low availability of the MQTT server. However, although the use of dynamic client identifiers can realize dynamic communication between the client of the MQTT protocol and the MQTT server, the use of dynamic client identifiers requires the client identifier to be regenerated each time a connection is made, resulting in the inability to maintain sessions and offline messages, so the method of using dynamic client identifiers for communication is not advisable. Therefore, there is an urgent need for a method to solve the problems of poor load balancing and low availability of MQTT services in the prior art.
[0023] In addition, when a client based on the MQTT protocol communicates with an MQTT server, there is also the problem of the order in which messages arrive at the server or other clients. When a client based on the MQTT protocol communicates with an MQTT server, only the end-to-end message reliability can be guaranteed, but the end-to-end message sequence cannot be guaranteed. If a client sends multiple messages, due to network delays or retransmission mechanisms, the order in which the messages arrive at the server or other clients may be inconsistent with the order in which they are sent, which is unacceptable for some application scenarios that require message order. For example, in Internet of Vehicles applications, the order of vehicle data is very important. For example, when analyzing driving behavior, the speed and position data of the vehicle need to be processed in chronological order; when analyzing fuel consumption, the fuel sensor data needs to be processed in sequence. If the message sequence is disordered, the analysis results will be distorted.
[0024] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0025] Figure 1A flow chart of a connection method provided for an exemplary embodiment of the present application is applicable to a first MQTT server included in an MQTT server cluster. The method includes at least the following steps S101-S104:
[0026] S101, in response to obtaining first request data of a client, obtaining, from the MQTT server cluster, a client virtual identifier of the client and a first transmission carrier corresponding to the client virtual identifier and used for transmitting communication data between the client and the first MQTT server;
[0027] Optionally, the first transmission carrier is used to transmit messages between the client and the first MQTT server.
[0028] Specifically, the first transmission carrier is an internal object of the MQTT server cluster end for managing the communication between the client and the MQTT server, and the server sends and receives data through the internal object.
[0029] Optionally, in the aforementioned S101, before responding to obtaining the first request data from the client, the method further includes the following steps S1-S3:
[0030] S1. In response to receiving a first connection request sent by the client and including a client identifier of the client, creating a client virtual identifier corresponding to the client identifier;
[0031] Optionally, the client identifier refers to a real unique identifier of the client.
[0032] Optionally, the client virtual identifier refers to an identifier newly created for the client and corresponding to the client identifier, serving as the virtual identifier of the client.
[0033] Optionally, the creating the client virtual identifier corresponding to the client identifier includes: creating the client virtual identifier corresponding to the client identifier based on a preset rule.
[0034] Optionally, creating the client virtual identifier corresponding to the client identifier based on a preset rule includes the following steps S001-S003:
[0035] S001. Obtain the number of transmission carriers that have been generated in the MQTT server cluster, and take the value of the number of transmission carriers plus one as the target value;
[0036] The number of transmission carriers is used to count the number of transmission carriers generated in the MQTT server cluster when it is put into use; the specific method for counting the number of transmission carriers is: when the MQTT server cluster is put into use, the initial value is set to zero, and each time the MQTT server cluster generates a transmission carrier, the value of the number of transmission carriers is increased by one;
[0037] Exemplarily, the number of transmission carriers can be counted by defining a global variable global_index, which is used as the index of the global_index transmission carrier. The initial value of global_index is 0, and the value of global_index increases by 1 every time a new transmission carrier is created in the MQTT server cluster.
[0038] S002, obtaining the current time and the random number, and generating an initial client virtual identifier according to the format of "target value_current time_random number" by using the target value, the current time and the random number;
[0039] S003. Perform hash processing or encoding processing on the initial client virtual identifier to obtain the client virtual identifier.
[0040] Exemplarily, the target value is obtained as current_index; the current timestamp is obtained as timestamp (accurate to milliseconds); a random number is generated as random_number. The initial client virtual identifier is generated as current_index_timestamp_random_number; the current_index_timestamp_random_number is processed using the SHA-256 hash function to generate a hash value of a fixed length, and the hash value is used as the client virtual identifier; or the current_index_timestamp_random_number is processed using the Base64 encoding method to generate a short and readable client virtual identifier.
[0041] In summary, a unique and predictable client virtual identifier is generated based on parameters such as the target value, the current timestamp and a random number, and the use of random numbers better guarantees the uniqueness of the client virtual identifier. These parameters are combined and hashed or encoded to ensure the uniqueness and security of the client virtual identifier.
[0042] S2. Create the first transmission carrier corresponding to the client virtual identifier in the MQTT server cluster;
[0043] Optionally, after the first transmission carrier is bound to the client virtual identifier, the binding may be stored in a hash table to facilitate subsequent query and management.
[0044] Optionally, the hash table is used to store a mapping relationship between the client virtual identifier and the first transmission carrier, wherein the client virtual identifier can be used as a key and the first transmission carrier can be used as a value and stored in the hash table.
[0045] Exemplarily, the generated client virtual identifier is 5a6b...; the first transmission carrier corresponding to the client virtual identifier is connection_object; and the mapping relationship stored in the hash table is ["5a6b..."]=connection_object.
[0046] S3. Send the client virtual identifier to the client through the first transmission carrier, so that the client establishes a connection with the first MQTT server, and communicates with the first MQTT server based on the first transmission carrier and the client virtual identifier.
[0047] Optionally, in the aforementioned S101, obtaining from the MQTT server cluster a first transmission carrier corresponding to the client virtual identifier and used to transmit communication data between the client and the first MQTT server includes: detecting whether a second transmission carrier corresponding to the client virtual identifier is stored in the MQTT server cluster, and if so, using the second transmission carrier as the first transmission carrier for transmitting communication data between the client and the first MQTT server.
[0048] Optionally, detecting whether a second transmission carrier corresponding to the client virtual identifier is stored in the MQTT server cluster includes:
[0049] It is detected whether a second transmission carrier corresponding to the client virtual identifier is stored in a hash table in the MQTT server cluster.
[0050] Optionally, in the aforementioned S101, a first transmission carrier corresponding to the client virtual identifier and used to transmit communication data between the client and the first MQTT server is obtained from the MQTT server cluster, including: if it is detected that the second transmission carrier corresponding to the client virtual identifier is not stored in the MQTT server cluster, detecting whether there is an idle third transmission carrier in the MQTT server cluster; if so, using the third transmission carrier as the first transmission carrier for transmitting communication data between the client and the first MQTT server; if not, generating a first transmission carrier for transmitting communication data between the client and the first MQTT server.
[0051] Optionally, the idle third transmission carrier refers to a transmission carrier that is not bound to a client virtual identifier in the MQTT server cluster, that is, the transmission carrier is in an unused state.
[0052] S102, responding to the first request data to obtain corresponding response data;
[0053] Optionally, in the aforementioned S102, the first request data includes: message content and a message sequence number of the message content, the message sequence number starts counting from the time when the client first establishes a connection with any MQTT server in the MQTT server cluster, and the responding to the first request data and obtaining corresponding response data in the aforementioned S102 includes: responding to the message content of the first request data according to the message sequence number and obtaining corresponding response data.
[0054] Optionally, the message sequence number is used to indicate the order in which the client sends messages to the MQTT server cluster, and each time the client sends a message to the MQTT server cluster, the message sequence number increases by one.
[0055] Optionally, the message sequence number may be located in the message header of the message content.
[0056] Optionally, in the aforementioned S102, responding to the message content of the first request data according to the message sequence number to obtain corresponding response data includes the following steps S1021-S1022:
[0057] S1021. Obtain the message sequence number from the message content, determine whether there is an unprocessed message before the message sequence number, and if so, obtain at least one to-be-processed message sequence number of at least one unprocessed message;
[0058] Optionally, in the aforementioned S1021, obtaining the message sequence number from the message content, and determining whether there is an unprocessed message before the message sequence number includes:
[0059] Obtain the message content, store the message content in a temporary buffer area, extract the message sequence number from the message content, and determine whether the message sequence number is continuous with the message sequence number corresponding to the previous message content of the message content; if not, determine that there is an unprocessed message before the message sequence number;
[0060] Optionally, when it is determined that the message sequence number is discontinuous with the message sequence number corresponding to the previous message content of the message content, the message content is marked as a to-be-sorted state and moved to a sorting cache area; if the sorting cache area already stores the same to-be-sorted message content as the message content, the message content is deleted.
[0061] S1022. Sort the message sequence number and the at least one to-be-processed message sequence number in ascending order, and process the unprocessed messages in ascending order until the message content corresponding to the first request data is processed to obtain corresponding response data.
[0062] Optionally, in the aforementioned S1022, sorting the message sequence number and the at least one to-be-processed message sequence number in ascending order includes:
[0063] In the sorting buffer area, the at least one to-be-processed message sequence number and the message sequence number are sorted in ascending order using a quick sorting algorithm.
[0064] Optionally, after sorting processing is performed in the sorting buffer area, the message group containing the message content corresponding to the message sequence number is transferred to the processing queue, and the message group is processed sequentially from small to large, wherein the message group refers to a group of sorted message contents to be processed that contain the message content.
[0065] Optionally, if there is a skipped sequence number in the message sequence number corresponding to the pending message content in the message group, that is, the message sequence number corresponding to the previous pending message content in the message group is 2 less than the message sequence number corresponding to the next pending message content, then it is determined that there is a skipped sequence number, and the missing message sequence number is sent to the client, requesting the client to re-transmit the message content corresponding to the missing message sequence number.
[0066] Optionally, in order to improve the efficiency of message processing, when there is a missing message sequence number (skipped sequence number) in the message group, a request is made to the client to resend the message content corresponding to the missing message sequence number, and at the same time, the pending messages corresponding to other message sequence numbers in the message group that are smaller than the missing message sequence number are processed first, thereby avoiding processing stagnation and improving the efficiency of message processing.
[0067] Optionally, in order to avoid the problem of message sequence number overflow caused by the message sequence number being too large, the method also includes: setting a message sequence number threshold, and when the message sequence number is not less than the message sequence number threshold, resetting the message sequence number to zero, wherein, in order to avoid duplication of message sequence numbers, establishing a correspondence between the message sequence number and the current time, and sorting the message sequence numbers in chronological order, thereby ensuring that the sequence number column does not overflow and avoiding the problem of duplicate sequence numbers that cannot be sorted.
[0068] In summary, the order of the messages received by the first MQTT server from the client corresponding to the client virtual identifier is guaranteed by the message sequence number and the client virtual identifier, and even if there is a delay in network transmission, the messages can be processed in the correct order. This enables the sequential processing of messages in application scenarios that require message order.
[0069] S103, feeding back the response data and the client virtual identifier to the client through the first transmission carrier;
[0070] S104: In response to not detecting that the client communicates with any server in the MQTT server cluster for a preset time period, releasing the correspondence between the client virtual identifier and the first transmission carrier in the MQTT server cluster.
[0071] Optionally, in the aforementioned S104, releasing the correspondence between the client virtual identifier and the first transmission carrier in the MQTT server cluster includes:
[0072] The correspondence between the client virtual identifier and the first transmission carrier stored in the hash table in the MQTT server cluster is deleted.
[0073] Optionally, when the correspondence between the client virtual identifier and the first transmission carrier is released, the first transmission carrier can be re-bound with other client virtual identifiers to establish a correspondence between the client virtual identifiers of other clients and the first transmission carrier. The client virtual identifier can also establish a correspondence with other idle or newly generated transmission carriers in the MQTT server cluster.
[0074] Optionally, in order to improve the efficiency of acquiring transmission carriers, the present application sets up a method of detecting idle transmission carriers and regenerating transmission carriers to solve the above-mentioned problem. The method also includes: detecting whether the idle number of the idle third transmission carriers in the MQTT server cluster is within a preset idle number range, and if not, generating a fourth transmission carrier.
[0075] Optionally, the preset idle number range includes a minimum idle number, and if the idle number of the idle third transmission carriers is not less than the minimum idle number, it is determined that the idle number of the third transmission carriers is within the preset idle number range.
[0076] Optionally, in order to avoid the problem that there are too many idle third transmission carriers in the MQTT server cluster and too many transmission carriers waste system resources, the method further includes:
[0077] Detect whether the idle number of the idle third transmission carriers in the MQTT server cluster exceeds the maximum idle threshold; if so, delete some of the idle third transmission carriers so that the idle number of the remaining idle third transmission carriers is within the preset idle number range, wherein the maximum idle threshold is the maximum number of idle transmission carriers that can be accommodated in the MQTT server cluster that is preset.
[0078] In summary, by detecting the number of idle transmission carriers, the peak and off-peak periods of communication demand are determined, and transmission carriers are automatically generated or deleted based on the peak and off-peak periods of communication demand, so that the number of reserved transmission carriers can meet the communication demand and avoid excessive reservation of transmission carriers, saving resources. During peak periods, there are more communication demands and more transmission carriers are required, while during off-peak periods, there are fewer communication demands and fewer transmission carriers are required.
[0079] Optionally, different MQTT servers in the MQTT server cluster can generate different numbers of transmission carriers, and the aforementioned generating the fourth transmission carrier includes the following steps S01-S02:
[0080] S01, obtaining a target MQTT server in the MQTT server cluster, the number of which has generated transmission carriers not exceeding its corresponding transmission carrier generation threshold;
[0081] Optionally, the load capacity of each MQTT server in the MQTT server cluster is different, and the MQTT server with high load capacity can generate a larger number of transmission carriers, and the MQTT server with low load capacity can generate a smaller number of transmission carriers. Therefore, in order to avoid excessive load or overload of a certain MQTT server, a transmission carrier generation threshold corresponding to its load capacity is set for each MQTT server, wherein the transmission carrier generation threshold is used to measure the load capacity of the MQTT server.
[0082] Optionally, the transmission carrier includes an identifier of the MQTT server, and the number of transmission carriers generated by each MQTT server can be obtained by counting the identifiers of the MQTT servers corresponding to the generated transmission carriers.
[0083] S02. Prompt the target MQTT server to generate a fourth transmission carrier.
[0084] Optionally, when the first MQTT server fails, in order to prevent the client's communication request from being continuously processed by other MQTT servers, the method further includes:
[0085] If the first MQTT server fails, the correspondence between the first MQTT server and the client virtual identifier of the client is canceled, the first transmission carrier generated by the first MQTT server is deleted, and a correspondence between the client virtual identifier and the fifth transmission carrier of other healthy MQTT servers in the MQTT server cluster is established. The healthy MQTT server communicates with the client through the fifth transmission carrier.
[0086] In summary, by dynamically generating transmission carriers and generating transmission carriers according to the load capacity of each MQTT server, the load of any MQTT server in the MQTT server cluster is balanced, avoiding overload of a certain MQTT server. And when a certain MQTT server is overloaded, the load pressure of the MQTT server can be dispersed by deleting the transmission carrier corresponding to the MQTT server and generating transmission carriers corresponding to other MQTT servers with lower loads. The normal operation of the MQTT server is guaranteed. When the MQTT server fails, the transmission carriers generated by other healthy MQTT servers can also be used to realize the communication between the healthy MQTT server and the client corresponding to the failed MQTT server, avoiding the situation that when the MQTT server fails, there is no corresponding MQTT server to process the communication request of its corresponding client, thus ensuring the stability and continuity of the client-server communication system.
[0087] In summary, the solution provided by the present application is to obtain the client virtual identifier of the client from the MQTT server cluster in response to the first request data obtained from the client, and the first transmission carrier corresponding to the client virtual identifier for transmitting the communication data between the client and the first MQTT server; respond to the first request data to obtain the corresponding response data; feedback the response data and the client virtual identifier to the client through the first transmission carrier; in response to the client not being detected to communicate with any server in the MQTT server cluster for more than a preset time, the corresponding relationship between the client virtual identifier and the first transmission carrier in the MQTT server cluster is released. It is realized that any server in the MQTT server cluster can establish communication with the client, so that the MQTT server can dynamically adjust the load, so that the load balancing of the MQTT server is better, and when the MQTT server fails or is overloaded, the client currently communicating with the MQTT server can be transferred to a healthy MQTT server, thereby improving the utilization rate of the MQTT server cluster and the availability of the MQTT server.
[0088] Secondly, by releasing the transmission carrier that is occupied by the client but not used for communication, the released transmission carrier can be used by other clients, which improves the efficiency of using the transmission carrier and improves the efficiency and flexibility of the server in managing the transmission carrier. And according to the communication needs during peak hours and off-peak hours, the transmission carrier is established or reduced, and the number of transmission carriers is dynamically adjusted according to the communication needs, which improves the communication efficiency and saves system resources. The transmission carrier generated by the MQTT server is dynamically adjusted according to the load capacity of each MQTT server to prevent a server from being overloaded and improve the stability and reliability of the MQTT server. By generating a unique and predictable client virtual identifier, it is convenient for the MQTT server cluster to manage the correspondence between the client virtual identifier and the transmission carrier.
[0089] Figure 2 A structural schematic diagram of a connection device provided for an exemplary embodiment of the present application; the device is applicable to a first MQTT server included in an MQTT server cluster, wherein the device includes:
[0090] An acquiring unit 21 is configured to acquire, in response to acquiring first request data of a client, a client virtual identifier of the client and a first transmission carrier corresponding to the client virtual identifier and used for transmitting communication data between the client and the first MQTT server from the MQTT server cluster;
[0091] A response unit 22, configured to respond to the first request data and obtain corresponding response data;
[0092] A feedback unit 23, configured to feed back the response data and the client virtual identifier to the client via the first transmission carrier;
[0093] The detection unit 24 is used to release the correspondence between the client virtual identifier and the first transmission carrier in the MQTT server cluster in response to not detecting that the client communicates with any server in the MQTT server cluster for more than a preset time.
[0094] Optionally, the device is used to, before responding to obtaining the first request data from the client, also be used to: in response to receiving a first connection request sent by the client, including the client identifier of the client, create the client virtual identifier corresponding to the client identifier; create the first transmission carrier corresponding to the client virtual identifier in the MQTT server cluster; send the client virtual identifier to the client via the first transmission carrier, so that the client establishes a connection with the first MQTT server, and communicates with the first MQTT server based on the first transmission carrier and the client virtual identifier.
[0095] Optionally, the device is used to obtain from the MQTT server cluster a first transmission carrier corresponding to the client virtual identifier and used to transmit communication data between the client and the first MQTT server, and is specifically used to: detect whether a second transmission carrier corresponding to the client virtual identifier is stored in the MQTT server cluster; if so, use the second transmission carrier as the first transmission carrier for transmitting communication data between the client and the first MQTT server.
[0096] Optionally, the device is used to obtain from the MQTT server cluster a first transmission carrier corresponding to the client virtual identifier and used to transmit communication data between the client and the first MQTT server, and is specifically used to: if it is detected that the second transmission carrier corresponding to the client virtual identifier is not stored in the MQTT server cluster, detect whether there is an idle third transmission carrier in the MQTT server cluster; if so, use the third transmission carrier as the first transmission carrier for transmitting communication data between the client and the first MQTT server; if not, generate a first transmission carrier for transmitting communication data between the client and the first MQTT server.
[0097] Optionally, in the device, the first request data includes: message content and a message sequence number of the message content, the message sequence number starts counting from the time when the client first establishes a connection with any MQTT server in the MQTT server cluster, and the device is used to respond to the first request data and obtain corresponding response data, specifically for: responding to the message content of the first request data according to the message sequence number and obtaining corresponding response data.
[0098] Optionally, the device is further used to: detect whether the idle number of the idle third transmission carriers in the MQTT server cluster is within a preset idle number range, and if not, generate a fourth transmission carrier.
[0099] Optionally, in the device, different MQTT servers in the MQTT server cluster can generate different numbers of transmission carriers, and the device is used for generating the fourth transmission carrier to specifically: obtain a target MQTT server in the MQTT server cluster, the number of transmission carriers generated not exceeding its corresponding transmission carrier generation threshold; and prompt the target MQTT server to generate the fourth transmission carrier.
[0100] It should be understood that the device embodiment and the method embodiment may correspond to each other, and similar descriptions may refer to the method embodiment. To avoid repetition, no further description is given here. Specifically, the device may perform the above method embodiment, and the above and other operations and / or functions of each module in the device are the corresponding processes in each method in the above method embodiment, respectively, and no further description is given here for the sake of brevity.
[0101] The above describes the device of the embodiment of the present application from the perspective of the functional module in conjunction with the accompanying drawings. It should be understood that the functional module can be implemented in hardware form, can be implemented by instructions in software form, and can also be implemented by a combination of hardware and software modules. Specifically, the steps of the method embodiment in the embodiment of the present application can be completed by the hardware integrated logic circuit and / or software form instructions in the processor, and the steps of the method disclosed in the embodiment of the present application can be directly embodied as a hardware decoding processor to perform, or a combination of hardware and software modules in the decoding processor to perform. Optionally, the software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, an electrically erasable programmable memory, a register, etc. The storage medium is located in a memory, and the processor reads the information in the memory, and completes the steps in the above method embodiment in conjunction with its hardware.
[0102] Figure 3 is a schematic block diagram of an electronic device provided in an embodiment of the present application, and the electronic device may include:
[0103] The memory 301 and the processor 302, the memory 301 is used to store the computer program and transmit the program code to the processor 302. In other words, the processor 302 can call and run the computer program from the memory 301 to implement the method in the embodiment of the present application.
[0104] For example, the processor 302 may be configured to execute the above method embodiments according to instructions in the computer program.
[0105] In some embodiments of the present application, the processor 302 may include but is not limited to:
[0106] General-purpose processor, digital signal processor (DSP), application-specific integrated circuit (ASIC), field programmable gate array (FPGA) or other programmable logic device, discrete gate or transistor logic device, discrete hardware components, etc.
[0107] In some embodiments of the present application, the memory 301 includes but is not limited to:
[0108] Volatile memory and / or non-volatile memory. Among them, the non-volatile memory can be read-only memory (ROM), programmable ROM (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM) or flash memory. The volatile memory can be random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link DRAM (SLDRAM), and direct RAM bus random access memory (DR RAM).
[0109] In some embodiments of the present application, the computer program may be divided into one or more modules, which are stored in the memory 301 and executed by the processor 302 to complete the method provided by the present application. The one or more modules may be a series of computer program instruction segments capable of completing specific functions, and the instruction segments are used to describe the execution process of the computer program in the electronic device.
[0110] like Figure 3 As shown, the electronic device may also include:
[0111] The transceiver 303 may be connected to the processor 302 or the memory 301 .
[0112] The processor 302 may control the transceiver 303 to communicate with other devices, specifically, to send information or data to other devices, or to receive information or data sent by other devices. The transceiver 303 may include a transmitter and a receiver. The transceiver 303 may further include an antenna, and the number of antennas may be one or more.
[0113] It should be understood that the various components in the electronic device are connected via a bus system, wherein the bus system includes not only a data bus but also a power bus, a control bus and a status signal bus.
[0114] The present application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a computer, enables the computer to perform the method of the above method embodiment. In other words, the present application embodiment also provides a computer program product containing instructions, which, when executed by a computer, enables the computer to perform the method of the above method embodiment.
[0115] When software is used for implementation, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function according to the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (digital subscriber line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integration. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a digital video disc (digital video disc, DVD)), or a semiconductor medium (e.g., a solid state drive (solid state disk, SSD)), etc.
[0116] Those of ordinary skill in the art will appreciate that the modules and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0117] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the module is only a logical function division. There may be other division methods in actual implementation, such as 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 interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.
[0118] The modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. For example, each functional module in each embodiment of the present application may be integrated into a processing module, or each module may exist physically separately, or two or more modules may be integrated into one module.
[0119] The above are only specific implementations of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A connection method, characterized in that: Applicable to a first MQTT server included in an MQTT server cluster, including: In response to obtaining the first request data of the client, obtaining, from the MQTT server cluster, a client virtual identifier of the client and a first transmission carrier corresponding to the client virtual identifier and used for transmitting communication data between the client and the first MQTT server; Respond to the first request data and obtain corresponding response data; Feedback the response data and the client virtual identifier to the client via the first transmission carrier; In response to not detecting that the client communicates with any server in the MQTT server cluster for a preset time period, the correspondence between the client virtual identifier and the first transmission carrier in the MQTT server cluster is released.
2. The method according to claim 1, characterized in that Before responding to the first request data obtained from the client, the method further includes: In response to receiving a first connection request sent by the client and including a client identifier of the client, creating the client virtual identifier corresponding to the client identifier; Creating the first transmission carrier corresponding to the client virtual identifier in the MQTT server cluster; The client virtual identifier is sent to the client via the first transmission carrier, so that the client establishes a connection with the first MQTT server, and communicates with the first MQTT server based on the first transmission carrier and the client virtual identifier.
3. The method according to claim 1, characterized in that: The step of acquiring, from the MQTT server cluster, a first transmission carrier corresponding to the client virtual identifier and used for transmitting communication data between the client and the first MQTT server includes: It is detected whether the second transmission carrier corresponding to the client virtual identifier is stored in the MQTT server cluster. If so, the second transmission carrier is used as a first transmission carrier for transmitting communication data between the client and the first MQTT server.
4. The method according to claim 1, characterized in that: Acquiring, from the MQTT server cluster, a first transmission carrier corresponding to the client virtual identifier and used to transmit communication data between the client and the first MQTT server, comprising: If it is detected that the second transmission carrier corresponding to the client virtual identifier is not stored in the MQTT server cluster, it is detected whether there is an idle third transmission carrier in the MQTT server cluster. If so, the third transmission carrier is used as the first transmission carrier for transmitting the communication data between the client and the first MQTT server. If not, a first transmission carrier is generated for transmitting the communication data between the client and the first MQTT server.
5. The method according to claim 1, characterized in that The first request data includes: message content and a message sequence number of the message content, the message sequence number starts counting after the client establishes a connection with any MQTT server in the MQTT server cluster for the first time, and the responding to the first request data to obtain corresponding response data includes: Respond to the message content of the first request data according to the message sequence number to obtain corresponding response data.
6. The method according to claim 4, characterized in that The method further comprises: It is detected whether the idle number of the idle third transmission carriers in the MQTT server cluster is within a preset idle number range; if not, a fourth transmission carrier is generated.
7. The method according to claim 6, characterized in that Different MQTT servers in the MQTT server cluster can generate different numbers of transmission carriers, and generating the fourth transmission carrier includes: Obtain a target MQTT server in the MQTT server cluster, the number of transmission carriers generated not exceeding its corresponding transmission carrier generation threshold; The target MQTT server is prompted to generate a fourth transmission bearer.
8. A connection device, characterized in that: Applicable to a first MQTT server included in an MQTT server cluster, including: an acquiring unit, configured to acquire, in response to acquiring first request data of the client, a client virtual identifier of the client and a first transmission carrier corresponding to the client virtual identifier and used for transmitting communication data between the client and the first MQTT server from the MQTT server cluster; A response unit, used to respond to the first request data and obtain corresponding response data; A feedback unit, configured to feed back the response data and the client virtual identifier to the client via the first transmission carrier; The detection unit is used to release the correspondence between the client virtual identifier and the first transmission carrier in the MQTT server cluster in response to not detecting that the client communicates with any server in the MQTT server cluster for more than a preset time.
9. An electronic device, characterized in that: include: processor; as well as A memory, configured to store executable instructions of the processor; The processor is configured to perform the method of any one of claims 1 to 7 by executing the executable instructions.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.
