是否有任何方法可以在 Java 代码中获取 Zipkin 的 TraceId

我想检索Zipkin的TraceId,有什么方法可以得到吗?


侃侃尔雅
浏览 905回答 2
2回答

蝴蝶刀刀

如果服务 2 正在从服务 1 获取 traceId,您可以在 java 代码中从 requestHeader 获取 traceId。否则 sleuth 在服务 2 中生成一个新的 traceId。在java中获取跟踪ID    @Autowired     private Tracer tracer;做就是了    tracer.getCurrentSpan().traceIdString();

芜湖不芜

您好,您还可以从请求中获取 x-b3-traceid 标头信息,我为此创建了一个 Util 类-> https://gist.github.com/walterwhites/067dd635986e564aafdb5ac559073b0fpublic final class DebugUtils {&nbsp; &nbsp; private static String PURPLE = "\033[0;35m";&nbsp; // PURPLE&nbsp; &nbsp; private static String RED = "\u001B[31m";&nbsp; // RED&nbsp; &nbsp; private static String RESET = "\u001B[0m";&nbsp; &nbsp; public static class ZipkinDebug {&nbsp; &nbsp; &nbsp; &nbsp; private static String url = "http://localhost:9411/zipkin/traces/";&nbsp; &nbsp; &nbsp; &nbsp; public static void displayTraceUrl(HttpServletRequest request) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String traceId = request.getHeader("x-b3-traceid");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(PURPLE + "DebugUtils:ZipkinDebug -> " + url + traceId + RESET);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static class RequestInfo {&nbsp; &nbsp; &nbsp; &nbsp; public static void displayAllRequestHeaders(HttpServletRequest request) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Enumeration<String> headerNames = request.getHeaderNames();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(RED + "DebugUtils:RequestInfo -> " + RESET);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headerNames.asIterator().forEachRemaining(header -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Header Name:" + header + "&nbsp; &nbsp;" + "Header Value:" + request.getHeader(header));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public static void displayRequestHeader(HttpServletRequest request, String headerName) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(RED + "DebugUtils:RequestInfo -> Header Name:" + headerName + "&nbsp; &nbsp;" + "Header Value:" + request.getHeader(headerName) + RESET);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}然后在你的主班,你只需要打电话ZipkinDebug.displayTraceUrl(request);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java