First, the role of call chain tracing
Call chain tracing includes 1. Call chain from front to back 2. Call chain between methods within a single service 3. Call chain between microservices 4. Call chain between application service and database 5. Call chain between application service and third-party service, such as Redis and MQ
The functions of call chain tracing are as follows: 1. Locate the fault point when multiple microservice invocation failures occur. Performance analysis 3.SQL analysis 4. Method or service invocation rationality analysis 5. Call logic and data flow analysis, this is especially useful for newcomers, looking at the call chain and executing SQL to see what is being done
Second, the need to call chain trace
So what are the requirements involved in call chain tracing? Look at the figure:
Key technologies involved in Java call chain tracing
- Invoke message passing, which can be passed through HTTP headers
- The front end is just set up when the request is sent. However, it is generally not possible to track the call time of the front end because there would be one more network interaction.
- In Spring, the RestTemplate interceptor and Fegin interceptor can be written to intercept request requests and set the invocation chain information in the message header.
- Spring developers would naturally want to use AOP to intercept the acquisition of the call stack in microservices, but spring AOP is inconvenient and intrusive to intercept calls between multiple methods of the same class, so it is not suitable. The best way is to use JavaAgent and use code injection to intercept.
- The internal invocation information of microservices needs to be passed through thread-wide variables, and ThreadLocal in Java already provides this capability.
- Call stack tracing Java also provides the ability to get call stack information for the current method.
- SQL monitoring, for the use of MyBatis will think of using MyBatis interceptor, this way in some cases will not work (reasons are not clear), and rely on MyBatis, so the application of more low-level methods to intercept SQL, one way is to implement JDBC proxy class, Another approach is to use third-party open source SQL monitoring tools. end.
Wechat Official Account:
Join “Java Stack actual combat camp” knowledge planet, participate in the discussion, more actual combat code to share! t.zsxq.com/RNzfi2j