当前位置:网站首页>从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
2022-07-06 00:33:00 【InfoQ】
前言
1核心功能
- 多个微服务模块拆分,抽取出一个 demo 微服务模块供扩展,已完成
- 提取核心框架模块,已完成
- 注册中心 Eureka,已完成
- 远程调用 OpenFeign,已完成
- 日志 logback,包含 traceId 跟踪,已完成
- Swagger API 文档,已完成
- 配置文件共享,已完成
- 日志检索,ELK Stack,已完成
- 自定义 Starter,待定
- 整合缓存 Redis,Redis 哨兵高可用,已完成
- 整合数据库 MySQL,MySQL 高可用,已完成
- 整合 MyBatis-Plus,已完成
- 链路追踪组件,待定
- 监控,待定
- 工具类,待开发
- 网关,技术选型待定
- 审计日志进入 ES,待定
- 分布式文件系统,待定
- 定时任务,待定
- 等等
一、痛点
痛点一:进程内的多条日志无法追踪

痛点二:跨服务的日志如何进行关联

痛点三:跨线程的日志如何关联

痛点四:第三方调用我们的服务,如何追踪?
二、方案
1.1 解决方案
1.2 MDC 方案
三、原理和实战
2.1 追踪一个请求的多条日志

<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{traceId} %-5level %logger - %msg%n</pattern>
headertraceId
示例代码
/**
* @author www.passjava.cn,公众号:悟空聊架构
* @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 {
//防止内存泄露
MDC.remove("traceId");
}
}
/**
* @author www.passjava.cn,公众号:悟空聊架构
* @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 跨服务跟踪多条日志

/**
* @author www.passjava.cn,公众号:悟空聊架构
* @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));
}
}

四、总结
边栏推荐
- [Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
- Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
- MySQL functions
- 猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
- AtCoder Beginner Contest 258【比赛记录】
- [designmode] adapter pattern
- FFmpeg抓取RTSP图像进行图像分析
- Room cannot create an SQLite connection to verify the queries
- Reading notes of the beauty of programming
- STM32 configuration after chip replacement and possible errors
猜你喜欢

MySQL functions
![Atcoder beginer contest 258 [competition record]](/img/e4/1d34410f79851a7a81dd8f4a0b54bf.gif)
Atcoder beginer contest 258 [competition record]

KDD 2022 | 脑电AI助力癫痫疾病诊断

Hudi of data Lake (2): Hudi compilation

Free chat robot API

FPGA内部硬件结构与代码的关系

AtCoder Beginner Contest 258【比赛记录】

Leetcode 450 deleting nodes in a binary search tree

Calculate sha256 value of data or file based on crypto++

anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
随机推荐
Spark DF增加一列
KDD 2022 | 脑电AI助力癫痫疾病诊断
如何制作自己的机器人
Folding and sinking sand -- weekly record of ETF
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
Classical concurrency problem: the dining problem of philosophers
Gd32f4xx UIP protocol stack migration record
建立时间和保持时间的模型分析
Spark获取DataFrame中列的方式--col,$,column,apply
[Online gadgets] a collection of online gadgets that will be used in the development process
Spark DF adds a column
MySQL存储引擎
Pointer pointer array, array pointer
Spark AQE
Leetcode:20220213 week race (less bugs, top 10% 555)
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
Leetcode Fibonacci sequence