springcloud-sleuth +zipkin completes log tracking

introduce

In the microservice architecture, with the development of business, the system becomes more and more complex, and the calling relationship becomes more and more complicated. Almost every request will form a call chain, and each chain may go wrong due to various reasons. At this time, it is found that tracking the full link call is becoming more and more important.
Srping Cloud Sleuth provides a complete solution. An ID generated by Sleuth, called Trace ID, is used to identify a request link. A request link contains a Trace ID and multiple Span IDs.

1. First, sleuth creates a TraceFilter to intercept all network requests. If there is no span information in the header of the request, create a Span object, generate span id, trace id, etc., if there is in the header, directly use the data in the header to create Span object
2. Then set the span id and trace id to the MDC of sl4j. When using RestTemplate to send a request, the RestTemplateInterceptorInjector interceptor intercepts the request, and sets the newly generated span id, trace id and other information into the header of the request. In this way, the server can parse the Span information from the header after receiving the request

1. Install zipkin

curl -sSL https://zipkin.io/quickstart.sh | bash -s

2. Start zipkin

nohup java -jar zipkin.jar >> zipkin.log 2>&1 &

Visit the zipkin client page localhost:9411

insert image description here

3. Introduce the dependencies of sleuth and zipkin in the springcloud project

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud </groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

4. Configuration file

spring.zipkin.enabled=true
spring.zipkin.discovery-client-enabled=false
spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0

5. Start the project

call an interface
insert image description here

Guess you like

Origin blog.csdn.net/qq_46645840/article/details/129087209