Long connection management method and device and computer storage medium
By verifying the validity of long connections between microservices and establishing new long connections, the problem of Connection Reset exceptions between microservices is solved and the reliability and consistency of data transmission are improved.
Patent Information
- Application Number
- CN202410798742.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-20
- Publication Date
- 2024-10-29
AI Technical Summary
In the existing technology, long http connections between microservices cause Connection Reset exceptions due to invalid connections, affecting the reliability and data consistency of data transmission.
Before transmitting data, obtain the long connection between microservices from the connection pool and perform validity verification. If it is invalid, establish a new long connection and put it into the connection pool to ensure that data is transferred through the new long connection. transmission to avoid Connection Reset exceptions.
It improves the reliability of data transmission between microservices, prevents important data transmission failures, and ensures data consistency.
Smart Images

Figure CN118869772A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data communications, and in particular to a long connection management method, device and computer storage medium. Background Art
[0002] Currently, calls between Spring Cloud microservices are mostly made using http. In the prior art, in order to improve the communication efficiency between microservices, long connections in the http connection pool are usually used, so that when making calls between microservices, there is no need to perform repeated three TCP handshakes, thereby improving communication efficiency.
[0003] However, using the persistent connection in the http connection pool may cause a Connection Reset exception due to some invalid persistent connections saved in the connection pool. For example, microservice A calls microservice B, and the default persistent connection keep-alive time (tomcat configuration) of microservice B is 60s. When microservice A calls microservice B 60S later and then makes a second call, microservice B has already closed the persistent connection, which may cause a Connection Reset exception in the call of microservice A. The Connection Reset exception between microservices may cause the failure of important data transmission between microservices, which may lead to data consistency problems.
[0004] Therefore, how to solve the Connection Reset exception in the existing technology to improve the reliability of data transmission between microservices is a technical problem that needs to be solved urgently. Summary of the invention
[0005] The present application provides a long connection management method, device and computer storage medium for improving the reliability of data transmission between microservices.
[0006] The first aspect of the present application provides a long connection management method, including:
[0007] When the first microservice needs to transmit target data to the second microservice, a first long connection between the first microservice and the second microservice is obtained from the connection pool;
[0008] Verifying whether the first long connection is valid;
[0009] If not, a second long connection between the first microservice and the second microservice is established, and the second long connection is placed in the connection pool, and the first microservice is controlled to transmit target data to the second microservice through the second long connection.
[0010] Optionally, after verifying whether the first long connection is valid, the long connection management method further includes:
[0011] If so, control the first microservice to transmit target data to the second microservice through the first long connection.
[0012] Optionally, in the if not, then establishing a second long connection between the first microservice and the second microservice, placing the second long connection into the connection pool, and controlling the first microservice to transmit target data to the second microservice through the second long connection, the long connection management method further includes:
[0013] Deleting the first long connection;
[0014] The connection pool is updated.
[0015] Optionally, the verifying whether the first long connection is valid includes:
[0016] Control the first microservice to transmit a first heartbeat message to the second microservice through the first long connection;
[0017] Determine whether the first heartbeat message is transmitted successfully;
[0018] If yes, it is determined that the first long connection is valid;
[0019] If not, it is determined that the first long connection is invalid.
[0020] Optionally, controlling the first microservice to transmit a first heartbeat message to the second microservice through the first long connection includes:
[0021] Control the first microservice to transmit a first heartbeat message in a preset format to the second microservice through the first long connection, where the preset format is a message format that can be parsed by the second microservice.
[0022] Optionally, if not, determining that the first long connection is invalid includes:
[0023] If not, control the first microservice to transmit a second heartbeat message to the second microservice through the first long connection;
[0024] Determine whether the second heartbeat message is transmitted successfully;
[0025] If not, it is determined that the first long connection is invalid.
[0026] Optionally, the first long connection and the second long connection are http long connections.
[0027] A second aspect of the present application provides a long connection management device, including:
[0028] A transmission unit, configured to obtain a first long connection between the first microservice and the second microservice from a connection pool when the first microservice needs to transmit target data to the second microservice;
[0029] A verification unit, used to verify whether the first long connection is valid;
[0030] and an establishing unit, configured to, if not, establish a second long connection between the first microservice and the second microservice, put the second long connection into a connection pool, and control the first microservice to transmit target data to the second microservice through the second long connection.
[0031] Optionally, the long connection management device further includes:
[0032] A control unit is configured to, if yes, control the first microservice to transmit target data to the second microservice through the first long connection.
[0033] Optionally, the long connection management device further includes:
[0034] A deleting unit, configured to delete the first long connection;
[0035] An updating unit is used to update the connection pool.
[0036] Optionally, the verification unit is specifically used for:
[0037] Control the first microservice to transmit a first heartbeat message to the second microservice through the first long connection;
[0038] Determine whether the first heartbeat message is transmitted successfully;
[0039] If yes, it is determined that the first long connection is valid;
[0040] If not, it is determined that the first long connection is invalid.
[0041] Optionally, the verification unit is specifically used for:
[0042] Control the first microservice to transmit a first heartbeat message in a preset format to the second microservice through the first long connection, where the preset format is a message format that can be parsed by the second microservice.
[0043] Optionally, the verification unit is specifically used to:
[0044] If not, control the first microservice to transmit a second heartbeat message to the second microservice through the first long connection;
[0045] Determine whether the second heartbeat message is transmitted successfully;
[0046] If not, it is determined that the first long connection is invalid.
[0047] A third aspect of the present application provides a long connection management device, the long connection management device comprising:
[0048] Processor, memory, input-output unit, and bus;
[0049] The processor is connected to the memory, the input and output unit, and the bus;
[0050] The memory stores a program, and the processor calls the program to execute the first aspect and an optional long connection management method of any one of the first aspect.
[0051] A fourth aspect of the present application provides a computer-readable storage medium, on which a program is stored. When the program is executed on a computer, the program executes the first aspect and any optional long connection management method in the first aspect.
[0052] It can be seen from the above technical scheme that the present application has the following advantages: when the first microservice needs to transmit target data to the second microservice, the first long connection between the first microservice and the second microservice is obtained from the connection pool, and it is checked whether the first long connection is valid. If not, a second long connection between the first microservice and the second microservice is established, and the second long connection is placed in the connection, and the first microservice is controlled to transmit the target data to the second microservice through the second long connection. Through the above method, before transmitting data, it is first determined whether the first long connection between the first microservice and the second microservice is valid. If it is invalid, the second long connection between the first microservice and the second microservice is immediately established, and data is transmitted through the second long connection, thereby avoiding the failure of important data transmission between microservices, thereby improving the reliability of data transmission. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] In order to more clearly illustrate the technical solution in the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0054] Figure 1 A flowchart of an embodiment of a long connection management method provided by this application;
[0055] Figure 2 A flowchart of another embodiment of a long connection management method provided by the present application;
[0056] Figure 3A flowchart of another embodiment of a long connection management method provided by the present application;
[0057] Figure 4 A flowchart of another embodiment of a long connection management method provided by the present application;
[0058] Figure 5 A schematic diagram of the structure of an embodiment of a long connection management device provided by the present application;
[0059] Figure 6 A schematic diagram of the structure of another embodiment of a long connection management device provided by the present application;
[0060] Figure 7 A schematic diagram of the structure of another embodiment of a long connection management device provided in this application. DETAILED DESCRIPTION
[0061] The present application provides a long connection management method, a management device and a computer storage medium for improving the reliability of data transmission between microservices.
[0062] It should be noted that the long connection management method provided in this application can be applied to a terminal or a server. For example, the terminal can be a smart phone or a computer, a tablet computer, a smart TV, a smart watch, a portable computer terminal, or a fixed terminal such as a desktop computer. For the convenience of explanation, this application uses the terminal as the execution subject for example.
[0063] Currently, calls between Spring Cloud microservices are mostly made using http. In the prior art, in order to improve the communication efficiency between microservices, long connections in the http connection pool are usually used, so that when making calls between microservices, there is no need to perform repeated three TCP handshakes, thereby improving communication efficiency.
[0064] However, using the long connection in the http connection pool may cause a Connection Reset exception due to some invalid long connections stored in the connection pool. Therefore, the present invention aims to solve the Connection Reset exception in the prior art, thereby improving the reliability of data transmission between microservices.
[0065] A long connection management method according to an embodiment of the first aspect of the present invention may include, but is not limited to:
[0066] Step S101: when the first microservice needs to transmit target data to the second microservice, a first long connection between the first microservice and the second microservice is obtained from the connection pool;
[0067] Step S102, verifying whether the first long connection is valid;
[0068] Step S103: if not, establish a second long connection between the first microservice and the second microservice, put the second long connection into the connection pool, and control the first microservice to transmit target data to the second microservice through the second long connection;
[0069] The long connection management method of the present invention requires that when the first microservice needs to transmit target data to the second microservice, the first long connection between the first microservice and the second microservice is first obtained from the connection pool; then the first long connection is verified to be valid; finally, if the first long connection is invalid, a second long connection between the first microservice and the second microservice is established, and the second long connection is placed in the connection pool, and the first microservice is controlled to transmit the target data to the second microservice through the second long connection. Through the above method, before transmitting data, it is first determined whether the first long connection between the first microservice and the second microservice is valid. If it is invalid, the second long connection between the first microservice and the second microservice is immediately established, and data is transmitted through the second long connection, thereby avoiding the failure of important data transmission between microservices, thereby improving the reliability of data transmission.
[0070] In step S101 of some embodiments, when the first microservice needs to transmit target data to the second microservice, the first long connection between the first microservice and the second microservice is obtained from the connection pool. It should be noted that the first microservice and the second microservice are small, self-contained, and ready-to-run applications, which can bring great flexibility to the code and simplify application management. The first microservice and the second microservice can transmit data through the first long connection established in advance. The first microservice can continuously send multiple data packets to the second microservice through the first long connection. Long connections are mostly used for frequent operations, point-to-point communication, and the number of connections cannot be too many. Each TCP connection requires a three-step handshake, which requires It takes time. If each operation is connected first, the processing speed will be much lower. Therefore, each operation is not disconnected. The data packet can be sent directly through the long connection during the next processing to improve the data transmission speed. For example, the first microservice and the second microservice transmit data through the first long connection established in advance. If short connections are used for frequent communication, socket errors may occur, and frequent socket creation will cause a waste of communication resources. Among them, the first long connection is an http long connection, and the connection pool is an http connection pool. In this way, when making data calls between microservices, there is no need to perform repeated TCP three-way handshakes, thereby achieving the effect of improving communication efficiency.
[0071] In step S102 of some embodiments, it is verified whether the first long connection is valid. It should be noted that before the first microservice and the second microservice transmit the target data through the first long connection established in advance, it is necessary to verify whether the first long connection is valid. This is because when using http long connection, occasional Connection Reset exceptions may occur. The reason for this exception is that the first long connection saved in the http pool may be invalid. For example: the first microservice calls the second microservice, and the default first long connection keep-alive time (tomcat configuration) of the second microservice is 60s. When the first microservice calls the second microservice 60s later, the second microservice has closed the connection at this time, then the call of the first microservice will throw a Connection Reset exception; the first long connection can be verified by sending a heartbeat message. The specific verification process will be described in Figure 2 The corresponding embodiments are described in detail, and the details are not repeated here. If the first long connection is verified to be valid, the first long connection can be marked, so that within the preset time range, when the first microservice needs to transmit target data to the second microservice again, it is no longer necessary to verify the first long connection, but the target data can be directly transmitted through the first long connection, thereby further improving the data transmission efficiency.
[0072] Reference Figure 2 According to some embodiments of the present application, step S102 of checking whether the first long connection is valid may specifically include, but is not limited to:
[0073] Step S201, controlling the first microservice to transmit a first heartbeat message to the second microservice through the first long connection;
[0074] Step S202, determining whether the first heartbeat message is transmitted successfully;
[0075] Step S203: If yes, determine that the first long connection is valid;
[0076] Step S204: if it is determined that the first long connection is valid, controlling the first microservice to transmit target data to the second microservice through the first long connection;
[0077] Step S205: if not, it is determined that the first long connection is invalid;
[0078] In steps S201 to S204 shown in the embodiment of the present application, a heartbeat message is a message sent by a sending source to a receiving party. The heartbeat message can be used to determine whether the data transmission channel between the sending source and the receiving party is connected normally, and to control the first microservice to transmit the first heartbeat message to the second microservice through the first long connection. By judging whether the first heartbeat message is successfully transmitted, it is determined whether the first long connection is valid. If the first heartbeat message is successfully transmitted, it is determined that the first long connection is valid, and step S204 is executed. If the first heartbeat message is not successfully transmitted, it is determined that the first long connection is invalid, and step S103 is executed. Whether the first long connection is valid is determined by whether the first heartbeat message is successfully sent. If the first long connection is valid, data is transmitted through the first long connection, thereby avoiding the connection failure caused by the invalid first long connection when the first microservice sends important data to the second microservice. Reset exception, thereby improving the reliability of data transmission and ensuring data consistency in some data transmission scenarios where failure is not allowed. Specifically, the first microservice can be controlled to transmit a first heartbeat message that conforms to a preset format to the second microservice through the first long connection. The preset format is a message format that can be parsed by both the first microservice and the second microservice. The first microservice is controlled to transmit the first heartbeat message that conforms to the preset format to the second microservice through the first long connection, so that the second microservice can correctly parse and respond to the first heartbeat message.
[0079] See also Figure 3 According to some embodiments of the present application, in step S205, if not, determining that the first long connection is invalid includes:
[0080] Step S301: if not, control the first microservice to transmit a second heartbeat message to the second microservice through the first long connection;
[0081] Step S302, determining whether the second heartbeat message is transmitted successfully;
[0082] Step S303: if not, determine that the first long connection is invalid;
[0083] In steps S301 to S303 shown in the embodiment of the present application, after controlling the first microservice to transmit the first heartbeat message to the second microservice through the first long connection fails, the first microservice is controlled again to transmit the second heartbeat message to the second microservice through the first long connection. If the second heartbeat message is successfully transmitted, it is determined that the first long connection is still valid at this time. The failure to transmit the first heartbeat message for the first time may be caused by network abnormalities and other reasons, rather than the failure of the first long connection. If the second heartbeat message fails to be transmitted, it is determined that the first long connection is invalid at this time.
[0084] In step S103 of some embodiments, if not, a second long connection between the first microservice and the second microservice is established, and the second long connection is placed in the connection pool, and the first microservice is controlled to transmit the target data to the second microservice through the second long connection. It should be noted that if it is determined that the first long connection is invalid, a second long connection between the first microservice and the second microservice is reestablished, and the second long connection is an http long connection, and the second long connection is placed in the connection pool, and the first microservice is controlled to transmit the target data to the second microservice through the second long connection. Through the above method, it is ensured that the long connection obtained from the http connection pool is valid, thereby avoiding the connection reset exception caused by the invalid first long connection when the first microservice sends important data to the second microservice, thereby improving the reliability of data transmission and ensuring data consistency in some data transmission scenarios where failure is not allowed.
[0085] Reference Figure 4 According to some embodiments of the present application, if no in step S103, a second long connection between the first microservice and the second microservice is established, and the second long connection is placed in the connection pool, and the first microservice is controlled to transmit target data to the second microservice through the second long connection, the long connection management method of the embodiment of the present application further includes:
[0086] Step S401, deleting the first long connection;
[0087] Step S402, updating the connection pool;
[0088] In some embodiments, in steps S401 to S402, it should be noted that since the number of long connections that can be stored in the connection pool is limited, after determining that the first long connection is invalid, the first long connection is promptly deleted from the connection pool, and the second long connection is added to the connection pool, thereby completing the update of the connection pool. Since the capacity of the connection pool is limited, invalid connections or connections with low frequency of use should be considered for deletion, thereby providing sufficient storage space for retaining valid connections, deleting the invalid first long connection between the first microservice and the second microservice, and saving the valid second long connection between the first microservice and the second microservice in the connection pool, so that when a microservice call is made, it is guaranteed that a valid long connection can be called, thereby improving the reliability of data transmission.
[0089] The above describes a long connection management method provided by the present application. The following describes a long connection management device provided by the present application:
[0090] See also Figure 5 , Figure 5 An embodiment of a long connection management device provided in the present application includes:
[0091] The transmission unit 501 is used to obtain a first long connection between the first microservice and the second microservice from a connection pool when the first microservice needs to transmit target data to the second microservice;
[0092] A verification unit 502, configured to verify whether the first long connection is valid;
[0093] The establishing unit 503 is used to establish a second long connection between the first microservice and the second microservice if not, put the second long connection into a connection pool, and control the first microservice to transmit target data to the second microservice through the second long connection.
[0094] In this embodiment, the functions performed by each unit are similar to those in the above-mentioned Figure 1 The steps in the method embodiment shown correspond to each other and will not be described in detail here.
[0095] The following is a detailed description of a long connection management device provided by this application. Figure 6 , Figure 6 Another embodiment of a long connection management device provided by the present application includes:
[0096] The transmission unit 601 is used to obtain a first long connection between the first microservice and the second microservice from a connection pool when the first microservice needs to transmit target data to the second microservice;
[0097] A verification unit 602, configured to verify whether the first long connection is valid;
[0098] The establishing unit 603 is used to establish a second long connection between the first microservice and the second microservice if not, put the second long connection into a connection pool, and control the first microservice to transmit target data to the second microservice through the second long connection.
[0099] Optionally, the long connection management device further includes:
[0100] The control unit 604 is configured to, if yes, control the first microservice to transmit the target data to the second microservice through the first long connection.
[0101] Optionally, the long connection management device further includes:
[0102] A deleting unit 605, configured to delete the first long connection;
[0103] The updating unit 606 is used to update the connection pool.
[0104] Optionally, the verification unit 602 is specifically configured to:
[0105] Control the first microservice to transmit a first heartbeat message to the second microservice through the first long connection;
[0106] Determine whether the first heartbeat message is transmitted successfully;
[0107] If yes, it is determined that the first long connection is valid;
[0108] If not, it is determined that the first long connection is invalid.
[0109] Optionally, the verification unit 602 is specifically configured to:
[0110] Control the first microservice to transmit a first heartbeat message in a preset format to the second microservice through the first long connection, where the preset format is a message format that can be parsed by the second microservice.
[0111] Optionally, the verification unit 602 is specifically configured to:
[0112] If not, control the first microservice to transmit a second heartbeat message to the second microservice through the first long connection;
[0113] Determine whether the second heartbeat message is transmitted successfully;
[0114] If not, it is determined that the first long connection is invalid.
[0115] This application also provides a long connection management device, see Figure 7 , Figure 7 An embodiment of a long connection management device provided in the present application includes:
[0116] Processor 701, memory 702, input and output unit 703, bus 704;
[0117] The processor 701 is connected to the memory 702, the input and output unit 703 and the bus 704;
[0118] The memory 702 stores a program, and the processor 701 calls the program to execute any of the above long connection management methods.
[0119] The present application also relates to a computer-readable storage medium, on which a program is stored. When the program runs on a computer, the computer executes any of the above long connection management methods.
[0120] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0121] 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 units is only a logical function division. There may be other division methods in actual implementation, such as multiple units 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 an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0122] 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.
[0123] 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. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0124] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable 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: U disk, mobile hard disk, read-only memory (ROM, read-only memory), random access memory (RAM, random access memory), disk or optical disk and other media that can store program code.
Claims
1. A method for managing a long connection, characterized in that: The long connection management method comprises: When the first microservice needs to transmit target data to the second microservice, a first long connection between the first microservice and the second microservice is obtained from the connection pool; Verifying whether the first long connection is valid; If not, a second long connection between the first microservice and the second microservice is established, and the second long connection is placed in the connection pool, and the first microservice is controlled to transmit target data to the second microservice through the second long connection.
2. The method for managing long connections according to claim 1, characterized in that: After verifying whether the first long connection is valid, the long connection management method further includes: If so, control the first microservice to transmit target data to the second microservice through the first long connection.
3. The method for managing long connections according to claim 1, characterized in that: In the if-not case, a second long connection between the first microservice and the second microservice is established, and the second long connection is placed in the connection pool, and after the first microservice is controlled to transmit target data to the second microservice through the second long connection, the long connection management method further includes: Deleting the first long connection; The connection pool is updated.
4. The method for managing long connections according to claim 1, characterized in that: The verifying whether the first long connection is valid includes: Control the first microservice to transmit a first heartbeat message to the second microservice through the first long connection; Determine whether the first heartbeat message is transmitted successfully; If yes, it is determined that the first long connection is valid; If not, it is determined that the first long connection is invalid.
5. The method for managing long connections according to claim 4, characterized in that: The controlling the first microservice to transmit the first heartbeat message to the second microservice through the first long connection includes: Control the first microservice to transmit a first heartbeat message in a preset format to the second microservice through the first long connection, where the preset format is a message format that can be parsed by the second microservice.
6. The method for managing long connections according to claim 4, characterized in that: If not, determining that the first long connection is invalid includes: If not, control the first microservice to transmit a second heartbeat message to the second microservice through the first long connection; Determine whether the second heartbeat message is transmitted successfully; If not, it is determined that the first long connection is invalid.
7. The method for managing a long connection according to any one of claims 1 to 6, characterized in that: The first long connection and the second long connection are http long connections.
8. A long connection management device, characterized in that: The long connection management method and device comprises: A transmission unit, configured to obtain a first long connection between the first microservice and the second microservice from a connection pool when the first microservice needs to transmit target data to the second microservice; A verification unit, used to verify whether the first long connection is valid; and an establishing unit, configured to, if not, establish a second long connection between the first microservice and the second microservice, put the second long connection into a connection pool, and control the first microservice to transmit target data to the second microservice through the second long connection.
9. A long connection management device, characterized in that: The long connection management device comprises: Processor, memory, input-output unit, and bus; The processor is connected to the memory, the input and output unit, and the bus; The memory stores a program, and the processor calls the program to execute the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a program stored thereon, wherein the program, when executed on a computer, performs the method according to any one of claims 1 to 7.
