Table of contents
1.1.1, the request processing process in the microservice framework
2.2.2, yaml configuration file
2.3.1, POM and yaml files are the same as the service provider
1 Overview
1.1. Existing problems
1.1.1, the request processing process in the microservice framework
- The client initiates a request
- In the backend system, it is called by multiple different service nodes
- produces result
1.1.2, causing problems
Each front-end request will form a complex distributed service call link, and high latency or errors in any link in the link will cause the final failure of the entire request
1.2. Solution
- SpringCloud Sleuth provides a complete set of service tracking solutions
- Provides tracking solutions in distributed systems and is compatible with zipkin
2. Examples
2.1、zipkin
2.1.1. Download
2.1.2, run
Enter in the command line terminal: java -jar zipkin-server-2.12.9-exec.jar
2.1.3, run the console
http://localhost:9411/zipkin
2.2. Service Provider
2.2.1, POM file
Introduce spring-cloud-starter-zipkin dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
2.2.2, yaml configuration file
Add zipkin and sleuth properties
spring:
application:
name: cloud-payment-service #微服务名称
zipkin:
base-url: http://localhost:9411
sleuth:
sampler:
# 采样率介于0到1之间,1表示全部采样
probability: 1
2.2.3、Controller
Services provided externally
@GetMapping("/payment/zipkin")
public String paymentZipkin() {
return "hi, i am Zipkin";
}
2.3. Service consumers
2.3.1, POM and yaml files are the same as the service provider
2.3.2、Controller
Call the url of the service provider
@GetMapping("/consumer/payment/zipkin")
public String paymentZipkin() {
String result = restTemplate.getForObject("http://localhost:8001" + "/payment/zipkin/", String.class);
return result;
}
2.4. Test
1. Start service provider and service consumer
2. Open the browser and visit http://localhost:9411
Get the microservice calling relationship:
get dependencies: