A method and system for microservice link tracking
By generating a unique TraceID in the microservice and passing it to each microservice, the problem of non-unique TraceID in asynchronous calling methods and asynchronous printing logs is solved, and effective tracking and troubleshooting of microservice links are realized.
Patent Information
- Application Number
- CN202011216196.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-04
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2040-11-04
AI Technical Summary
When applying microservices, in the asynchronous calling method and the asynchronous log printing method, the problem that TraceID is not unique makes it impossible to perform link tracing on the microservices involved in the request task.
At the beginning of the request task, the service consumer generates a unique TraceID and passes it to each microservice involved in the execution of the request task, so that the TraceID of each microservice is the same, thereby realizing the request task execution process based on the TraceID. Perform link tracking on the microservices involved. This method includes intercepting service requests, determining the existence of TraceID, writing TraceID, sending requests, obtaining log information, and using the ELK tool to extract and display link tracing results.
It realizes the link tracking of microservices involved in the execution of request tasks, solves the problem of non-unique TraceID, ensures the uniqueness of TraceID during the execution of request tasks, and can effectively carry out microservice link tracking and troubleshooting.
Smart Images

Figure CN112286776B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a method and system for microservice link tracking. Background Art
[0002] Microservices are a software architecture that is gradually being widely used. In the process of applying microservices, the service consumer usually calls the service provider in an asynchronous way, and when printing the logs of microservices, they usually use asynchronous printing to print the logs.
[0003] However, in a request task, both the asynchronous call method and the asynchronous log printing method have the problem that the TraceID (tracing ID) in the request task is not unique, so it is impossible to perform corresponding link tracking on the microservices involved in the request task. Summary of the invention
[0004] In view of this, an embodiment of the present invention provides a method and system for microservice link tracing to implement link tracing of microservices involved in a request task.
[0005] To achieve the above objectives, the embodiments of the present invention provide the following technical solutions:
[0006] A first aspect of an embodiment of the present invention discloses a method for microservice link tracing, the method comprising:
[0007] Intercepting a service request sent by a service consumer to a service provider in a request task, wherein the service request includes a request context and a mapping diagnostic context MDC corresponding to the service consumer, and the service consumer and the service provider are microservices;
[0008] Determine whether there is a trace ID in the service request, where the trace ID is a unique identifier generated by the service consumer when the request task starts and written into the service request by the service consumer;
[0009] If so, sending the service request to the service provider, so that the service provider responds to the service request and writes the TraceID into its corresponding MDC;
[0010] Obtaining first log information of the service consumer and second log information of the service provider corresponding to the TraceID, wherein the first log information is generated by the service consumer according to the TraceID in the MDC corresponding to the service consumer, and the second log information is generated by the service provider according to the TraceID in the MDC corresponding to the service consumer;
[0011] According to the first log information and the second log information, link tracing is performed on the microservice involved in the request task.
[0012] Preferably, the obtaining of the first log information of the service consumer and the second log information of the service provider corresponding to the TraceID includes:
[0013] Using the ELK tool, extract the first log information of the service consumer corresponding to the TraceID, and extract the second log information of the service provider corresponding to the TraceID.
[0014] Preferably, it also includes:
[0015] If the TraceID does not exist in the service request, the TraceID is written into the service request, and the step of sending the service request to the service provider is performed.
[0016] Preferably, after performing link tracing on the microservice involved in the request task according to the first log information and the second log information, the method further includes:
[0017] The Kibana tool is used to display the link tracing results of the microservice corresponding to the TraceID in a graphical interface.
[0018] A second aspect of an embodiment of the present invention discloses a system for microservice link tracking, the system comprising:
[0019] An interception unit, used to intercept a service request sent by a service consumer to a service provider in a request task, wherein the service request includes a request context and a mapping diagnostic context MDC corresponding to the service consumer, and the service consumer and the service provider are microservices;
[0020] A determination unit, configured to determine whether a trace ID exists in the service request, and if so, execute a sending unit, wherein the trace ID is a unique identifier generated by the service consumer end when the request task starts and written into the service request by the service consumer end;
[0021] The sending unit is used to send the service request to the service provider, so that the service provider responds to the service request and writes the TraceID into the MDC corresponding to itself;
[0022] An acquisition unit, used to acquire first log information of the service consumer end and second log information of the service provider end corresponding to the TraceID, wherein the first log information is generated by the service consumer end according to the TraceID in the MDC corresponding to the service consumer end, and the second log information is generated by the service provider end according to the TraceID in the MDC corresponding to the service consumer end;
[0023] The tracking unit is used to track the link of the microservice involved in the request task according to the first log information and the second log information.
[0024] Preferably, the acquisition unit is specifically used to: use an ELK tool to extract first log information of the service consumer corresponding to the TraceID, and extract second log information of the service provider corresponding to the TraceID.
[0025] Preferably, the system further comprises:
[0026] A writing unit is used to write the TraceID into the service request if the TraceID does not exist in the service request, and execute the sending unit.
[0027] Preferably, the system further comprises:
[0028] The display unit is used to display the link tracing result of the microservice corresponding to the TraceID in a graphical interface by using the Kibana tool.
[0029] A third aspect of an embodiment of the present invention discloses an electronic device, characterized in that it includes:
[0030] Memory and processor;
[0031] Wherein, the memory is used to store programs;
[0032] The processor is used to execute the program. When the program is executed, it is specifically used to implement the microservice link tracking method disclosed in the first aspect of the embodiment of the present invention.
[0033] A fourth aspect of an embodiment of the present invention discloses a computer storage medium for storing a computer program. When the computer program is executed, it is used to implement the method for microservice link tracking disclosed in the first aspect of the embodiment of the present invention.
[0034] Based on the above-mentioned embodiment of the present invention, a method and system for microservice link tracking is provided, the method is: intercepting the service request sent by the service consumer to the service provider in the request task; determining whether there is a TraceID in the service request; if so, sending the service request to the service provider, so that the service provider responds to the service request and writes the TraceID into its corresponding MDC; obtaining the first log information of the service consumer corresponding to the TraceID and the second log information of the service provider; based on the first log information and the second log information, link tracking is performed on the microservice involved in the request task. In this scheme, a unique TraceID is generated by the service consumer at the beginning of the request task, and the TraceID is passed to each microservice involved in the execution of the request task, so that the TraceID of each microservice involved in the execution of the request task is the same, thereby realizing link tracking of the microservices involved in the execution of the request task according to the TraceID. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying creative work.
[0036] Figure 1 A flowchart of a method for microservice link tracing provided by an embodiment of the present invention;
[0037] Figure 2 A structural block diagram of a microservice link tracking system provided by an embodiment of the present invention;
[0038] Figure 3 Another structural block diagram of a microservice link tracking system provided by an embodiment of the present invention;
[0039] Figure 4 Another structural block diagram of a microservice link tracking system provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0040] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0041] In this application, the terms "comprises", "comprising" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprising a ..." does not exclude the presence of other identical elements in the process, method, article or device comprising the element.
[0042] As can be seen from the background technology, currently in the process of applying microservices, the application methods usually involve asynchronous calling methods and asynchronous logging methods, but in a request task, the above two methods have the problem of non-unique TraceID, so it is impossible to perform corresponding link tracking on the microservices involved in the request task.
[0043] The embodiment of the present invention provides a method and system for microservice link tracing. When a request task starts, a unique TraceID is generated by a service consumer, and the TraceID is passed to each microservice involved in the execution of the request task, so that the TraceID of each microservice involved in the execution of the request task is the same, so as to realize link tracing of the microservices involved in the execution of the request task according to the TraceID.
[0044] See also Figure 1 , shows a flow chart of a method for microservice link tracing provided by an embodiment of the present invention, the method comprising:
[0045] Step S101: intercepting a service request sent by a service consumer to a service provider in a request task.
[0046] It should be noted that the service request includes the request context and mapped diagnostic context (MDC) corresponding to the service consumer. The service consumer and the service provider are microservices. That is, for a microservice, depending on different application scenarios, the microservice can be a service consumer or a service provider.
[0047] It should be further explained that, in an embodiment of the present invention, each microservice is developed using the Dubbo microservice architecture, in which RpcContext is used to store the request context of the microservice, and the logs corresponding to each microservice are recorded in the form of the Logback log framework.
[0048] The request context is equivalent to a request object, which contains the request parameter information of the current service request. The request context is only available in the current service request. When the service request ends, the request context also disappears.
[0049] In the specific implementation process of step S101, in the request task, when the service consumer sends a service request to the service provider, that is, when the service consumer needs to call the service provider, a service interceptor (such as a Filter interceptor) is used to intercept the service request before calling the service provider.
[0050] Step S102: Determine whether there is a TraceID in the service request. If not, execute step S103; if so, execute step S104.
[0051] It should be noted that TraceID is a unique identifier generated by the service consumer when the request task starts and is written into the service request by the service consumer.
[0052] It can be understood that from the foregoing content, both the service consumer and the service provider are microservices, that is, in a request task that requires calling multiple microservices, the service provider can also be transformed into a service consumer to call other microservices.
[0053] TraceID is the TraceID generated by the first service consumer corresponding to the request task (the unique TraceID corresponding to the request task), and then written by the first service consumer into the request context and MDC in the corresponding service request. In other words, in a request task, before the first call to the microservice, the service consumer (the first service consumer to call the microservice) generates the unique TraceID corresponding to the request task, and writes the TraceID into the request context and MDC corresponding to the service consumer.
[0054] It should be noted that TraceID is a unique 32-bit string consisting of letters and numbers. In a request task, when the service consumer calls the service provider (that is, one microservice calls another microservice), the TraceID is passed to the subsequent microservices, and only the unique TraceID is used in the request task.
[0055] It should be further explained that when the service consumer writes the TraceID into the request context and MDC, the TraceID is written into the request context in the form of key-value. Similarly, the TraceID is also written into the MDC.
[0056] As can be seen from the above content, it is necessary to ensure that only a unique TraceID is used in the request task. In the specific implementation of step S102, after intercepting the service request, determine whether the TraceID exists in the request context and MDC in the service request. If it exists, execute the subsequent steps. If not, write the TraceID into the request context and MDC to ensure that the service request contains the TraceID (unique).
[0057] It is understandable that in the process of executing the request task, microservices are usually called in a multi-level service call mode. Each time before the service consumer calls the service provider, the service consumer needs to write the TraceID into its corresponding request context and MDC. For example:
[0058] During the execution of the request task, microservice A calls microservice B, and microservice B calls microservice C. When microservice A (the service consumer at this time) calls microservice B (the service provider at this time), microservice A needs to write the TraceID into the request context and MDC corresponding to microservice A. When microservice B (the service consumer at this time) calls microservice C (the service provider at this time), microservice B needs to write the TraceID (consistent with the TraceID of microservice A) into the request context and MDC corresponding to microservice B.
[0059] To ensure the uniqueness of the TraceID during the execution of the request task, it is necessary to ensure that the service request contains the TraceID every time the service consumer calls the service provider.
[0060] Step S103: Write the TraceID into the service request and execute step S104.
[0061] In the specific implementation of step S103 , after intercepting the service request, if it is checked and determined that the request context and MDC in the service request do not contain the TraceID, the TraceID is written into the request context and the MDC.
[0062] Step S104: Send the service request to the service provider, so that the service provider responds to the service request and writes the TraceID into its corresponding MDC.
[0063] In the specific implementation of step S104, after ensuring that the service request contains the TraceID, the service request is sent to the service provider, so that the service provider responds to the service request.
[0064] At the same time, the service provider obtains the TraceID from the request context of the service request and records the TraceID in its corresponding MDC. That is, when the service provider needs to call other microservices, the service provider is the service consumer. When calling other microservices, the service provider (equivalent to the service consumer at this time) uses the TraceID to send the corresponding service request to the called microservice (equivalent to the service provider at this time). For specific content, please refer to the content of the above steps, which will not be repeated here.
[0065] Step S105: Acquire the first log information of the service consumer and the second log information of the service provider corresponding to the TraceID.
[0066] It should be noted that the first log information is generated by the service consumer according to the TraceID in the MDC corresponding to itself, and the second log information is generated by the service provider according to the TraceID in the MDC corresponding to itself.
[0067] For the service consumer or service provider, the corresponding value is obtained according to the key of TraceID in the corresponding MDC, thereby obtaining the TraceID. The service consumer or service provider prints the log according to the obtained TraceID, that is, uses the Logback log framework to store the log information containing TraceID into the corresponding log file. The log information includes at least: TraceID, server timestamp, server IP, server application name, class information, thread information and process information.
[0068] The server timestamp refers to the time of the server of the current application, the server IP refers to the IP of the server of the current application, and the server application name is the name set when the server is deployed. Each server has a unique name.
[0069] In the specific implementation of step S105, the ELK (Elasticsearch, Logstash and Kibana) tool is used to extract the first log information of the service consumer corresponding to the TraceID, and the second log information of the service provider corresponding to the TraceID is extracted.
[0070] That is to say, all log information containing the TraceID is extracted, including the log information containing the TraceID corresponding to the service consumer and the service provider. For example, FileBeat in the ELK log framework is used to collect all log information containing the TraceID.
[0071] Step S106: According to the first log information and the second log information, link tracing is performed on the microservices involved in the request task.
[0072] It should be noted that link tracking means: in a request task, from the beginning to the end of the request task, all systems called during the execution of the request task and the time consumed are determined. According to the link tracking results, the problem can be further tracked and investigated.
[0073] It can be understood that from the foregoing content, the first log information and the second log information are all log information containing TraceID in the request task, and the first log information and the second log information both contain information such as TraceID, server timestamp, server IP and server application name.
[0074] It should be noted that for a request task, the link route during the execution of the request task is fixed. The server timestamp in the log information containing TraceID can reflect the sequence of the microservices called during the execution process, and the server IP and server application name can reflect which microservice is called during the execution process.
[0075] Therefore, in the specific implementation of step S106, the first log information and the second log information collected according to TraceID, that is, all log information containing TraceID, are used to determine the microservices called during the execution of the request task and the calling time information, and the corresponding link tracking results can be generated in combination with TraceID.
[0076] In other words, TraceID can be used to track the microservices called during the execution of the request task and the calling time information, so as to realize link tracking of the microservices involved in the execution of the request task.
[0077] Preferably, after using TraceID to perform link tracing on the microservices involved in the execution of the request task, the Kibana tool is used to display the link tracing results of the microservices corresponding to the TraceID in a graphical interface.
[0078] In the specific implementation, FileBeat can be used to collect all log information, and then Logstash can be used to filter all log information according to TraceID (the TraceID that needs to be tracked) to obtain all log information (first log information and second log information) containing the TraceID. All log information containing the TraceID is collected in Elasticsearch, and then the link tracking results of the TraceID are viewed through Kibana (a graphical interface tool).
[0079] Through the above method, when applying microservices in request tasks, the problem of non-unique TraceID in asynchronous calling and asynchronous log printing can be solved.
[0080] In an embodiment of the present invention, a unique TraceID is generated by the service consumer at the beginning of a request task, and the TraceID is passed to each microservice involved in the execution of the request task, so that the TraceID of each microservice involved in the execution of the request task is the same, thereby realizing link tracking of the microservices involved in the execution of the request task according to the TraceID.
[0081] Corresponding to the method for microservice link tracing provided in the above embodiment of the present invention, see Figure 2 , an embodiment of the present invention also provides a structural block diagram of a system for microservice link tracing, the system comprising: an interception unit 201, a determination unit 202, a sending unit 203, an acquisition unit 204 and a tracing unit 205;
[0082] The interception unit 201 is used to intercept the service request sent by the service consumer to the service provider in the request task. The service request includes the request context and MDC corresponding to the service consumer. The service consumer and the service provider are microservices.
[0083] The determining unit 202 is used to determine whether there is a TraceID in the service request. If so, the sending unit 203 is executed. The TraceID is a unique identifier generated by the service consumer when the request task starts and written into the service request by the service consumer.
[0084] The sending unit 203 is used to send the service request to the service provider, so that the service provider responds to the service request and writes the TraceID into the MDC corresponding to itself.
[0085] The acquisition unit 204 is used to obtain the first log information of the service consumer end and the second log information of the service provider end corresponding to the TraceID. The first log information is generated by the service consumer end according to the TraceID in the MDC corresponding to itself, and the second log information is generated by the service provider end according to the TraceID in the MDC corresponding to itself.
[0086] In a specific implementation, the acquisition unit 204 is specifically used to: use the ELK tool to extract the first log information of the service consumer corresponding to the TraceID, and to extract the second log information of the service provider corresponding to the TraceID.
[0087] The tracking unit 205 is used to track the link of the microservice involved in the request task according to the first log information and the second log information.
[0088] In an embodiment of the present invention, a unique TraceID is generated by the service consumer at the beginning of a request task, and the TraceID is passed to each microservice involved in the execution of the request task, so that the TraceID of each microservice involved in the execution of the request task is the same, thereby realizing link tracking of the microservices involved in the execution of the request task according to the TraceID.
[0089] Preferably, combined Figure 2 , see Figure 3 , shows another structural block diagram of a system for microservice link tracing provided by an embodiment of the present invention, the system further comprising:
[0090] The writing unit 206 is used to write the TraceID into the service request if the TraceID does not exist in the service request, and execute the sending unit 203 .
[0091] Preferably, combined Figure 2 , see Figure 4 , shows another structural block diagram of a system for microservice link tracking provided by an embodiment of the present invention, the system further comprising:
[0092] The display unit 207 is used to display the link tracing result of the microservice corresponding to the TraceID in a graphical interface by using the Kibana tool.
[0093] Preferably, an embodiment of the present invention further provides an electronic device, comprising: a memory and a processor, wherein the memory is used to store programs, and the processor is used to execute programs, and when the program is executed, it is specifically used to implement the method for microservice link tracking provided in the above-mentioned embodiment of the present invention.
[0094] Preferably, an embodiment of the present invention further provides a computer storage medium for storing a computer program, which, when executed, is used to implement the method for microservice link tracking provided in the above-mentioned embodiment of the present invention.
[0095] In summary, the embodiments of the present invention provide a method and system for microservice link tracing. When a request task starts, a unique TraceID is generated by a service consumer, and the TraceID is passed to each microservice involved in the execution of the request task, so that the TraceID of each microservice involved in the execution of the request task is the same, thereby realizing link tracing of the microservices involved in the execution of the request task according to the TraceID.
[0096] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can refer to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system or system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can refer to the partial description of the method embodiment. The system and system embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, 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. Ordinary technicians in this field can understand and implement it without creative work.
[0097] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may 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 the present invention.
[0098] The above description of the disclosed embodiments enables one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for microservice link tracking, characterized in that: The method comprises: Intercepting a service request sent by a service consumer to a service provider in a request task, wherein the service request includes a request context and a mapping diagnostic context MDC corresponding to the service consumer, and the service consumer and the service provider are microservices; Determine whether there is a trace ID in the service request, where the trace ID is a unique identifier generated by the service consumer when the request task starts and written into the service request by the service consumer; If so, sending the service request to the service provider, so that the service provider responds to the service request and writes the TraceID into its corresponding MDC; Obtaining first log information of the service consumer and second log information of the service provider corresponding to the TraceID, wherein the first log information is generated by the service consumer according to the TraceID in the MDC corresponding to the service consumer, and the second log information is generated by the service provider according to the TraceID in the MDC corresponding to the service consumer; According to the first log information and the second log information, link tracing is performed on the microservice involved in the request task, and the first log information and the second log information both contain TraceID, server timestamp, server IP and server application name information.
2. The method according to claim 1, characterized in that The obtaining of the first log information of the service consumer and the second log information of the service provider corresponding to the TraceID includes: Using the ELK tool, extract the first log information of the service consumer corresponding to the TraceID, and extract the second log information of the service provider corresponding to the TraceID.
3. The method according to claim 1, characterized in that Also includes: If the TraceID does not exist in the service request, the TraceID is written into the service request, and the step of sending the service request to the service provider is performed.
4. The method according to claim 1, characterized in that: After performing link tracing on the microservice involved in the request task according to the first log information and the second log information, the method further includes: The Kibana tool is used to display the link tracing results of the microservice corresponding to the TraceID in a graphical interface.
5. A microservice link tracking system, characterized in that: The system comprises: An interception unit, used to intercept a service request sent by a service consumer to a service provider in a request task, wherein the service request includes a request context and a mapping diagnostic context MDC corresponding to the service consumer, and the service consumer and the service provider are microservices; A determining unit, configured to determine whether a trace ID exists in the service request, and if so, to execute a sending unit, wherein the trace ID is a unique identifier generated by the service consumer end when the request task starts and written into the service request by the service consumer end; The sending unit is used to send the service request to the service provider, so that the service provider responds to the service request and writes the TraceID into the MDC corresponding to itself; An acquisition unit, used to acquire first log information of the service consumer end and second log information of the service provider end corresponding to the TraceID, wherein the first log information is generated by the service consumer end according to the TraceID in the MDC corresponding to the service consumer end, and the second log information is generated by the service provider end according to the TraceID in the MDC corresponding to the service consumer end; A tracing unit is used to perform link tracing on the microservice involved in the request task according to the first log information and the second log information, wherein the first log information and the second log information both contain TraceID, server timestamp, server IP and server application name information.
6. The system according to claim 5, characterized in that The acquisition unit is specifically used to: use the ELK tool to extract the first log information of the service consumer corresponding to the TraceID, and extract the second log information of the service provider corresponding to the TraceID.
7. The system according to claim 5, characterized in that The system further comprises: A writing unit is used to write the TraceID into the service request if the TraceID does not exist in the service request, and execute the sending unit.
8. The system according to claim 5, characterized in that The system further comprises: The display unit is used to display the link tracing result of the microservice corresponding to the TraceID in a graphical interface by using the Kibana tool.
9. An electronic device, characterized in that: include: Memory and processor; Wherein, the memory is used to store programs; The processor is used to execute the program, and when the program is executed, it is specifically used to implement the method for microservice link tracking according to any one of claims 1 to claim 4.
10. A computer storage medium, characterized in that: Used to store a computer program, which, when executed, is used to implement the method for microservice link tracing as claimed in any one of claims 1 to 4.
