How to Use Zipkin for Tracing in Microservices
To use
Zipkin for tracing, you first instrument your microservices to send trace data to a Zipkin server. Then, run the Zipkin server to collect and visualize traces, helping you track requests across services.Syntax
Using Zipkin involves three main parts:
- Instrumentation: Add Zipkin tracing libraries to your services to capture trace data.
- Zipkin Server: Run the Zipkin server to receive and store trace data.
- Visualization: Use the Zipkin UI to view and analyze traces.
Each service sends trace data via HTTP or messaging to the Zipkin server endpoint.
bash
zipkin-server --port=9411 # Example HTTP POST to Zipkin server POST http://localhost:9411/api/v2/spans Content-Type: application/json [{"traceId":"abc123","id":"def456","name":"getUser","timestamp":1234567890000,"duration":3500}]
Example
This example shows a simple Java Spring Boot microservice instrumented with Zipkin using Spring Cloud Sleuth. It automatically sends trace data to a running Zipkin server.
java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class ZipkinExampleApplication { public static void main(String[] args) { SpringApplication.run(ZipkinExampleApplication.class, args); } } @RestController class HelloController { @GetMapping("/hello") public String hello() { return "Hello from Zipkin traced service!"; } } // application.properties // spring.zipkin.base-url=http://localhost:9411/ // spring.sleuth.sampler.probability=1.0
Output
When you call http://localhost:8080/hello, the request is traced and sent to Zipkin server at http://localhost:9411 where you can view the trace.
Common Pitfalls
- Not running the Zipkin server before starting instrumented services causes trace data loss.
- Setting sampling probability too low (default is 0.1) may miss important traces; set to 1.0 for full tracing during debugging.
- Forgetting to configure the Zipkin server URL in your service causes traces to not be sent.
- Not propagating trace context across service calls breaks the trace chain.
properties
Wrong configuration example: // application.properties spring.zipkin.base-url=http://wronghost:9411/ Right configuration example: // application.properties spring.zipkin.base-url=http://localhost:9411/ spring.sleuth.sampler.probability=1.0
Quick Reference
| Step | Description |
|---|---|
| 1. Instrument Services | Add Zipkin or Sleuth libraries to your microservices. |
| 2. Run Zipkin Server | Start the Zipkin server on port 9411 to collect traces. |
| 3. Configure Services | Set Zipkin server URL and sampling rate in service configs. |
| 4. Make Requests | Call your services to generate trace data. |
| 5. View Traces | Open Zipkin UI at http://localhost:9411 to analyze traces. |
Key Takeaways
Instrument your microservices with Zipkin libraries to capture trace data.
Run the Zipkin server before starting your services to collect traces.
Configure the Zipkin server URL and sampling rate correctly in your services.
Use the Zipkin UI to visualize and analyze distributed traces.
Ensure trace context propagates across service calls for complete tracing.