在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。