关于 Java 应用程序的小问题,请使用 Brave / Zipkin 进行跟踪。
我有一段非常简单的代码(附加了代码+ maven pom,准备运行并重现问题)
import io.netty.handler.logging.LogLevel;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import reactor.netty.transport.logging.AdvancedByteBufFormat;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.brave.ZipkinSpanHandler;
import zipkin2.reporter.urlconnection.URLConnectionSender;
import brave.Tracing;
import brave.opentracing.BraveTracer;
import java.util.Map;
public class QuestionApplication {
public static void main(String[] args) {
final var sender = URLConnectionSender.create("http://the-zipkin-instance.com:9411/api/v2/spans");
final var spanReporter = AsyncReporter.create(sender);
final var tracing = Tracing.newBuilder().localServiceName("thisIsTheClientApplicationWhoIsGeneratingTheFirstTraceId").addSpanHandler(ZipkinSpanHandler.create(spanReporter)).build();
final var tracer = BraveTracer.create(tracing).unwrap().tracer();
final var webClient = WebClient.create().mutate().clientConnector(new ReactorClientHttpConnector(HttpClient.create().wiretap("reactor.netty.http.client.HttpClient", LogLevel.INFO, AdvancedByteBufFormat.HEX_DUMP))).build();
final var jsonPayload = Map.of("key", "someKey", "value", "someValue");
final var span = tracer.currentSpan();
final var context = span.context();
final var traceIdString = context.traceIdString();
final var response = webClient.post().uri("http://the-server.com/api/route").header("X-B3-TraceId", traceIdString).body(BodyInserters.fromValue(jsonPayload)).retrieve().bodyToMono(String.class).block();
System.out.println(response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github</groupId>
<artifactId>question</artifactId>
<version>1.1</version>
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-urlconnection</artifactId>
<version>2.16.3</version>
</dependency>
<dependency>
<groupId>io.opentracing.brave</groupId>
<artifactId>brave-opentracing</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
在这里,使用上面的代码,我得到一个java.lang.NullPointerException
在线final var context = span.context();
,因为跨度为空final var span = tracer.currentSpan();
这个应用程序是“客户端”应用程序,意味着第一个发起 http 调用,因此,我希望这小段主代码是第一个生成跟踪 ID 的,它将在十几个其他子系统中传播。
请问问题是什么,这个 NPE 的根本原因是什么,或者我应该说,创建这个跟踪 ID 以传递的正确方法是什么,因为我是链中的第一个?
谢谢