目录

一、http接口链路

1、代码

(1)依赖

(2)filter

 (3)MDC util

(4) 业务接口

 (5)启动类

(6)配置

2、测试:

二、多线程

1、引入多线程

2、改进 

(1)TaskDecorator 

(2)在线程池中设置下

(3) 测试

三、event

1、producer

2、监听器 

 3、测试

四、MQ

五、跨服务调用

一、http接口链路

1、代码
(1)依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>log-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.4</version>
        <relativePath/>
    </parent>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--commom-->
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

    <build>
        <!--        <finalName>my-service</finalName>-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
(2)filter
package org.example.filter;

import io.micrometer.common.util.StringUtils;
import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.example.util.TraceUtil;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.UUID;

@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
public class TraceFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("处理trace开始,优先级最高");
        HttpServletResponse httpServletResponse =  (HttpServletResponse) servletResponse;
        //1、获取traceId
        String traceId = TraceUtil.getTraceId();
        if (StringUtils.isBlank(traceId)) {
            log.warn("traceId为空");
            traceId = UUID.randomUUID().toString().replaceAll("-","");
            //为空,则塞到请求中
            traceId = TraceUtil.generate();
        }
        //2、返回给前端
        log.info("traceId={}",traceId);
        httpServletResponse.setHeader(TraceUtil.TRACE_ID, traceId);
        log.info("处理trace结束");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
package org.example.filter;

import jakarta.servlet.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Order(1)
@Slf4j
@Component
public class OtherFilter implements Filter {


    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("其他filter开始");
        log.info("其他filter结束");
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
 (3)MDC util
package org.example.util;

import org.slf4j.MDC;

import java.util.UUID;

public class TraceUtil {
    public static final String TRACE_ID = "my-trace-id";

    public static String getTraceId() {
        return MDC.get(TRACE_ID);
    }

    public static String generate() {
        String id = UUID.randomUUID().toString().replaceAll("-","");
        MDC.put(TRACE_ID,id);
        return id;
    }
}
(4) 业务接口
package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.service.UserService;
import org.example.util.TraceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getById")
    public String getById(Integer id) {
        log.info("controller-getById开始,id={}",id);
        userService.getById(id);
        log.info("controller-getById结束,id={}",id);
        return "getById";
    }
}
package org.example.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.service.UserService;
import org.example.util.UserUtil;
import org.springframework.stereotype.Service;

@Service("userService")
@Slf4j
public class UserServiceImpl implements UserService {

    @Override
    public void getById(Integer id) {
        log.info("service开始,id={}",id);
        try{
            UserUtil.checkUserId(id);
        }catch (Exception e){
            log.error("调用util异常");
        }
        log.info("service结束,id={}",id);
    }
}
package org.example.util;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UserUtil {

    public static void checkUserId(Integer id) {
        log.info("这是util");
    }
}
 (5)启动类
package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LogDemo {
    public static void main(String[] args) {
        SpringApplication.run(LogDemo.class, args);
    }
}
(6)配置
server.port=6666
server.servlet.context-path=/logDemo

logging.config=classpath:logback.xml

logback.xml打印my-trace-id: 

<configuration>

    <!-- 格式化输出:%date表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度 %msg:日志消息,%n是换行符-->
    <property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] [%X{my-trace-id}] %-5level %logger{36} - %msg%n" />
    <!-- 定义日志存储的路径,不要配置相对路径 -->
    <property name="FILE_PATH" value="usr/logs/demo.%d{yyyy-MM-dd}.%i.log" />

    <!-- 控制台输出日志 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- 按照上面配置的LOG_PATTERN来打印日志 -->
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <!--每天生成一个日志文件,保存15天的日志文件。rollingFile是用来切分文件的 -->
    <appender name="FILE"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${FILE_PATH}</fileNamePattern>
            <!-- keep 15 days' worth of history -->
            <maxHistory>15</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- 日志文件的最大大小,超过这个大小重新生成一个文件 -->
                <maxFileSize>1MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

        <encoder>
            <pattern>${LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    <!-- project default level -->
    <logger name="src" level="INFO" />

    <!-- 日志输出级别 常用的日志级别按照从高到低依次为:ERROR、WARN、INFO、DEBUG。 -->
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

2、测试:

启动项目

(1)调用localhost:6666/logDemo/user/getById?id=1

  从日志打印可以看出,整个链路的trace-id相同:

11:36:17.633 [http-nio-6666-exec-2] [] INFO  o.a.c.c.C.[.[localhost].[/logDemo] - Initializing Spring DispatcherServlet 'dispatcherServlet'
11:36:17.633 [http-nio-6666-exec-2] [] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
11:36:17.634 [http-nio-6666-exec-2] [] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 1 ms
11:36:17.635 [http-nio-6666-exec-2] [] INFO  org.example.filter.TraceFilter - 处理trace开始,优先级最高
11:36:17.636 [http-nio-6666-exec-2] [] WARN  org.example.filter.TraceFilter - traceId为空
11:36:17.637 [http-nio-6666-exec-2] [6d01a0b7ed124045b5b124eb82798042] INFO  org.example.filter.TraceFilter - traceId=6d01a0b7ed124045b5b124eb82798042
11:36:17.637 [http-nio-6666-exec-2] [6d01a0b7ed124045b5b124eb82798042] INFO  org.example.filter.TraceFilter - 处理trace结束
11:36:17.638 [http-nio-6666-exec-2] [6d01a0b7ed124045b5b124eb82798042] INFO  org.example.filter.OtherFilter - 其他filter开始
11:36:17.638 [http-nio-6666-exec-2] [6d01a0b7ed124045b5b124eb82798042] INFO  org.example.filter.OtherFilter - 其他filter结束
11:36:17.660 [http-nio-6666-exec-2] [6d01a0b7ed124045b5b124eb82798042] INFO  o.example.controller.UserController - controller-getById开始,id=1
11:36:17.660 [http-nio-6666-exec-2] [6d01a0b7ed124045b5b124eb82798042] INFO  o.e.service.impl.UserServiceImpl - service开始,id=1
11:36:17.660 [http-nio-6666-exec-2] [6d01a0b7ed124045b5b124eb82798042] INFO  org.example.util.UserUtil - 这是util
11:36:17.660 [http-nio-6666-exec-2] [6d01a0b7ed124045b5b124eb82798042] INFO  o.e.service.impl.UserServiceImpl - service结束,id=1
11:36:17.660 [http-nio-6666-exec-2] [6d01a0b7ed124045b5b124eb82798042] INFO  o.example.controller.UserController - controller-getById结束,id=1

   查看postman请求结果中也返回了my-trace-id:

(2)再调用一次:可以看到,生成了新的traceId

11:37:58.195 [http-nio-6666-exec-5] [] INFO  org.example.filter.TraceFilter - 处理trace开始,优先级最高
11:37:58.195 [http-nio-6666-exec-5] [] WARN  org.example.filter.TraceFilter - traceId为空
11:37:58.195 [http-nio-6666-exec-5] [4d14f3a79f2b4f7ebe2b26b7018af469] INFO  org.example.filter.TraceFilter - traceId=4d14f3a79f2b4f7ebe2b26b7018af469
11:37:58.196 [http-nio-6666-exec-5] [4d14f3a79f2b4f7ebe2b26b7018af469] INFO  org.example.filter.TraceFilter - 处理trace结束
11:37:58.196 [http-nio-6666-exec-5] [4d14f3a79f2b4f7ebe2b26b7018af469] INFO  org.example.filter.OtherFilter - 其他filter开始
11:37:58.196 [http-nio-6666-exec-5] [4d14f3a79f2b4f7ebe2b26b7018af469] INFO  org.example.filter.OtherFilter - 其他filter结束
11:37:58.196 [http-nio-6666-exec-5] [4d14f3a79f2b4f7ebe2b26b7018af469] INFO  o.example.controller.UserController - controller-getById开始,id=1
11:37:58.196 [http-nio-6666-exec-5] [4d14f3a79f2b4f7ebe2b26b7018af469] INFO  o.e.service.impl.UserServiceImpl - service开始,id=1
11:37:58.196 [http-nio-6666-exec-5] [4d14f3a79f2b4f7ebe2b26b7018af469] INFO  org.example.util.UserUtil - 这是util
11:37:58.196 [http-nio-6666-exec-5] [4d14f3a79f2b4f7ebe2b26b7018af469] INFO  o.e.service.impl.UserServiceImpl - service结束,id=1
11:37:58.197 [http-nio-6666-exec-5] [4d14f3a79f2b4f7ebe2b26b7018af469] INFO  o.example.controller.UserController - controller-getById结束,id=1

二、多线程

1、引入多线程

对上面的userService加上多线程:

(1)新增多线程配置

package org.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class ThreadPoolTaskExecutorConfig {
    private static int CORE_POOL_SIZE = 5;
    private static int MAX_POOL_SIZE = 10;
    public static final String userPool = "userPool";

    @Bean(name= userPool)
    public ThreadPoolTaskExecutor userTaskExecutor(){
        ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor();
        //线程池维护线程的最少数量
        poolTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);
        //线程池维护线程的最大数量
        poolTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
        //线程池所使用的缓冲队列
        poolTaskExecutor.setQueueCapacity(200);
        //线程池维护线程所允许的空闲时间
        poolTaskExecutor.setKeepAliveSeconds(30000);
        poolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        poolTaskExecutor.setBeanName(userPool);
        return poolTaskExecutor;
    }

}

(2)在org.example.service.impl.UserServiceImpl#getById方法头上开启多线程

    @Override
    @Async(ThreadPoolTaskExecutorConfig.userPool)
    public void getById(Integer id)

(3)测试 

调用接口,日志打印如下,可以看到主线程http-nio-6666-exec-2的MDC上下文没有传递给子线程userPool-1

13:33:42.094 [http-nio-6666-exec-2] [] INFO  o.a.c.c.C.[.[localhost].[/logDemo] - Initializing Spring DispatcherServlet 'dispatcherServlet'
13:33:42.094 [http-nio-6666-exec-2] [] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
13:33:42.095 [http-nio-6666-exec-2] [] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 1 ms
13:33:42.096 [http-nio-6666-exec-2] [] INFO  org.example.filter.TraceFilter - 处理trace开始,优先级最高
13:33:42.097 [http-nio-6666-exec-2] [] WARN  org.example.filter.TraceFilter - traceId为空
13:33:42.098 [http-nio-6666-exec-2] [0bbc0b142889443a83c08596ed0d83e5] INFO  org.example.filter.TraceFilter - traceId=0bbc0b142889443a83c08596ed0d83e5
13:33:42.099 [http-nio-6666-exec-2] [0bbc0b142889443a83c08596ed0d83e5] INFO  org.example.filter.TraceFilter - 处理trace结束
13:33:42.100 [http-nio-6666-exec-2] [0bbc0b142889443a83c08596ed0d83e5] INFO  org.example.filter.OtherFilter - 其他filter开始
13:33:42.100 [http-nio-6666-exec-2] [0bbc0b142889443a83c08596ed0d83e5] INFO  org.example.filter.OtherFilter - 其他filter结束
13:33:42.124 [http-nio-6666-exec-2] [0bbc0b142889443a83c08596ed0d83e5] INFO  o.example.controller.UserController - controller-getById开始,id=1
13:33:42.127 [http-nio-6666-exec-2] [0bbc0b142889443a83c08596ed0d83e5] INFO  o.example.controller.UserController - controller-getById结束,id=1
13:33:42.127 [userPool-1] [] INFO  o.e.service.impl.UserServiceImpl - service开始,id=1
13:33:42.127 [userPool-1] [] INFO  org.example.util.UserUtil - 这是util
13:33:42.128 [userPool-1] [] INFO  o.e.service.impl.UserServiceImpl - service结束,id=1
2、改进 

在上面的基础上加上TaskDecorator 透传MDC参数。

(1)TaskDecorator 
package org.example.util;

import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;

import java.util.Map;

public class MdcTaskDecorator implements TaskDecorator {
    @Override
    public Runnable decorate(Runnable runnable) {
        // Capture the context of the current thread (the main thread)
        Map<String, String> contextMap = MDC.getCopyOfContextMap();

        return () -> {
            try {
                // Set the context map for the child thread
                if (contextMap != null) {
                    MDC.setContextMap(contextMap);
                }
                runnable.run();
            } finally {
                // Clear the context map after the child thread has finished
                MDC.clear();
            }
        };
    }
}
(2)在线程池中设置下
poolTaskExecutor.setTaskDecorator(new MdcTaskDecorator());
(3) 测试

重启项目

① 访问localhost:6666/logDemo/user/getById?id=1,可以看到线程池中也打印了traceId,且子线程和父线程的traceId一致:

13:52:14.689 [http-nio-6666-exec-1] [] INFO  o.a.c.c.C.[.[localhost].[/logDemo] - Initializing Spring DispatcherServlet 'dispatcherServlet'
13:52:14.689 [http-nio-6666-exec-1] [] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
13:52:14.691 [http-nio-6666-exec-1] [] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 2 ms
13:52:14.692 [http-nio-6666-exec-1] [] INFO  org.example.filter.TraceFilter - 处理trace开始,优先级最高
13:52:14.693 [http-nio-6666-exec-1] [] WARN  org.example.filter.TraceFilter - traceId为空
13:52:14.694 [http-nio-6666-exec-1] [8f5be6395558444c93b64e7cf71eb429] INFO  org.example.filter.TraceFilter - traceId=8f5be6395558444c93b64e7cf71eb429
13:52:14.694 [http-nio-6666-exec-1] [8f5be6395558444c93b64e7cf71eb429] INFO  org.example.filter.TraceFilter - 处理trace结束
13:52:14.696 [http-nio-6666-exec-1] [8f5be6395558444c93b64e7cf71eb429] INFO  org.example.filter.OtherFilter - 其他filter开始
13:52:14.696 [http-nio-6666-exec-1] [8f5be6395558444c93b64e7cf71eb429] INFO  org.example.filter.OtherFilter - 其他filter结束
13:52:14.717 [http-nio-6666-exec-1] [8f5be6395558444c93b64e7cf71eb429] INFO  o.example.controller.UserController - controller-getById开始,id=1
13:52:14.720 [http-nio-6666-exec-1] [8f5be6395558444c93b64e7cf71eb429] INFO  o.example.controller.UserController - controller-getById结束,id=1
13:52:14.722 [userPool-1] [8f5be6395558444c93b64e7cf71eb429] INFO  o.e.service.impl.UserServiceImpl - service开始,id=1
13:52:14.735 [userPool-1] [8f5be6395558444c93b64e7cf71eb429] INFO  org.example.util.UserUtil - 这是util
13:52:14.735 [userPool-1] [8f5be6395558444c93b64e7cf71eb429] INFO  o.e.service.impl.UserServiceImpl - service结束,id=1

 ② 再次访问,可以看到生成了新的traceId

13:56:05.268 [http-nio-6666-exec-5] [] INFO  org.example.filter.TraceFilter - 处理trace开始,优先级最高
13:56:05.269 [http-nio-6666-exec-5] [] WARN  org.example.filter.TraceFilter - traceId为空
13:56:05.269 [http-nio-6666-exec-5] [c16445b7e08a48ddb17ea0e078e8c745] INFO  org.example.filter.TraceFilter - traceId=c16445b7e08a48ddb17ea0e078e8c745
13:56:05.269 [http-nio-6666-exec-5] [c16445b7e08a48ddb17ea0e078e8c745] INFO  org.example.filter.TraceFilter - 处理trace结束
13:56:05.269 [http-nio-6666-exec-5] [c16445b7e08a48ddb17ea0e078e8c745] INFO  org.example.filter.OtherFilter - 其他filter开始
13:56:05.269 [http-nio-6666-exec-5] [c16445b7e08a48ddb17ea0e078e8c745] INFO  org.example.filter.OtherFilter - 其他filter结束
13:56:05.270 [http-nio-6666-exec-5] [c16445b7e08a48ddb17ea0e078e8c745] INFO  o.example.controller.UserController - controller-getById开始,id=1
13:56:05.270 [http-nio-6666-exec-5] [c16445b7e08a48ddb17ea0e078e8c745] INFO  o.example.controller.UserController - controller-getById结束,id=1
13:56:05.270 [userPool-2] [c16445b7e08a48ddb17ea0e078e8c745] INFO  o.e.service.impl.UserServiceImpl - service开始,id=1
13:56:05.271 [userPool-2] [c16445b7e08a48ddb17ea0e078e8c745] INFO  org.example.util.UserUtil - 这是util
13:56:05.271 [userPool-2] [c16445b7e08a48ddb17ea0e078e8c745] INFO  o.e.service.impl.UserServiceImpl - service结束,id=1

三、event

event和普通的业务调用区别不大,猜想producer和 listener 可以共享MDC。改动代码验证下:

1、producer

  修改service,发布event

package org.example.event;

import org.springframework.context.ApplicationEvent;

public class MyUserEvent extends ApplicationEvent {

    public MyUserEvent(Integer id) {
        super(id);
    }
}
package org.example.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.config.ThreadPoolTaskExecutorConfig;
import org.example.event.MyUserEvent;
import org.example.service.UserService;
import org.example.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service("userService")
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    //@Async(ThreadPoolTaskExecutorConfig.userPool)
    public void getById(Integer id) {
        log.info("service开始,id={}",id);
        try{
            //UserUtil.checkUserId(id);
            applicationContext.publishEvent(new MyUserEvent(id));
        }catch (Exception e){
            log.error("调用util异常");
        }
        log.info("service结束,id={}",id);
    }
}
2、监听器 

用两个监听器去监听同一个事件

package org.example.listener;

import lombok.extern.slf4j.Slf4j;
import org.example.event.MyUserEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MyUserListenerOne {

    @EventListener
    public void myEventListener(MyUserEvent event) throws InterruptedException {
       log.info("这是监听one");
    }
}
package org.example.listener;

import lombok.extern.slf4j.Slf4j;
import org.example.event.MyUserEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class MyUserListenerTwo {

    @EventListener
    public void myEventListener(MyUserEvent event) throws InterruptedException {
       log.info("这是监听two");
    }
}
 3、测试

重启项目调用接口

查看日志,可以看到producer和监听器共享MDC上下文:

14:14:53.026 [http-nio-6666-exec-2] [] INFO  o.a.c.c.C.[.[localhost].[/logDemo] - Initializing Spring DispatcherServlet 'dispatcherServlet'
14:14:53.026 [http-nio-6666-exec-2] [] INFO  o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
14:14:53.027 [http-nio-6666-exec-2] [] INFO  o.s.web.servlet.DispatcherServlet - Completed initialization in 1 ms
14:14:53.028 [http-nio-6666-exec-2] [] INFO  org.example.filter.TraceFilter - 处理trace开始,优先级最高
14:14:53.029 [http-nio-6666-exec-2] [] WARN  org.example.filter.TraceFilter - traceId为空
14:14:53.029 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  org.example.filter.TraceFilter - traceId=240e8424a1514e2cbf1d1053b13c4b08
14:14:53.030 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  org.example.filter.TraceFilter - 处理trace结束
14:14:53.031 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  org.example.filter.OtherFilter - 其他filter开始
14:14:53.032 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  org.example.filter.OtherFilter - 其他filter结束
14:14:53.053 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  o.example.controller.UserController - controller-getById开始,id=1
14:14:53.053 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  o.e.service.impl.UserServiceImpl - service开始,id=1
14:14:53.053 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  o.example.listener.MyUserListenerOne - 这是监听one
14:14:53.053 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  o.example.listener.MyUserListenerTwo - 这是监听two
14:14:53.053 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  o.e.service.impl.UserServiceImpl - service结束,id=1
14:14:53.053 [http-nio-6666-exec-2] [240e8424a1514e2cbf1d1053b13c4b08] INFO  o.example.controller.UserController - controller-getById结束,id=1

四、MQ

和上面的event类似,

entry point入口-->producer生产者-->consumer消费者 可以共用同一个traceId。

五、跨服务调用

比如A项目的http接口调用B项目的http接口。无需共享,各自使用一个traceId。

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部