In the architectural design of today’s Internet applications, distributed systems have become the norm. In such a complex system, locating the fault point when a problem occurs is a very challenging task. To solve this problem, developers need to use distributed tracing tools to uncover the mysteries of the application black box. This article will introduce Sleuth and Zipkin, two popular distributed tracing tools, to help developers better monitor and debug distributed systems.
With the proliferation of microservicesarchitectures and complex distributed systems, it has become critical to track the flow of requests and responses across components and services . DistributedTrackingVisualizationThe application execution process reveals performance bottlenecks, dependencies and anomalies.
Sleuth: Spring Boot’s tracking tool
Sleuth is a lightweight distributed tracing framework for Spring Boot applications. It integrates with spring cloud Sleuth Starter to provide tracking capabilities out of the box. Simply add dependencies to automatically capture events such as Http requests, Database calls, and remote service calls.
Sample code:
@Configuration public class SleuthConfig { @Bean public Sampler sampler() { return Sampler.ALWAYS_SAMPLE; } }
Zipkin: A visualization tool for tracking data
Zipkin is an open source platform for collecting, storing and querying tracking data. It provides an interactive user interface that allows users to intuitively explore trace data and identify dependencies and performance issues.
Sample code:
import io.zipkin.reporter.AsyncReporter; import io.zipkin.reporter.okhttp3.Okhttpsender; import io.zipkin.zipkin2.Span; // 使用 OkHttp 发送器将追踪数据发送到 Zipkin 服务器 OkHttpSender sender = OkHttpSender.newBuilder().endpoint("http://localhost:9411/api/v2/spans").build(); // 使用异步报告器,提高性能 AsyncReporter reporter = AsyncReporter.newBuilder(sender).build(); // 上报追踪信息到 Zipkin 服务器 reporter.report(span);
The marriage of Sleuth and Zipkin
Sleuth’s integration with Zipkin allows easy export of tracking data from the Sleuth application to the Zipkin platform. This integration is possible via the spring Cloud Sleuth Zipkin Starter.
Sample code:
@Configuration public class SleuthZipkinConfig { @Bean public ZipkinSender sender() { return new ZipkinSender(); } @Bean public SpanReporter reporter() { return new SpanReporter.Builder(sender()).build(); } }
Benefits of distributed tracing
Distributed tracing has the following advantages in application development and maintenance:
in conclusion
Sleuth and Zipkin are a powerful combination of distributed tracing, giving developers deep insight into an application’s internal logic, improving performance and reliability. By integrating these two tools into distributed systems, you can significantly improve application observability and gain the insights you need to control, optimize, and troubleshoot.
The above is the detailed content of Sleuth and Zipkin: Distributed tracing, uncovering the mysteries of application black boxes. For more information, please follow other related articles on the PHP Chinese website!