当前位置:网站首页>从 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));
}
}
四、总结
边栏推荐
- What is information security? What is included? What is the difference with network security?
- Mysql - CRUD
- [Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
- Atcoder beginer contest 258 [competition record]
- 2022.7.5-----leetcode. seven hundred and twenty-nine
- STM32 key chattering elimination - entry state machine thinking
- Browser local storage
- Permission problem: source bash_ profile permission denied
- Spark获取DataFrame中列的方式--col,$,column,apply
- AtCoder Beginner Contest 258【比赛记录】
猜你喜欢
Set data real-time update during MDK debug
The relationship between FPGA internal hardware structure and code
Tools to improve work efficiency: the idea of SQL batch generation tools
OS i/o devices and device controllers
MySQL functions
FPGA内部硬件结构与代码的关系
Browser reflow and redraw
Go learning - dependency injection
Go learning --- structure to map[string]interface{}
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
随机推荐
Pointer pointer array, array pointer
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
LeetCode 6006. Take out the least number of magic beans
【线上小工具】开发过程中会用到的线上小工具合集
About the slmgr command
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
LeetCode 6004. Get operands of 0
PHP determines whether an array contains the value of another array
Arduino六足机器人
SQLServer连接数据库读取中文乱码问题解决
Browser local storage
Model analysis of establishment time and holding time
LeetCode 6005. The minimum operand to make an array an alternating array
What is information security? What is included? What is the difference with network security?
免费的聊天机器人API
多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
The relationship between FPGA internal hardware structure and code
如何解决ecology9.0执行导入流程流程产生的问题
Key structure of ffmpeg -- AVCodecContext
2022.7.5-----leetcode. seven hundred and twenty-nine