当前位置:网站首页>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
边栏推荐
- CTF daily question day44 rot
- Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
- Model analysis of establishment time and holding time
- LeetCode 斐波那契序列
- Calculate sha256 value of data or file based on crypto++
- [groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
- Leetcode 44 Wildcard matching (2022.02.13)
- Extracting profile data from profile measurement
- 【DesignMode】组合模式(composite mode)
- Mysql - CRUD
猜你喜欢

Date类中日期转成指定字符串出现的问题及解决方法

剖面测量之提取剖面数据

uniapp开发,打包成H5部署到服务器

Extension and application of timestamp

如何解决ecology9.0执行导入流程流程产生的问题

Analysis of the combination of small program technology advantages and industrial Internet

Arduino hexapod robot
![Atcoder beginer contest 258 [competition record]](/img/e4/1d34410f79851a7a81dd8f4a0b54bf.gif)
Atcoder beginer contest 258 [competition record]
![Atcoder beginer contest 254 [VP record]](/img/13/656468eb76bb8b6ea3b6465a56031d.png)
Atcoder beginer contest 254 [VP record]

Search (DFS and BFS)
随机推荐
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
Extracting profile data from profile measurement
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
An understanding of & array names
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
Folding and sinking sand -- weekly record of ETF
Set data real-time update during MDK debug
Yolov5, pychar, Anaconda environment installation
孤勇者
Data analysis thinking analysis methods and business knowledge - analysis methods (III)
STM32按键消抖——入门状态机思维
Solve the problem of reading Chinese garbled code in sqlserver connection database
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
Single source shortest path exercise (I)
KDD 2022 | 脑电AI助力癫痫疾病诊断
XML配置文件
Codeforces round 804 (Div. 2) [competition record]
Introduction of motor
Notepad + + regular expression replace String