微服务架构上通过业务来划分服务的,通过REST调用,对外暴露的一个接口,可能需要很多个服务协同才能完成这个接口功能,如果链路上任何一个服务出现问题或者网络超时,都会形成导致接口调用失败。随着业务的不断扩张,服务之间互相调用会越来越复杂。
随着服务的越来越多,对调用链的分析会越来越复杂。它们之间的调用关系也许如下:
将Span和Trace在一个系统中使用Zipkin注解的过程图形化:
以上内容转自:https://blog.csdn.net/forezp/article/details/70162074
Zipkin分布式跟踪系统;它可以帮助收集时间数据,解决在microservice架构下的延迟问题;它管理这些数据的收集和查找;Zipkin的设计是基于谷歌的Google Dapper论文。
每个应用程序向Zipkin报告定时数据,Zipkin UI呈现了一个依赖图表来展示多少跟踪请求经过了每个应用程序;如果想解决延迟问题,可以过滤或者排序所有的跟踪请求,并且可以查看每个跟踪请求占总跟踪时间的百分比。
随着业务越来越复杂,系统也随之进行各种拆分,特别是随着微服务架构和容器技术的兴起,看似简单的一个应用,后台可能有几十个甚至几百个服务在支撑;一个前端的请求可能需要多次的服务调用最后才能完成;当请求变慢或者不可用时,我们无法得知是哪个后台服务引起的,这时就需要解决如何快速定位服务故障点,Zipkin分布式跟踪系统就能很好的解决这样的问题。
在目前的新版本中,Zipkin已独立成一个jar包,我们只需要下载它(文末有下载链接),用‘java -jar zipkin.jar’命令去运行它就好了,运行成功,我们访问http://localhost:9411/zipkin/ 注意这里最后一定要有‘/’不然会报404,或者你可以直接访问http://localhost:9411 出现如下界面,就表示,你的Zipkin服务启动成功了。
只有在对外暴露的接口被调用时,Zipkin才会收集数据,这就是为什么叫服务追踪了。
创建两个新Model,我将他们命名为Service-Hey,Service-Hello。两个项目的pom.xml文件的依赖一致,我就只贴出Service-Hey的pom.xml文件了。
<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.0modelVersion>
<groupId>cn.ponygroupId>
<artifactId>service_helloartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>service_helloname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.3.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<spring-cloud.version>Finchley.RELEASEspring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zipkinartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
而后,我们修改Service-Hey的启动类。
@SpringBootApplication
@RestController
public class ServiceHeyApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHeyApplication.class, args);
}
private static final Logger LOG = Logger.getLogger(ServiceHeyApplication.class.getName());
@Autowired
private RestTemplate restTemplate;
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@RequestMapping("/hello")
public String hello(){
LOG.log(Level.INFO, "hey开始调用跟踪了");
return "你好呀,这里是service-hey";
}
@RequestMapping("/hey")
public String info(){
LOG.log(Level.INFO, "hey开始调用跟踪了");
return restTemplate.getForObject("http://localhost:9009/info",String.class);
}
@Bean
public Sampler defaultSampler(){
return Sampler.ALWAYS_SAMPLE;
}
}
修改Service-Hello的启动类。
@SpringBootApplication
@RestController
public class ServiceHelloApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHelloApplication.class, args);
}
private static final Logger LOG = Logger.getLogger(ServiceHelloApplication.class.getName());
@Autowired
private RestTemplate restTemplate;
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
@RequestMapping("/hello")
public String hello(){
LOG.log(Level.INFO, "开始调用跟踪service-hello");
return restTemplate.getForObject("http://localhost:9008/hey", String.class);
}
@RequestMapping("/info")
public String info(){
LOG.log(Level.INFO, "开始调用跟踪service-hello");
return "你好呀,这里是service-hello";
}
@Bean
public Sampler defaultSampler(){
return Sampler.ALWAYS_SAMPLE;
}
}
启动两个项目,访问http://localhost:9009/hello,出现:
你好呀,这里是service-hello
然后,我们再打开Zipkin的管理界面,http://localhost:9411/zipkin/,选择依赖分析,可以发现服务之间的依赖关系:
选择查找调用链,可以看到具体服务相互调用的数据:
点击下面蓝色的链接,可以看到更为详细的信息:
链接: https://pan.baidu.com/s/1dXDVFKqkjvRTeDxQSfD2Ig 密码: y6nm