This article mainly talks about the service tracking component zipkin, and 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.
― Excerpted from the official website
Spring Cloud Sleuth's main function is to provide tracking solutions in distributed systems, and it is compatible with zipkin. You only need to introduce corresponding dependencies into the pom file.
2. Service tracking and analysis
In the microservice architecture, services are divided through services. An interface exposed to the outside world may require many services to collaborate to complete this interface function. If any service on the link has problems or network timeouts, it will cause the interface call to fail. As the business continues to expand, the call between services will become more and more complicated.
As more and more services are available, the analysis of call chains will become more and more complicated. The call relationship between them may be as follows:
III. Terms
Span: Basic unit of work. For example, sending an RPC in a newly created span is equivalent to sending a response request to the RPC. The span is uniquely identified by a 64-bit ID, and the trace is represented by another 64-bit ID. The span has other data information, such as digest, timestamp events, key value annotations (tags), span's ID, and progress ID (usually IP address).
The span is constantly starting and stopping, and at the same time records the time information. When you create a span, you must stop it at some point 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: used to record the existence of an event in time, and some core annotations are used to define the beginning and end of a request.
Graphicalize the process of using Zipkin annotation in one system:
4. Construction project
After explaining the basic knowledge, let’s take the actual work. The case in this article mainly consists of three projects: a server-zipkin, which mainly uses the functions of ZipkinServer to collect call data and display it; a service-hi, which exposes the hi interface to the outside; a service-miya, which exposes the miya interface to the outside; these two services can be called each other; and only after they are called, server-zipkin will collect data, which is why it is called service tracking.
4.1 Build server-zipkin
Create a spring-boot project named server-zipkin, and introduce dependencies in its pom:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> </dependency> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
In its program entry class, add the annotation @EnableZipkinServer to enable the function of ZipkinServer:
@SpringBootApplication@EnableZipkinServerpublic class ServerZipkinApplication { public static void main(String[] args) { SpringApplication.run(ServerZipkinApplication.class, args); }}In the configuration file application.yml specifies the service port as:
server.port=9411
4.2 Create service-hi
In its pom introduction, the initial dependency is spring-cloud-starter-zipkin, the code is as follows:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--compile('org.springframework.cloud:spring-cloud-starter-zipkin')--> <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> </dependency> </dependency> </dependency> <dependencyManagement> <dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>In its configuration file application.yml, specify the address of the zipkin server, and the header is specified by configuring "spring.zipkin.base-url":
server.port=8988spring.zipkin.base-url=http://localhost:9411spring.application.name=service-hi
It's OK to introduce the spring-cloud-starter-zipkin dependency and set spring.zipkin.base-url.
External exposure interface:
@SpringBootApplication@RestControllerpublic 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 AlwaysSampler defaultSampler(){ return new AlwaysSampler(); }}4.3 Create service-miya
The creation process hurts service-hi, introduces the same dependencies, and configures spring.zipkin.base-url.
External exposure interface:
@SpringBootApplication@RestControllerpublic 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(); }}4.4 Start the project, demonstrate and track
Start the above three projects in turn, open the browser to access: http://localhost:9411/, and the following interface will appear:
Visit: http://localhost:8989/miya, the browser appears: i'm service-hi
Then open the interface of http://localhost:9411/ and click Dependencies to discover the dependencies of the service:
Click find traces to see the data of the specific services calling each other:
Download the source code of this article: https://github.com/forezp/SpringCloudLearning/tree/master/chapter9
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.