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.
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:
Below we will use a simple example to show how to integrate Zipkin and Spring Cloud Sleuth in a Spring Boot application.
First, we need to create a Spring Boot project. You can use Spring Initializr to quickly create a project and add the following dependencies:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-sleuth</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-zipkin</artifactId>
- </dependency>
- </dependencies>
existapplication.properties
Add the following configuration to the file:
- spring.application.name=my-service
- server.port=8080
-
- spring.zipkin.base-url=http://localhost:9411
- spring.sleuth.sampler.probability=1.0
spring.zipkin.base-url
Specifies the address of the Zipkin server.spring.sleuth.sampler.probability
A setting of 1.0 means all trace data is recorded.Create a simple REST controller to simulate the service call:
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- @RestController
- public class MyController {
-
- private final RestTemplate restTemplate;
-
- public MyController(RestTemplate restTemplate) {
- this.restTemplate = restTemplate;
- }
-
- @GetMapping("/hello")
- public String hello() {
- String response = restTemplate.getForObject("http://localhost:8081/greeting", String.class);
- return "Hello, " + response;
- }
- }
In this example,/hello
The endpoint calls another service/greeting
endpoint.
You can quickly start a Zipkin server using Docker:
docker run -d -p 9411:9411 openzipkin/zipkin
Start your Spring Boot application and accesshttp://localhost:8080/hello
Endpoint. At this point, Spring Cloud Sleuth will automatically generate tracing information for your requests and send it to the Zipkin server.
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.
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.