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

SpringBoot集成OpenTelemetry+Zipkin遇Bean创建错误求助

问题解决与需求实现方案

核心问题根源

你碰到的ManifestResourceProvider实例化失败,本质是OpenTelemetry Starter与Micrometer Tracing桥接依赖的初始化逻辑冲突:SpringBoot 3.3.0原生适配Micrometer生态,而opentelemetry-spring-boot-starter会自行初始化OTEL SDK,两者叠加后(尤其是引入opentelemetry-exporter-zipkin时),自动配置的Bean互相抢占资源,导致DataSource初始化时调用OpenTelemetry实例出错。


方案一:移除opentelemetry-spring-boot-starter(推荐,最稳定)

依赖SpringBoot原生的Micrometer生态配合OTEL桥接,完全满足你的三个需求,且彻底避免冲突:

1. 调整POM依赖

保留必要组件,移除opentelemetry-spring-boot-starter,补充JDBC、Redis的OTEL自动埋点依赖:

<dependencyManagement>
    <dependencies>
        <!-- 保持原OTEL BOM版本不变 -->
        <dependency>
            <groupId>io.opentelemetry</groupId>
            <artifactId>opentelemetry-instrumentation-bom</artifactId>
            <version>2.8.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- Prometheus监控依赖 -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    <!-- Micrometer桥接OTEL,实现追踪上下文传递 -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-tracing-bridge-otel</artifactId>
    </dependency>
    <!-- OTEL Zipkin导出器 -->
    <dependency>
        <groupId>io.opentelemetry</groupId>
        <artifactId>opentelemetry-exporter-zipkin</artifactId>
    </dependency>
    <!-- JDBC自动埋点 -->
    <dependency>
        <groupId>io.opentelemetry.instrumentation</groupId>
        <artifactId>opentelemetry-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- Redis自动埋点(适配Spring Data Redis 4.0+) -->
    <dependency>
        <groupId>io.opentelemetry.instrumentation</groupId>
        <artifactId>opentelemetry-spring-data-redis-4.0</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

2. 配置application.yaml

# 追踪采样与Zipkin配置
management:
  tracing:
    sampling:
      probability: 1.0 # 全量采样,可按需调整
    propagation:
      type: tracecontext,baggage
  zipkin:
    tracing:
      endpoint: http://你的Zipkin地址:9411/api/v2/spans

# 日志自动注入traceId/spanId到MDC
logging.pattern.level: "%5p [traceId=%X{traceId}, spanId=%X{spanId}]"

3. 需求验证

  • MDC自动加traceId:通过logging.pattern.level配置,日志框架会自动从Micrometer追踪上下文获取traceId/spanId注入MDC,直接输出到日志。
  • JDBC/Redis自动加spanId:引入的opentelemetry-jdbc和opentelemetry-spring-data-redis-4.0会自动拦截JDBC连接、Redis操作,创建子Span并关联主Trace。
  • 追踪数据导出Zipkin:micrometer-tracing-bridge-otel会把Micrometer的Trace数据转成OTEL格式,通过opentelemetry-exporter-zipkin发送到配置的Zipkin端点。

方案二:保留opentelemetry-spring-boot-starter(需手动禁用冲突配置)

如果必须保留Starter,需禁用Micrometer Tracing的自动配置,让OTEL完全接管追踪逻辑:

1. 调整POM依赖

移除micrometer-tracing-bridge-otel,添加OTEL日志MDC适配器:

<dependencies>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
    <dependency>
        <groupId>io.opentelemetry</groupId>
        <artifactId>opentelemetry-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>io.opentelemetry</groupId>
        <artifactId>opentelemetry-exporter-zipkin</artifactId>
    </dependency>
    <!-- OTEL日志MDC适配器 -->
    <dependency>
        <groupId>io.opentelemetry.instrumentation</groupId>
        <artifactId>opentelemetry-logback-mdc-1.0</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

2. 配置application.yaml

# OTEL核心配置
opentelemetry:
  traces:
    exporter: zipkin
    sampling:
      probability: 1.0
  zipkin:
    endpoint: http://你的Zipkin地址:9411/api/v2/spans
  instrumentation:
    jdbc:
      enabled: true
    spring-data-redis:
      enabled: true

# OTEL的MDC键名是trace_id/span_id,注意和Micrometer区分
logging.pattern.level: "%5p [traceId=%X{trace_id}, spanId=%X{span_id}]"

3. 禁用冲突的自动配置

在SpringBoot启动类添加注解,避免Micrometer和OTEL的配置互相干扰:

@SpringBootApplication(exclude = {
    MicrometerTracingAutoConfiguration.class,
    OtelBridgeAutoConfiguration.class
})
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

关键注意事项

  1. SpringBoot 3.3.0与OTEL 2.8.0版本完全兼容,无需调整BOM版本。
  2. 两种方案二选一,不要混合Micrometer桥接和OTEL Starter的自动配置逻辑。
  3. 方案一中,若还有其他需要自动埋点的组件(如RabbitMQ),需补充对应的OTEL instrumentation依赖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 04:07:40