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

使用Micrometer与Feign Client保持HTTP调用的Span连续性

关于Spring Boot 3.1.5中Micrometer与Feign调用Span连续性的问题

我使用Spring Boot 3.1.5,已将追踪实现从Sleuth迁移到Micrometer,目前已能正常打印TraceId和SpanId。但通过Spring Cloud OpenFeign调用其他微服务时,Micrometer会生成新Span,而旧版Sleuth默认不会,我希望保持调用时的SpanId一致。

我的Feign配置如下:

@Configuration
public class FeignConfiguration {

    
    @Bean
    public Capability capability(final MeterRegistry registry) {
      return new MicrometerCapability(registry);
    }    
    
    @Bean
    public Logger.Level loggerLevel() {
        return Logger.Level.FULL;
    }
}

问题1:为何旧版Sleuth默认在REST调用中不创建新Span,而Micrometer却会?

  • Sleuth的默认设计是把Feign调用当成当前Span的一部分,仅传递TraceId和SpanId,不生成新Span,这种模式更贴近"单一请求链路"的直观认知。
  • Micrometer Tracing的默认逻辑遵循OpenTelemetry规范,把外部HTTP调用(包括Feign发起的)都视为独立的子Span,目的是更精细地追踪每个外部调用的耗时、状态等细节,符合分布式追踪中"每个外部交互都作为独立Span"的标准设计。你觉得不合理可能是习惯了Sleuth的简化模式,但Micrometer的设计是为了提供更完整的链路追踪维度。

问题2:如何通过Feign Client在HTTP调用中保持Span的连续性?

要实现和Sleuth一致的Span不新增效果,有两种可行方案:

方案1:自定义MicrometerCapability,禁用子Span创建

自定义Capability覆盖默认的Span创建逻辑,只传递当前Span的TraceId和SpanId,不生成新Span:

@Configuration
public class FeignConfiguration {

    @Bean
    public Capability capability(Tracer tracer) {
        return new MicrometerCapability(tracer) {
            @Override
            public void enrich(ClientRequest request) {
                Span currentSpan = tracer.currentSpan();
                if (currentSpan != null) {
                    // 直接传递当前Span的traceparent信息,不创建新Span
                    request.headers().putSingle("traceparent", currentSpan.context().traceparent());
                }
            }
        };
    }

    @Bean
    public Logger.Level loggerLevel() {
        return Logger.Level.FULL;
    }
}

方案2:全局配置关闭Feign的Span创建

通过配置文件直接禁用Feign调用的自动子Span生成:

# 关闭Feign调用的链路追踪Span自动创建
management.tracing.instrumentation.feign.enabled=false

这种方式更简单,会全局关闭Feign的链路追踪增强,仅传递TraceId和SpanId,完全匹配Sleuth的默认行为。

注意:选择方案2时,要确保服务能正常通过traceparent头传递TraceId和SpanId到下游,这是Micrometer的默认传递机制,无需额外配置。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 08:33:35