我目前正在我们的项目中实施 Spring Cloud Sleuth。我需要将 traceId 添加到响应标头中。有没有办法做到这一点?
谢谢,
阿努普
我有下面的代码,用于处理来自其他应用程序的请求,这些应用程序通过标头传递traceId Traceparent。我希望它能够从请求中吸收父级 TraceId,并且在 zipkin 仪表板中我应该看到该应用程序与其他应用程序之间的连接。它曾经与spring-cloud-sleuth-zipkin. 现在我迁移到spring boot 3,包改为micrometer-tracing-bridge-otel(参见pom.xml)。现在,它不再从请求中提取父traceId,而是生成一个全为0的默认父traceId,导致应用程序与zipkin仪表板中的其他应用程序断开连接
我使用带有标头的简单卷曲请求进行了测试Traceparent:curl --location --request GET 'http://localhost:8080/test' --header 'Traceparent: 00-63cf0173620c57b0aed605ee94255089-1444ca74c3d2133a-01'但此代码不会从标头中提取父上下文。知道如何进行这项工作吗?
@RestController
public class Test {
@Autowired
private Tracer tracer;
@GetMapping(path="/test")
public ResponseEntity<?> handleTest() {
ScopedSpan span = tracer.startScopedSpan("test span");
return ResponseEntity.ok();
}
}
Run Code Online (Sandbox Code Playgroud)
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
</parent>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-urlconnection</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud) spring-boot spring-cloud-sleuth micrometer spring-micrometer micrometer-tracing
我的一些微服务使用log4j2作为记录器.Spring cloud Sleuth支持logback.在这种情况下,如何使用Sleuth获取分布式跟踪.我理解使用带有log4j2的侦探,我必须实现某些类.我试过这个,但没有运气.请帮忙
只需添加 spring-cloud-starter-zipkin 也能够生成 spanId 和 TraceId 那么 Sleuth 需要什么?spring-cloud-starter-zipkin 实际上是自己拉 io.zipkin.brave 而不是 Sleuth。
我正在尝试使用 Sleuth 跟踪 Spring Cloud 集成 aws + AWS SQS 应用程序。将消息添加到队列后,接收方从 SQS 接收消息。日志有应用程序名称,但从 sql 队列接收消息时没有跟踪 id 和跨度 id。这是日志中的一行:
2017-07-28 16:24:02.352 INFO [sqs-sleuth-demo,,,] 9706 --- [enerContainer-2] com.example.demo.SQSMessageReceiver:出队消息:hello world
我使用 Spring Boot '1.5.4.RELEASE' 和 Spring Cloud 'Dalston.SR1'。这是依赖项:
dependencies {
compile("org.springframework.boot:spring-boot")
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.cloud:spring-cloud-aws-messaging")
compile("org.springframework.cloud:spring-cloud-aws-autoconfigure")
compile('org.springframework.cloud:spring-cloud-starter-sleuth')
compile("org.springframework.integration:spring-integration-aws:1.0.0.RELEASE")
compile("com.amazonaws:aws-java-sdk-sqs")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序配置.java
@Configuration
public class AppConfig {
@Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
@Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
@Value("${amazon.sqs.endpoint}")
private String amazonSqsEndpoint;
@Value("${cloud.aws.region.static}")
private String awsRegion;
@Bean
@Primary
public AWSCredentialsProviderChain …Run Code Online (Sandbox Code Playgroud) 我希望开发一个基于 spring-cloud-gateway:2.0.2-RELEASE 的网关服务器,并希望利用 sleuth 进行日志记录。自从我写入日志时,我就开始运行 Sleuth,我看到 Sleuth 详细信息(跨度 ID 等),但我希望看到自动记录的消息正文。我需要做些什么才能让 Sleuth 使用 Spring-Cloud-Gateway 记录开箱即用的请求/响应吗?
这是到达我的下游服务的请求标头
标题:
{ 'x-request-foo': '2a9c5e36-2c0f-4ad3-926c-cb20d4428462',
转发: 'proto=http;host=localhost;for="0:0:0:0:0:0:0:1:51720"',
'x-forwarded-for': '0:0:0:0:0:0:0:1',
'x-forwarded-proto': 'http',
'x-转发端口': '80',
'x-转发主机': '本地主机',
'x-b3-traceid': '5bd33eb8050c7a32dfce6adfe68b06ca',
'x-b3-spanid': 'ba202a6d6f3e2893',
'x-b3-parentspanid': 'dfce6adfe68b06ca',
'x-b3-采样': '0',
主机:'本地主机:8080'},
网关服务中的 Gradle 文件..
构建脚本{
分机{
kotlin版本 = '1.2.61'
springBootVersion = '2.0.6.RELEASE'
springCloudVersion = 'Finchley.RELEASE'
}
}
依赖管理{
进口{
mavenBom“org.springframework.cloud:spring-cloud-sleuth:2.0.2.RELEASE”
mavenBom 'org.springframework.cloud:spring-cloud-gateway:2.0.2.RELEASE'
mavenBom“org.springframework.cloud:spring-cloud-dependency:$ {springCloudVersion}”
}
}
依赖项{
实现('org.springframework.cloud:spring-cloud-starter-sleuth')
实现('org.springframework.cloud:spring-cloud-starter-gateway')
实现(“org.jetbrains.kotlin:kotlin-stdlib-jdk8”)
实现(“org.jetbrains.kotlin:kotlin-reflect”)
testImplementation('org.springframework.boot:spring-boot-starter-test')
}
最后是网关服务的 application.yml 文件...
服务器:
小服务程序:
上下文路径:/
端口:80
春天:
应用:
名称:api.gateway.ben.com … 我正在使用 Sleuth,我想知道是否可以获取当前的 traceId?我不需要添加任何回复或任何东西。我只想要在某些情况下提醒开发团队的电子邮件的 traceId。
当我通过intellij启动我的spring启动应用程序时,下面的类正确初始化并且可以看到日志.但如果我从构建中再次运行jar,MyCurrentTraceContext似乎没有初始化,我也看不到输出中的日志.我确实需要这个类和我的定制逻辑来运行一些参数到MDC.有什么建议?
@Configuration
@ConditionalOnProperty(value="spring.sleuth.enabled", matchIfMissing=true)
@AutoConfigureBefore(TraceAutoConfiguration.class)
public class MyLogConfiguration extends SleuthLogAutoConfiguration {
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(WtrLogConfiguration.class);
@Configuration
@ConditionalOnClass(MDC.class)
@EnableConfigurationProperties(SleuthSlf4jProperties.class)
protected static class MySlf4jConfiguration extends Slf4jConfiguration {
@Bean
@ConditionalOnProperty(value = "spring.sleuth.log.slf4j.enabled", matchIfMissing = true)
@ConditionalOnMissingBean
@Override
public CurrentTraceContext slf4jSpanLogger() {
LOGGER.info("************ OVER WRITTING WTIH WtrCurrentTraceContext*******");
return new MyCurrentTraceContext(Slf4jCurrentTraceContext.create());
}
}
}
Run Code Online (Sandbox Code Playgroud) Spring Cloud Sleuth 用于创建traceId(跨服务请求时唯一)和spanId(对于一个工作单元来说是相同的)。我的想法是使用 Zipkin 服务器来跨服务获取这些日志的集体可视化。但我知道并且已经使用过 ELK 堆栈,它必然具有相同的功能。我的意思是我们可以使用 ELK 堆栈将具有相同 traceId 的请求分组以进行可视化。但我确实看到人们尝试使用 Sleuth、ELK 以及 Zipkin 来实现分布式跟踪,如这些示例(Link1、Link2)中所示。但是如果已经有 ELK 来进行日志收集和可视化,为什么我们还需要 Zipkin呢?我哪里失踪了?
zipkin microservices elastic-stack spring-cloud-sleuth distributed-tracing
随着 SpringBoot 3.0.0-M1 和 SpringCloud 2022.0.0-M1 的发布,我继续升级版本。
\nhttps://spring.io/blog/2022/01/20/spring-boot-3-0-0-m1-is-now-available
\nhttps://spring.io/blog/2022/01/27/spring-cloud-2022-0-0-m1-codename-kilburn-has-been-released
\n虽然在我的 SpringBoot 2.6.3 + Jubilee 上一切正常,但完全相同的代码,只是将 pom 文件的版本更改为新的 SpringBoot 3.0.0-M1 + SpringCloud 2022.0.0-M1 对,但以下错误是观察到的。
\n/Library/Java/JavaVirtualMachines/jdk-17.0.2.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=abc -Dmaven.home=maven3 [...] -Didea.version=2021.3.1 clean install\n[INFO] Scanning for projects...\n[ERROR] [ERROR] Some problems were encountered while processing the POMs:\n[ERROR] \'dependencies.dependency.version\' for org.springframework.cloud:spring-cloud-starter-sleuth:jar is missing. @ line xx, column 21\n[ERROR] \'dependencies.dependency.version\' for org.springframework.cloud:spring-cloud-sleuth-zipkin:jar is missing. @ line xx, column 21\n @ \nRun Code Online (Sandbox Code Playgroud)\n根据 Spring Cloud 官方文档:
\nSpring Cloud Sleuth\n\nSpring Cloud Sleuth\xe2\x80\x99s last …Run Code Online (Sandbox Code Playgroud)