实现基于Spring Cloud的微服务链路追踪
    		       		warning:
    		            这篇文章距离上次修改已过422天,其中的内容可能已经有所变动。
    		        
        		                
                
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class TracingController {
 
    private final Tracer tracer;
 
    public TracingController(Tracer tracer) {
        this.tracer = tracer;
    }
 
    @GetMapping("/trace-id")
    public String getTraceId() {
        Span currentSpan = tracer.getCurrentSpan();
        return currentSpan != null ? currentSpan.traceIdString() : "No active span found";
    }
}这段代码演示了如何在Spring Cloud应用中使用Tracer获取当前Span的跟踪ID。通过/trace-id的GET请求,可以获取当前请求的跟踪信息。这对于调试和监控微服务架构中的请求链路至关重要。
评论已关闭