You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3迁移后Quartz中Micrometer无法生成traceId/spanId的解决方法

解决Quartz Jobs无TraceId/SpanId的问题

1. 添加Micrometer Tracing Quartz相关依赖

确保项目依赖中包含Micrometer追踪对Quartz的支持组件,以Maven和Gradle为例:

Maven配置

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-instrumentation</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
<!-- 若使用OpenTelemetry替代Brave,替换为micrometer-tracing-bridge-otel依赖 -->

Gradle配置

implementation 'io.micrometer:micrometer-tracing-bridge-brave'
implementation 'io.micrometer:micrometer-tracing-instrumentation'
implementation 'org.springframework.boot:spring-boot-starter-quartz'

2. 自定义Quartz JobFactory实现追踪包装

Spring Sleuth原有的Quartz自动配置在Micrometer中需要手动实现,通过自定义JobFactory将任务包装为可追踪的实例:

import io.micrometer.tracing.Tracer;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

public class TracingSpringBeanJobFactory extends SpringBeanJobFactory {

    private final Tracer tracer;

    @Autowired
    public TracingSpringBeanJobFactory(Tracer tracer) {
        this.tracer = tracer;
    }

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        if (jobInstance instanceof org.quartz.Job) {
            return new TraceableJob((org.quartz.Job) jobInstance, tracer);
        }
        return jobInstance;
    }
}

接着在配置类中注册该JobFactory并绑定到Scheduler:

import org.quartz.spi.JobFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {

    @Bean
    public JobFactory tracingJobFactory(Tracer tracer) {
        return new TracingSpringBeanJobFactory(tracer);
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(JobFactory tracingJobFactory) {
        SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
        scheduler.setJobFactory(tracingJobFactory);
        // 其他Quartz配置(如触发器、任务详情等)
        return scheduler;
    }
}

3. 手动创建Span(自动配置失效时的备选方案)

如果上述配置仍未生效,可在Job的执行方法中手动创建并管理Span:

import io.micrometer.tracing.Span;
import io.micrometer.tracing.Tracer;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

public class SampleQuartzJob implements Job {

    private final Tracer tracer;

    @Autowired
    public SampleQuartzJob(Tracer tracer) {
        this.tracer = tracer;
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Span span = tracer.nextSpan().name("sample-quartz-job-execute").start();
        try (Tracer.SpanInScope spanScope = tracer.withSpan(span)) {
            // 执行你的业务逻辑
            processJobTask();
        } finally {
            span.end();
        }
    }

    private void processJobTask() {
        // 具体业务代码
    }
}

4. 验证日志输出格式

确保日志配置中包含traceId和spanId字段,以Logback为例:

<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} traceId=%X{traceId}, spanId=%X{spanId} - %msg%n</pattern>

内容的提问来源于stack exchange,提问作者cloud_7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 21:25:13