新增common-trace模块:traceId设置到feign调用head中

This commit is contained in:
xudawei 2024-04-22 16:46:10 +08:00
parent a1fed917e7
commit 2a5f7eb5bd
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>axzo-common-loggings</artifactId>
<groupId>cn.axzo.framework.logging</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.axzo.framework</groupId>
<artifactId>axzo-common-trace</artifactId>
<name>Axzo Common trace</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-core</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,23 @@
package com.axzo.framework.trace.interceptor;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;
/**
* 调用前从 MDC中获取上一步骤设置的traceId放置到 header中供下一个服务从header中获取traceId
*/
@Configuration
public class FeignInterceptor implements RequestInterceptor {
private static final String TRACE_ID = "traceId";
public static final String CTX_LOG_ID_MDC = "ctxLogId";
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header(TRACE_ID, MDC.get(TRACE_ID));
requestTemplate.header(CTX_LOG_ID_MDC, MDC.get(CTX_LOG_ID_MDC));
}
}