spring cloud gateway 整合micrometer,替代sleuth

在spring-boot3.0后,官方弃用了sleuth,对trace日志追踪的配置会稍有些不同。

@SpringBootApplication
@EnableDiscoveryClient
public class DemoApplication {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // 开启reactor的上下文传递
        Hooks.enableAutomaticContextPropagation();
        new SpringApplicationBuilder(DemoApplication .class).run(args);
    }
}
@Configuration(proxyBeanMethods = false)
class CommonConfiguration {
    /**
     *
     * @param registry
     * @param applicationContext
     * @return
     */
    @Bean
    public HttpHandler httpHandler(ObservationRegistry registry, ApplicationContext applicationContext) {
        return WebHttpHandlerBuilder.applicationContext(applicationContext)
                .observationRegistry(registry)
                .build();
    }

}
logging:
  level:
    ROOT: info
  pattern:
    level: "%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]"
management:
  endpoint:
    gateway:
      enabled: true
  endpoints:
    web:
      exposure:
        include: health,gateway,prometheus
  tracing:
    enabled: true
    propagation:
      type: w3c

boot 3.2.0
cloud 2023.0.0
micrometer 1.12.1

<dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-tracing-bridge-otel</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-observation</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-registry-prometheus -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-tracing</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>context-propagation</artifactId>
            <version>1.1.0</version>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-core</artifactId>
            <version>3.6.1</version>
        </dependency>

使用的时候 直接注入 final Tracer tracer; 既可以获取traceId
tracer.currentSpan().context().traceId();

如果下游使用springboot2.0的微服务,那么把sleuth的propagation切换为w3c格式,w3c的请求头使用traceparent作为header name。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值