使用Filter,给所有请求头和响应头添加X-Head-RequestId。调用方没传时,默认生成。此RequestId方便查日志定位问题。
1.添加tool的github的repository
<repositories>
<repository>
<id>maven-repository</id>
<url>https://raw.github.com/javaRepository/maven-repository/master/releases</url>
</repository>
</repositories>
2.添加依赖
<dependency>
<groupId>common.tool</groupId>
<artifactId>tool-requestId</artifactId>
<version>0.0.1</version>
</dependency>
3.springboot添加扫描包
@ServletComponentScan(basePackages={"com.tool"})
4.logback使用[RequestId: %X{X-Head-RequestId}]获取RequestId
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [RequestId: %X{X-Head-RequestId}] %logger{50} - %msg%n</pattern>
</encoder>
</appender>