由于微服务体系结构已成为所有最新分布式系统的标准,因此从一种微服务到另一种微服务的呼叫跟踪一直是一项挑战。
为了解决这个问题,Sprig Cloud引入了春季侦探,该公司从dapper那里大量借用。 Spring sleuth将traceID和spanID添加到日志中,以便轻松跟踪微服务/ Web服务调用。
在本教程中,我们将实现Spring Sleuth并与齐普金集成,Zipkin是一个分布式跟踪系统,该系统提供了一个UI,使我们可以使用traceID搜索事务,并查看依赖关系图,该关系图显示了通过每个微服务有多少被跟踪的事务。
让我们分别创建三个spring boot微服务服务一 服务二 服务三。
在每个服务中,添加以下项的依赖关系:Spring Sleuth,Zipkin和春季启动启动器网站,结果pom.xml文件应如下所示。
如果您是视频人,这里是视频教程
Please show some love and subscribe to my channel Subscribe.
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>MainService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Service One</name>
<description>Spring Boot With Spring Sleuth</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
为三个具有GET端点/ serviceOne / serviceTwo / serviceThree的服务添加RestController,如下所示。
package com.springsleuth.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ServiceOneController {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOneController.class);
@Autowired
private RestTemplate restTemplate;
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@GetMapping("/serviceOne")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> getSleuthTest(){
LOGGER.info("I'm here in service calling service two");
String response = restTemplate.getForObject("http://localhost:8081/serviceTwo", String.class);
return new ResponseEntity<String>(response, HttpStatus.OK);
}
}
如果使用RestTemplate调用了服务并进行了自动装配,Spring Sleuth可以将相同的traceId集成到微服务链中,像上面的示例中那样创建restTemplate对象并自动装配,以便spring处理依赖项注入。
我们已经准备好serviceOne的端点,并为serviceTwo和serviceThree创建端点,与上述serviceOne相似,下面的示例是serviceTwo和serviceThree控制器的代码。
package com.springsleuth.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ServiceTwoController {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceTwoController.class);
@Autowired
private RestTemplate restTemplate;
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@GetMapping("/serviceTwo")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> getServiceTwo() throws Exception {
LOGGER.info("Here inside service two ");
String response = restTemplate.getForObject("http://localhost:8080/serviceThree", String.class);
return new ResponseEntity<String>(response, HttpStatus.OK);
}
}
package com.springsleuth.demo.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class ServiceThreeController {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceThreeController.class);
@Autowired
private RestTemplate restTemplate;
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@GetMapping("/serviceThree")
@ResponseStatus(HttpStatus.OK)
public ResponseEntity<String> getServiceThree() throws Exception {
LOGGER.info("Here inside service Three ");
return new ResponseEntity<String>("Success calling Spring Sleuth", HttpStatus.OK);
}
}
Application Properties
由于我们将在同一台机器上运行所有三个微服务,因此我们将它们运行在不同的端口上,因此请在相应项目的application.properties文件中设置端口。 appliction.properties文件将具有两个属性service.name和server.port,如下所示。
spring.application.name =服务一
server.port = 8080
Zipkin Setup
Zipkin是一个开源项目,您可以使用以下命令下载,有关Zipkin的更多信息,请访问Zipkin.io。
curl -sSL https://zipkin.io/quickstart.sh | bash -s
下载完成后,使用以下命令运行Zipkin jar。
java -jar zipkin.jar
可以在localhost:9094 / zipkin上看到Zipkin UI,如下所示。
现在,让我们在不同端口上启动所有三个微服务,并命中serviceOne端点。 将下面的日志与[serviceName,traceId,spanId,True / False]一起使用,这些都是Spring Sleuth所添加的,与我们无关。
2020-03-14 23:31:05.084信息[serviceOne,2ed9f774e90e9450,2ed9f774e90e9450,true] 17903-[nio-8082-exec-2] c.s.d.controller.MainServiceController:我在主服务调用服务1中
2020-03-14 23:31:05.181 INFO [serviceTwo,2ed9f774e90e9450,4bc477a78961a834,true] 17902 --- [nio-8081-exec-1] c.s.d.controller.ServiceTwoController:在服务内部
2020-03-14 23:31:05.304 INFO [serviceThree,2ed9f774e90e9450,dbc9d06429c2394a,true] 17901 --- [nio-8080-exec-1] c.s.d.controller.ServiceOneController:在服务内部
如果您观察以上日志,则三个微服务的所有日志的调用都具有相同的traceID,现在返回zipkin UI并使用traceID搜索,您将看到从一个服务到另一个服务的调用,如下所示,它还显示了时间 每次通话都采取。
如果您是视频人,这里是视频教程
from: https://dev.to//nagarajendra/spring-cloud-sleuth-tutorial-with-zipkin-22lc