当前位置:网站首页>从 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>
header
traceId

示例代码
/**
* @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));
}
}

四、总结
边栏推荐
- Leetcode 450 deleting nodes in a binary search tree
- LeetCode 6004. Get operands of 0
- [Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
- notepad++正則錶達式替換字符串
- 《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
- Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
- LeetCode 8. String conversion integer (ATOI)
- [simple implementation of file IO]
- Key structure of ffmpeg - avframe
- MySQL storage engine
猜你喜欢
Free chat robot API
The relationship between FPGA internal hardware structure and code
Atcoder beginer contest 258 [competition record]
Problems and solutions of converting date into specified string in date class
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
Spark SQL空值Null,NaN判断和处理
Tools to improve work efficiency: the idea of SQL batch generation tools
Leetcode 450 deleting nodes in a binary search tree
Model analysis of establishment time and holding time
随机推荐
【线上小工具】开发过程中会用到的线上小工具合集
时间戳的拓展及应用实例
STM32 configuration after chip replacement and possible errors
Idea远程提交spark任务到yarn集群
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
Folding and sinking sand -- weekly record of ETF
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
How to make your own robot
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
[designmode] Decorator Pattern
Calculate sha256 value of data or file based on crypto++
uniapp开发,打包成H5部署到服务器
Spark AQE
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
剖面测量之提取剖面数据
Pointer - character pointer
NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
MySQL之函数
FFmpeg抓取RTSP图像进行图像分析