当前位置:网站首页>Starting from 1.5, build a micro Service Framework - call chain tracking traceid
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
2022-07-06 00:36:00 【InfoQ】
Preface
1Core functions
- Split multiple micro service modules , Draw out one demo Microservice module for expansion , Completed
- Extract core framework modules , Completed
- Registry Center Eureka, Completed
- The remote invocation OpenFeign, Completed
- journal logback, contain traceId track , Completed
- Swagger API file , Completed
- Configure file sharing , Completed
- Log retrieval ,ELK Stack, Completed
- Customize Starter, undetermined
- Consolidated cache Redis,Redis Sentinel high availability , Completed
- Consolidate databases MySQL,MySQL High availability , Completed
- Integrate MyBatis-Plus, Completed
- Link tracking component , undetermined
- monitor , undetermined
- Tool class , To be developed
- gateway , Technology selection to be determined
- Audit log entry ES, undetermined
- distributed file system , undetermined
- Timing task , undetermined
- wait
One 、 Pain points
It hurts a little : Multiple logs in the process cannot be traced

Pain point two : How to correlate logs across Services

Pain point three : How to correlate logs across threads

It hurts four : The third party calls our service , How to track ?
Two 、 programme
1.1 Solution
1.2 MDC programme
3、 ... and 、 Principle and practice
2.1 Track multiple logs of a request

<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{traceId} %-5level %logger - %msg%n</pattern>
headertraceId
Sample code
/**
* @author www.passjava.cn, official account : Wukong chat structure
* @date 2022-07-05
*/
@Service
public class LogInterceptor extends HandlerInterceptorAdapter {
private static final String TRACE_ID = "traceId";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String traceId = request.getHeader(TRACE_ID);
if (StringUtils.isEmpty(traceId)) {
MDC.put("traceId", UUID.randomUUID().toString());
} else {
MDC.put(TRACE_ID, traceId);
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// Prevent memory leaks
MDC.remove("traceId");
}
}
/**
* @author www.passjava.cn, official account : Wukong chat structure
* @date 2022-07-05
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Resource
private LogInterceptor logInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(logInterceptor).addPathPatterns("/**");
}
}
2.2 Track multiple logs across Services

/**
* @author www.passjava.cn, official account : Wukong chat structure
* @date 2022-07-05
*/
@Configuration
public class FeignInterceptor implements RequestInterceptor {
private static final String TRACE_ID = "traceId";
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header(TRACE_ID, (String) MDC.get(TRACE_ID));
}
}

Four 、 summary
边栏推荐
- Ffmpeg captures RTSP images for image analysis
- Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
- MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
- STM32按键消抖——入门状态机思维
- How spark gets columns in dataframe --column, $, column, apply
- XML Configuration File
- 多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
- 电机的简介
- MySql——CRUD
- Reading notes of the beauty of programming
猜你喜欢
![[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)](/img/09/9076de099147b2d0696fe979a68ada.jpg)
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)

《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network

Idea remotely submits spark tasks to the yarn cluster

Leetcode:20220213 week race (less bugs, top 10% 555)

电机的简介

Set data real-time update during MDK debug

MySQL storage engine

Go learning - dependency injection

Hudi of data Lake (1): introduction to Hudi

How to make your own robot
随机推荐
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
uniapp开发,打包成H5部署到服务器
Common API classes and exception systems
JS can really prohibit constant modification this time!
LeetCode 1598. Folder operation log collector
Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
数据分析思维分析方法和业务知识——分析方法(二)
MYSQL GROUP_ The concat function realizes the content merging of the same ID
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL storage engine
Search (DFS and BFS)
【线上小工具】开发过程中会用到的线上小工具合集
看抖音直播Beyond演唱会有感
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
synchronized 和 ReentrantLock
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
MDK debug时设置数据实时更新