This article focuses on the service tracking component zipkin. Spring Cloud Sleuth integrates the zipkin component.
1, Introduction
Add sleuth to the classpath of a Spring Boot application (see below
for Maven and Gradle examples), and you will see the correlation data
being collected in logs, as long as you are logging requests.
The main function of Spring Cloud Sleuth is to provide tracking solutions in distributed systems, and it is compatible with zipkin. You only need to introduce corresponding dependencies in pom files.
2, Service tracking analysis
In the microservice architecture, services are divided by business. An interface exposed through REST calls may require the cooperation of many services to complete this interface function. If any service on the link has a problem or the network times out, it will lead to the failure of interface call. With the continuous expansion of business, the mutual invocation between services will become more and more complex.
With more and more services, the analysis of call chain will become more and more complex. The calling relationship between them may be as follows:
3, Terminology
- Span: basic work unit. For example, sending an RPC in a new span is equivalent to sending a response request to the RPC. Span is uniquely identified by a 64 bit ID, and trace is represented by another 64 bit ID. span also has other data information, such as summary, timestamp events, key value comments (tags), span ID And the progress ID (usually the IP address) span keeps starting and stopping, and records the time information. When you create a span, you must stop it at some time in the future.
- Trace: a tree structure composed of a series of spans. For example, if you are running a distributed big data project, you may need to create a trace.
Annotation: it is used to record the existence of an event in time. Some core annotations are used to define the beginning and end of a request- cs - Client Sent - the client initiates a request. This annotation describes the beginning of the span
- sr - Server Received - the server obtains the request and is ready to start processing it. If you subtract the cs timestamp from its sr, you can get the network delay
- ss - Server Sent - the annotation indicates the completion of the request processing (when the request returns to the client). If ss subtracts the sr timestamp, the processing time required by the server can be obtained
- cr - Client Received - indicates the end of span. The client successfully receives the reply from the server. If cr subtracts the cs timestamp, all the time required for the client to obtain the reply from the server can be obtained. The process of span and Trace using Zipkin annotation in one system is graphical:
Graphize the process of Span and Trace using Zipkin annotation in one system:
4, Construction Engineering
After the basic knowledge is explained, let's practice. The case of this paper mainly consists of three projects: a server Zipkin, which uses the function of ZipkinServer to collect and display call data; A service hi that exposes the hi interface; A service miya that exposes the miya interface; The two services can call each other; And server Zipkin will collect data only when it is called, which is why it is called service tracking.
4.1 build server Zipkin
When spring Cloud is version F, you don't need to build Zipkin Server by yourself. You just need to download jar. Download address:
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
It can also be here https://gitee.com/frey6/spring-cloud-learn Download. After downloading the jar package, you need to run the jar as follows:
java -jar zipkin-server-2.10.1-exec.jar
Visit the browser localhost:9494
4.2 create service hi
Spring cloud starter Zipkin is introduced in its pom. The code is as follows:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.forezp</groupId> <artifactId>service-zipkin</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-hi</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.forezp</groupId> <artifactId>sc-f-chapter9</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
In its configuration file application YML specifies the address of the zipkin server. The header is specified by configuring "spring. Zipkin. Base URL":
server.port=8988
spring.zipkin.base-url=http://localhost:9411
spring.application.name=service-hi
By introducing spring cloud starter Zipkin dependency and setting spring zipkin. Base URL is OK.
External exposure interface:
package com.forezp; import brave.sampler.Sampler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.logging.Level; import java.util.logging.Logger; @SpringBootApplication @RestController public class ServiceHiApplication { public static void main(String[] args) { SpringApplication.run(ServiceHiApplication.class, args); } private static final Logger LOG = Logger.getLogger(ServiceHiApplication.class.getName()); @Autowired private RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } @RequestMapping("/hi") public String callHome(){ LOG.log(Level.INFO, "calling trace service-hi "); return restTemplate.getForObject("http://localhost:8989/miya", String.class); } @RequestMapping("/info") public String info(){ LOG.log(Level.INFO, "calling trace service-hi "); return "i'm service-hi"; } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
4.3 create service Miya
Create a service Hi, introduce the same dependencies, and configure spring zipkin. base-url.
External exposure interface:
package com.forezp; import brave.sampler.Sampler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.logging.Level; import java.util.logging.Logger; @SpringBootApplication @RestController public class ServiceMiyaApplication { public static void main(String[] args) { SpringApplication.run(ServiceMiyaApplication.class, args); } private static final Logger LOG = Logger.getLogger(ServiceMiyaApplication.class.getName()); @RequestMapping("/hi") public String home(){ LOG.log(Level.INFO, "hi is being called"); return "hi i'm miya!"; } @RequestMapping("/miya") public String info(){ LOG.log(Level.INFO, "info is being called"); return restTemplate.getForObject("http://localhost:8988/info",String.class); } @Autowired private RestTemplate restTemplate; @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
4.4 start the project, demonstrate and track
Start the above projects in sequence and open the browser to access: http://localhost:9411/ , the following interface will appear:
visit: http://localhost:8989/miya , the browser appears:
Here is the reference
i'm service-hi
Reopen http://localhost:9411/ Click Dependencies on the interface to find the Dependencies of the service:
Click find traces to see the data of specific services calling each other:
Download the source code of this article:
https://gitee.com/frey6/spring-cloud-learn
5, References
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html