分布式链路ID实现

实现原理

api入口或者网关处生成traceId,调用服务时优先检查是否头部带有traceId,有则复用,没有则生成

实现方式

处理api相关traceId

1.通过filter复用或者生成traceId,并且将traceId输入到响应头中

java 复制代码
import java.io.IOException;
import java.util.UUID;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.MDC;
import org.springframework.web.filter.OncePerRequestFilter;

public class TraceIdFilter extends OncePerRequestFilter {
    public TraceIdFilter() {
    }

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String traceId = request.getHeader("X-Trace-Id");
        if (traceId == null) {
            traceId = this.generateTraceId();
        }

        MDC.put("traceId", traceId);
        response.setHeader("X-Trace-Id", traceId);

        try {
            filterChain.doFilter(request, response);
        } finally {
            MDC.clear();
        }

    }

    private String generateTraceId() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}

2、在对应项目中启用filter

java 复制代码
@Configuration
public class WebFilterConfig {

    @Bean
    public FilterRegistrationBean<TraceIdFilter> loggingFilter() {
        FilterRegistrationBean<TraceIdFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new TraceIdFilter());
        registrationBean.addUrlPatterns("/*"); // 只对特定 URL 匹配
        return registrationBean;
    }
}

Feign调用时传递traceId

1、声明拦截器(feign调用时从mdc获取traceId放入请求头中)

java 复制代码
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;

public class FeIgnMCDInterceptor implements RequestInterceptor {
    public FeIgnMCDInterceptor() {
    }

    public void apply(RequestTemplate template) {
        String traceId = MDC.get("traceId");
        if (traceId != null) {
            template.header("X-Trace-Id", new String[]{traceId});
        }

    }
}

2、启用拦截器

java 复制代码
// 任意位置注册为springbean即可
@Bean
public FeIgnMCDInterceptor feIgnMCDInterceptor() {
    return new FeIgnMCDInterceptor();
}

日志配置文件中打印traceId

XML 复制代码
<!-- 日志输出格式 -->
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [${application_name}] [%X{traceId}] [%thread] %-5level %logger - [%method,%line] - %msg%n" />
相关推荐
y***61312 分钟前
SpringBoot集成Flowable
java·spring boot·后端
i***58679 分钟前
springcloud springboot nacos版本对应
spring boot·spring·spring cloud
烤麻辣烫12 分钟前
黑马程序员苍穹外卖(新手)DAY6
java·开发语言·学习·spring·intellij-idea
s***385613 分钟前
SpringBoot中如何手动开启事务
java·spring boot·spring
i***220736 分钟前
springboot整合libreoffice(两种方式,使用本地和远程的libreoffice);docker中同时部署应用和libreoffice
spring boot·后端·docker
q***61411 小时前
Spring中Aware的用法以及实现
java·数据库·spring
代码or搬砖1 小时前
SpringMVC的执行流程
java·spring boot·后端
q***16082 小时前
SpringCloud 系列教程:微服务的未来(二)Mybatis-Plus的条件构造器、自定义SQL、Service接口基本用法
spring cloud·微服务·mybatis
Appreciate(欣赏)2 小时前
JAVA使用poi类读取xlxs文件内容拼接成添加数据SQL
java·开发语言·sql