Sleuth brief introduction
Microservice architecture divides services by services. Exposed interfaces may require many services to cooperate to complete an interface function. If any service on the link has problems, interface calls will fail. At this time, it is very difficult to find the microservices with problems. The main function of Spring Cloud Sleuth is to provide tracking solutions in distributed systems, and it is compatible with zipkin.
Sleuth Server
zipkin server does not need to build its own code. It can directly use the official one. Download address: https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/
After downloading the jar package, you need to run the jar, java -jar zipkin-server-2.10.1-exec.jar
1. Build a service provider
Under micro parent, create a new module, micro zipkin client, add zipkin and other related dependencies. pom.xml 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> <artifactId>micro-zuul-client</artifactId> <packaging>jar</packaging> <name>micro-zuul-client</name> <description>Microservice practice</description> <parent> <groupId>com.sun</groupId> <artifactId>micro-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
Profile:
server: port: 7091 spring: application: name: micro-zipkin-client zipkin: base-url: http://Localhost: 9411 ා Zipkin service address eureka: client: service-url: defaultZone: http://localhost:8080/eureka
The startup class provides an external method:
package com.sun.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class ZipkinApplication { public static void main(String[] args) { SpringApplication.run(ZipkinApplication.class, args); } @RequestMapping("/hello") public String hello(){ return "hello"; } }
2. Build a service consumer
Under micro parent, create a new module, micro zipkin server, add zipkin and other related dependencies. pom.xml 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> <artifactId>micro-zipkin-server</artifactId> <packaging>jar</packaging> <name>micro-zipkin-server</name> <description>Microservice practice</description> <parent> <groupId>com.sun</groupId> <artifactId>micro-parent</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> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies> </project>
Profile:
spring: zipkin: base-url: http://Localhost: 9411 ා Zipkin server application: name: micro-zipkin-client server: port: 7090 eureka: client: service-url: defaultZone: http://localhost:8080/eureka
Startup class:
package com.sun.eureka; 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; @SpringBootApplication @RestController public class BusApplication { public static void main(String[] args) { SpringApplication.run(BusApplication.class, args); } @Autowired private RestTemplate restTemplate; @Bean @LoadBalanced public RestTemplate getRestTemplate(){ return new RestTemplate(); } @RequestMapping("/hi") public String callHome(){ return restTemplate.getForObject("http://MICRO-ZIPKIN-CLIENT/hello", String.class); } }
Start mirco Eureka server, mirco Zipkin server, mirco Zipkin client; access http://localhost:7090/hi Show:
open http://localhost:9411/zipkin/
From the service name, you can see the mirco Zipkin server and mirco Zipkin client services. Click find traces to see the data of specific services calling each other
Click the service again to see the details:
Click dependency analysis to see the service dependency:
Source address: