Spring Cloud tutorial series eight: Distributed Link Tracking Spring Cloud Sleuth (F version)

Introduction

When the system resolution, a front end of the link call request becomes longer, it calls a plurality of different back-end service. When a request to slow down or unavailable, in order to facilitate the positioning problems, so there distributed link tracking.

Distributed link tracking increases in the request traceId and spanId, a link that contains more than one traceId spanId
such as a front-end order system call request, call again and order system inventory system.

The system logs the following order (as this is not the default format, the following format may be configured, the default format will be mentioned later)

[INFO][traceId=aaa][SpanId=123]

Inventory system log is as follows

[INFO][traceId=aaa][SpanId=456]

Traceid a link request, the same value in different systems, a plurality spanId, the values ​​are different in different systems.

Distributed link tracking has a lot to achieve, here briefly and integration of Zipkin

github address: https: //github.com/erlieStar/spring-cloud-learning

Integration with Zipkin

Creating zipkin-service

After spring boot 2.x version, the official is not recommended to compile, but to provide the compiled jar package for our use, can be downloaded directly start

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

After starting at http: // localhost: 9411 / zipkin
Here Insert Picture Description

Creating Consumer Services

Sample project: consumer-zipkin (spring-cloud-sleuth)

1. Project configuration is as follows

pom.xml

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

application.yaml

server:
  port: 8080

spring:
  zipkin:
    base-url: http://localhost:9411
  application:
    name: consumer-zipkin
  sleuth:
    sampler:
      probability: 1.0 # 采样比例,1.0表示全部采集

2. Start classes are as follows

@Slf4j
@RestController
@SpringBootApplication
public class ConsumerZipkin {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerZipkin.class);
    }

    @Autowired
    private RestTemplate restTemplate;

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @GetMapping("hello")
    public String hello() {
        log.info("start invoke");
        return restTemplate.getForObject("http://localhost:8090/hello", String.class);
    }
}

Creating producer services

Sample project: producer-zipkin (spring-cloud-sleuth)

1. Project configuration is as follows

pom.xml

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

application.yaml

server:
  port: 8090

spring:
  zipkin:
    base-url: http://localhost:9411
  application:
    name: producer-zipkin
  sleuth:
    sampler:
      probability: 1.0 # 采样比例,1.0表示全部采集

2. Start classes are as follows

@Slf4j
@RestController
@SpringBootApplication
public class ProducerZipkin {

    public static void main(String[] args) {
        SpringApplication.run(ProducerZipkin.class);
    }

    @RequestMapping("hello")
    public String hello() {
        log.info("new request");
        return "hi zipkin";
    }
}

Began calling

访问http://localhost:8080/hello

zipkin analyze the relationship between the call as follows
Here Insert Picture Description
consumer-zikpin print log is as follows

INFO [consumer-zipkin,53836e18c9552b3f,53836e18c9552b3f,true] 6664 --- [nio-8080-exec-1] com.javashitang.ConsumerZipkin           : start invoke

printing log as producer-zipkin

INFO [producer-zipkin,53836e18c9552b3f,6eefb28892009df6,true] 20832 --- [nio-8090-exec-1] com.javashitang.ProducerZipkin           : new request

Analyze log format

[consumer-zipkin,53836e18c9552b3f,53836e18c9552b3f,true] 

The first value: application name
second value: traceid
third value: spanId
fourth value: whether to collect outputs the information services and the like to display zipkin

Enter traceId, analyze the following call link
Here Insert Picture Description

Welcome attention

Here Insert Picture Description

Published 385 original articles · won praise 1471 · Views 900,000 +

Guess you like

Origin blog.csdn.net/zzti_erlie/article/details/104335926