1, What is Sleuth distributed link request tracking
Official website: https://github.com/spring-cloud/spring-cloud-sleuth
In the microservice framework, a request initiated by the client will be called by multiple different service nodes in the back-end system to produce the final request result. Each request will form a complex distributed service call link. The high delay or error of any link in the link will cause the final failure of the whole request.
Spring Cloud Sleuth provides a complete service tracking solution for this problem, and supports zipkin.
Simply put, Sleuth integrates zipkin.
What is zipkin
It can be simply understood that the tracking data sent by service calls between microservices is collected by Sleuth and presented through zipkin.
2, Installation and construction of zipkin server
Spring Cloud does not need to build Zipkin Server itself from the F version. It only downloads the corresponding jar package locally and then calls the jar package.
Download address: https://developer.aliyun.com/mvn/search , select the warehouse as the keyword Zipkin server in Alibaba cloud warehouse to search.
Then click the jar package version you need, and then click the download link to download it.
How to run zipkin server?
First find the file directory downloaded by the jar package, and then run the following jar package command in the command window:
java -jar zipkin-server-2.23.5-exec.jar
Then enter the address in the browser address bar: http://localhost:9411/zipkin/ You can view the zipkin dashboard panel page.
zipkin full call link
The figure shows a request link. A link is uniquely identified by Trace Id, span identifies the request information initiated, and each span is associated by parent id
It can be simplified to:
The dependencies of the whole link are as follows:
Explanation of related terms:
Trace: a Span set similar to a tree structure. It represents a calling link. There is a unique identification, that is, a Trace Id can only identify one service calling link.
Span: refers to the source of the calling link. Generally, span can be understood as a request for information.
3, Implementation of Sleuth link monitoring display
1. First configure the authoring service provider
(1) In POM Add the following dependencies to XML
<!--Contains sleuth+zipkin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
(2) In application Add the following configuration to the YML configuration file
spring: application: name: cloud-provider-payment zipkin: base-url: http://localhost:9411 sleuth: sampler: #The sample rate value is between 0 and 1, and 1 means all collection probability: 1
The added position is shown in the figure:
(3) Write controller (here only write controller for simple demonstration)
// =======> zipkin @GetMapping("/payment/zipkin") public String paymentZipkin() { return "hi ,i'am paymentzipkin server fall back,welcome to here,O(∩_∩)O ha-ha~"; }
2. Configure authoring service consumers
(1) In POM Add the following dependencies to XML
<!--Contains sleuth+zipkin--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
(2) In application Add the following configuration to YML
spring: application: name: cloud-consumer-order zipkin: base-url: http://localhost:9411 sleuth: sampler: #The sample rate value is between 0 and 1, and 1 means all collection probability: 1
(3) Write exposed interfaces
/** * @Author Herz * @Date 2021/12/30 17:08 */ @Component @FeignClient(value = "CLOUD-PROVIDER-PAYMENT") public interface PaymentFeignService { @GetMapping("/payment/zipkin") String paymentZipkin(); }
(4) Write controller
/** * @Author Herz * @Date 2021/12/30 17:14 */ @RestController @Slf4j public class OrderController { @Autowired private PaymentFeignService paymentFeignService; // =======> zipkin @GetMapping("/consumer/payment/zipkin") public String paymentZipkin() { return paymentFeignService.paymentZipkin(); } }
Test: start the service provider, service consumer and registry, and start Zipkin server
Enter the address in the address field to send the request: http://localhost/consumer/payment/zipkin (I refreshed it four times and sent five requests in total)
Enter address http://localhost:9411/zipkin/ , and then select the query condition serverName to view the calls and dependencies between microservices