First, Introduction
With the development of the business, the scale of the system will become bigger and larger, and the call relationship between the microservices becomes more and more complex. Typically a client-initiated request will be used by several different microservices calls in the backend system to produce the final request result, and in a complex microservices architecture system, almost every front-end request forms a complex distributed service invocation link, It is possible to cause the final failure of the request when either of the dependent services in each link is too late or an error occurs. At this point, the tracking of full-link calls becomes more and more important for each request, and by implementing a trace of the request invocation can help us quickly identify the source of the error and monitor the performance bottlenecks on each request link.
Spring Cloud Sleuth provides a complete set of solutions for the distributed service tracking problem described above.
Second, Zipkin
Zipkin is an open source project for Twitter, based on Google Dapper. We can use it to collect trace data from the request link on each server, and through its Rest API interface to assist the query tracking data to implement the monitoring program of the distributed system, so as to detect the delay rise problem in the system and find out the root of the system performance bottleneck. In addition to the development-oriented API interface, it also provides a handy UI component to help us visually search for trace information and analyze request link details, such as querying the processing time of each user request over a certain period of time.
The following figure shows the infrastructure of Zipkin, which consists mainly of 4 core components.
Collector: Collector component, which mainly handles tracking information sent from external systems, translates this information into a zipkin internal processing span format to support subsequent storage, analysis, and display functions. Storage: A storage component that primarily handles the trace information received by the collector, which is stored in memory by default. We can also modify this storage policy to store trace information in the database by using other storage components.
RESTful Api:api component, which is primarily used to provide external access to interfaces. For example, to show the client tracking information, or external system access to implement monitoring.
Web Ui:ui component, an upper-level application based on API component implementation. Through the UI components, users can easily and intuitively query and analyze trace information.
third, build Zipkin Server
(1) Create a new Springboot project , naming no requirements;
(2) Pom.xml, adding Zipkin dependency
<dependencies>
<dependency>
<groupId>io.zipkin.java</groupId>
< artifactid>zipkin-server</artifactid>
</dependency>
<dependency>
<groupid >io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</ dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
< artifactid>spring-boot-starter-web</artifactid>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactid>spring-boot-starter-test </artifactId>
<scope>test</scope>
</dependency>
</dependencies>
(3) application.yml
Spring:
application:
name:zipkin-server
server:
port:5595
(4) Entrance class
@EnableZipkinServer
@SpringBootApplication Public
class Springcloudsleuthzipkinapplication {
public static void Main (string[] args) {
springapplication.run (springcloudsleuthzipkinapplication.class, args);
}
}
(5) After creating the above project, we will start it up and visit http:i/localhost:5595/, you can see the Zipkin administration page as shown in the following illustration:
Iv. Construction of Trace1-client (1) Create a springboot project , naming no requirements;
(2) Pom.xml
<?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.example</grou Pid> <artifactId>springcloudsleuthtrace1</artifactId> <version>0.0.1-snapshot</version&
Gt <packaging>jar</packaging> <name>springcloudsleuthtrace1</name> <description>demo P Roject for Spring boot</description> <parent> <groupid>org.springframework.boot</groupid > <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.release</vers Ion> <relativePath/> <!--lookup parent from repository to </parent> <PR Operties> <projEct.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputEncoding> Utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> <spring-clo ud.version>edgware.sr1</spring-cloud.version> </properties> <dependencies> <depen Dency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-st arter-web</artifactid> </dependency> <dependency> <GROUPID>ORG.SPRINGF Ramework.cloud</groupid> <artifactId>spring-cloud-starter-zipkin</artifactId> </d Ependency> <dependency> <groupId>org.springframework.boot</groupId> & Lt;artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </depe Ndency> </dependencies> <dependencyManagement> <dependencies> <dependency> &L T;groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-dependencies</art Ifactid> <version>${spring-cloud.version}</version> <type>pom</typ
e> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <gr Oupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifacti
d> </plugin> </plugins> </build> </project>
(3) application.yml
Server:
port:5596
Spring:
zipkin:
base-url:http://localhost:5595
Application:
Name: Trace1-client
(4) Entrance class
@RestController
@EnableDiscoveryClient
@SpringBootApplication Public
class springcloudsleuthtrace1application {
private static final Logger log = Loggerfactory.getlogger ( Springcloudsleuthtrace1application.class);
@Bean
@LoadBalanced
resttemplate resttemplate () {
return new resttemplate ();
}
@RequestMapping (value = "/trace1", method = requestmethod.get) public
String Trace () {
log.info ("<< <<<-call trace1->>>>> ");
Return Resttemplate (). Getforentity ("Http://localhost:5597/trace2", String.class). GetBody ();
public static void Main (string[] args) {
springapplication.run (springcloudsleuthtrace1application.class, args);
}
}
v. Construction of Trace2-client
(1) Create a springboot project, naming no requirements;
(2) Pom.xml
<?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.example</groupid> <artifactId>springcloudsleuthtrace2</artifactId> <version>0.0.1-
Snapshot</version> <packaging>jar</packaging> <name>springcloudsleuthtrace2</name> <description>demo Project for Spring boot</description> <parent> <groupId> Org.springframework.boot</groupid> <artifactId>spring-boot-starter-parent</artifactId> < Version>1.5.9.release</version> <relativePath/> <!--lookup parent from repository to </paren t> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <p roject.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8< /java.version> <spring-cloud.version>Edgware.SR1</spring-cloud.version> </properties> < dependencies> <dependency> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPR ing-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework .cloud</groupid> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> < Dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> </dependencies > <dependencyManagement> <dependencies> <dependency> <groupid>org.springframework.cl Oud</groupid> <artifactid>spring-cloud-dependencies</artifactid> <version>${spring-cloud.version}</version> <type>pom</type> <scope>i mport</scope> </dependency> </dependencies> </dependencyManagement> <build> <PL ugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-
maven-plugin</artifactid> </plugin> </plugins> </build> </project>
(3) application.yml
Server:
port:5597
Spring:
zipkin:
base-url:http://localhost:5595
Application:
Name: Trace2-client
(4) Entrance class
@RestController
@EnableDiscoveryClient
@SpringBootApplication Public
class springcloudsleuthtrace2application {
private static final Logger log = Loggerfactory.getlogger ( Springcloudsleuthtrace2application.class);
@RequestMapping (value = "/trace2", method = requestmethod.get) public
String Trace () {
log.info ("=====< <<call trace2>>>===== ");
return "Trace";
}
public static void Main (string[] args) {
springapplication.run (springcloudsleuthtrace2application.class, args);
}
}
VI. Testing
(1) Start zipkin-server--port 5595;trace1-client--Port 5596;trace2-client--Port 5597 respectively;
(2) Visit Http://localhost:5596/trace1
The result is HTTP://LOCALHOST:5597/TRACE2 content, the program passes:
Resttemplate (). Getforentity ("Http://localhost:5597/trace2", String.class). GetBody ();
Use Resttemplate to invoke the interface of the Trace2 app.
(3) Open Zipkin management interface--dependency analysis
(4) Open the Zipkin management interface--Find the call chain
(6) Click on the red box above
with the above steps, you can see the data that the specific service calls to each other
Reference "Spring Cloud micro-service Combat"
a novice, welcome to shoot Bricks ~ ~ ~