当前位置:网站首页>从 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));
}
}

四、总结
边栏推荐
- Calculate sha256 value of data or file based on crypto++
- DEJA_ Vu3d - cesium feature set 055 - summary description of map service addresses of domestic and foreign manufacturers
- Set data real-time update during MDK debug
- Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
- Pointer pointer array, array pointer
- Spark-SQL UDF函数
- MySQL functions
- 如何制作自己的機器人
- Folding and sinking sand -- weekly record of ETF
- 《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
猜你喜欢

How to solve the problems caused by the import process of ecology9.0

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

FFmpeg抓取RTSP图像进行图像分析

Browser reflow and redraw

Problems and solutions of converting date into specified string in date class

MySQL storage engine

免费的聊天机器人API

猿桌派第三季开播在即,打开出海浪潮下的开发者新视野

建立时间和保持时间的模型分析
![N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2](/img/f3/8e237296f5948dd0488441aa625182.jpg)
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
随机推荐
[designmode] composite mode
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
Determinant learning notes (I)
PHP determines whether an array contains the value of another array
Spark DF增加一列
Opencv classic 100 questions
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
Atcoder beginer contest 258 [competition record]
XML Configuration File
LeetCode 斐波那契序列
How to make your own robot
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
7.5 decorator
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
如何制作自己的机器人
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
电机的简介
Calculate sha256 value of data or file based on crypto++
[simple implementation of file IO]