Spring Sleuth, Log4j2 不同服务中不同的trace id

Spring Sleuth, Log4j2 different trace id in different services

我正在使用配置有 spring 侦探的微服务从 service1 调用到 service2。两种微服务都具有相同的设置。我期望两个服务的跟踪 ID 相同,但得到的跟踪 ID 不同。每个服务的当前输出。

登录服务1

2019-12-07T10:51:04,219 INFO [service1,1b6f2fbf64c316fc][http-nio-8090-exec-2] c.m.p.c.MyController1: An INFO Message 1

登录服务2

2019-12-07T10:51:04,976 INFO [service2,cd796e06d80b1dfd][http-nio-8091-exec-1] c.m.s.c.MyController2: An INFO Message in service 2

我只是想知道我在以下设置中做错了什么。请帮忙。

控制器class

@RestController
@RequestMapping("/service1")
public class MyController1 {

    static final Logger LOGGER = LogManager.getLogger(MyController.class);

    @Bean
    public Sampler defaultSampler() {
        return Sampler.ALWAYS_SAMPLE;
    }

    @PostMapping("/msg")
    public String processMsg(@Valid @RequestBody final String request){

        LOGGER.info("An INFO Message 1");

        String requestURI="http://localhost:8090/service2/call1";

        HttpHeaders headers = new HttpHeaders();
        RestTemplate restTemplate = new RestTemplate();
        List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
        acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(acceptableMediaTypes);
        HttpEntity<String> entity = new HttpEntity<String>(request, headers);
        ResponseEntity<String> result = restTemplate.exchange(requestURI, HttpMethod.POST, entity, String.class);
    }
}

log4j2-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
    <Property name="springAppName">${bundle:application:spring.application.name}</Property>
    <Property name="sleuthInfo">${springAppName},%X{X-B3-TraceId}</Property>
</Properties>

<Appenders>

    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout
            pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[${sleuthInfo}][%style{%t}{bright,blue}] %style{%C{1.}}{bright,red}: %msg%n%throwable"
            disableAnsi="false" />
    </Console>

    <RollingFile name="RollingFile"
        fileName="../logs/my-logger.log"
        filePattern="../logs/$${date:yyyy-MM}/my-logger-%d{-dd-MMMM-yyyy}-%i.log.gz">
        <PatternLayout>
            <pattern>%d %p %C{1.} [%t] %m%n</pattern>
        </PatternLayout>
        <Policies>
            <!-- rollover on startup, daily and when the file reaches 10 MegaBytes -->
            <OnStartupTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="10 MB" />
            <TimeBasedTriggeringPolicy />
        </Policies>
    </RollingFile>
</Appenders>

<Loggers>
    <!-- LOG everything at INFO level -->
    <Root level="error">
        <AppenderRef ref="Console" />
        <AppenderRef ref="RollingFile" />
    </Root>

    <!-- LOG "com.mycompany*" at TRACE level -->
    <Logger name="com.mycompany" level="trace"></Logger>
</Loggers>

</Configuration>

pom.xml

<?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>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.mycompany</groupId>
<artifactId>service1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service1</name>
<description>service1 to communicate with service2</description>

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR2</spring-cloud.version> <!-- Spring Cloud Sleuth -->
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Spring Cloud Sleuth -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>

    <!-- In order to use log4j2 logging library other than Logback, exclude 
        default logback from our dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- Add log4j2 for logging -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
        <version>2.2.1.RELEASE</version><!--$NO-MVN-MAN-VER$ -->
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!-- zipkin -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/io.zipkin.brave/brave-context-log4j2 -->
    <dependency>
        <groupId>io.zipkin.brave</groupId>
        <artifactId>brave-context-log4j2</artifactId>
        <version>5.9.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我发现我做错了什么(我没有使用 spring 最佳实践)。我发布它并希望有人能从中受益。

错误代码:

RestTemplate restTemplate = new RestTemplate();

正确代码:

@Autowired
RestTemplate restTemplate;

@Bean
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

我最初认为我对 sleuth,log4j2 做错了 configuration/dependencies。通过上述修复,可以使用上述代码在 spring 带有 sleuth 和 log4j 的启动应用程序中跟踪请求。

您正在通过 new 使用 RestTemplate,您需要将其用作 bean,以便 Sleuth 可以检测它。