Technology Sharing

Distributed tracing with Zipkin and Spring Cloud Sleuth

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

In a microservice architecture, the call chain between services can be very complex, which makes it difficult to locate problems and optimize performance. To solve this problem, we can use a distributed tracing system to monitor and diagnose the request chain throughout the microservice architecture. Zipkin and Spring Cloud Sleuth are two very popular tools that can help us achieve this goal.

What are Zipkin and Spring Cloud Sleuth?

  • ZipkinIt is a distributed tracing system that helps us collect timing data in microservice architectures so that we can analyze and monitor system performance.
  • Spring Cloud SleuthIt is a Spring Cloud project that provides automatic distributed tracing capabilities for Spring Boot applications and can be integrated with Zipkin.

Why do we need distributed tracing?

In a microservice architecture, a user request may be processed by multiple services. Without a distributed tracing system, when a problem occurs, it is difficult to determine which service or service call link the problem lies in. A distributed tracing system can help us:

  • Quickly locate performance bottlenecks.
  • Monitor the calling relationships between services.
  • Analyze and optimize system performance.

Integrating Zipkin and Spring Cloud Sleuth

Below we will use a simple example to show how to integrate Zipkin and Spring Cloud Sleuth in a Spring Boot application.

1. Create a Spring Boot project

First, we need to create a Spring Boot project. You can use Spring Initializr to quickly create a project and add the following dependencies:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-sleuth</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-starter-zipkin</artifactId>
  13. </dependency>
  14. </dependencies>

2. Configure the application

existapplication.propertiesAdd the following configuration to the file:

  1. spring.application.name=my-service
  2. server.port=8080
  3. spring.zipkin.base-url=http://localhost:9411
  4. spring.sleuth.sampler.probability=1.0
  • spring.zipkin.base-urlSpecifies the address of the Zipkin server.
  • spring.sleuth.sampler.probabilityA setting of 1.0 means all trace data is recorded.

3. Write sample code

Create a simple REST controller to simulate the service call:

  1. import org.springframework.web.bind.annotation.GetMapping;
  2. import org.springframework.web.bind.annotation.RestController;
  3. import org.springframework.web.client.RestTemplate;
  4. @RestController
  5. public class MyController {
  6. private final RestTemplate restTemplate;
  7. public MyController(RestTemplate restTemplate) {
  8. this.restTemplate = restTemplate;
  9. }
  10. @GetMapping("/hello")
  11. public String hello() {
  12. String response = restTemplate.getForObject("http://localhost:8081/greeting", String.class);
  13. return "Hello, " + response;
  14. }
  15. }

In this example,/helloThe endpoint calls another service/greetingendpoint.

4. Start the Zipkin server

You can quickly start a Zipkin server using Docker:

docker run -d -p 9411:9411 openzipkin/zipkin

5. Run the application

Start your Spring Boot application and accesshttp://localhost:8080/helloEndpoint. At this point, Spring Cloud Sleuth will automatically generate tracing information for your requests and send it to the Zipkin server.

6. View tracking information

Open your browser and visithttp://localhost:9411, you will see the Zipkin web interface. Here you can view all the tracking information, including the time taken for each request, the call link, etc.

Summarize

By integrating Zipkin and Spring Cloud Sleuth, we can easily implement distributed tracing in microservice architecture. This not only helps us quickly locate and solve problems, but also helps us optimize system performance. I hope this blog can help you understand and use these two powerful tools.

References