这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件。java
1、简介web
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.spring
------ 摘自官网apache
Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,而且兼容支持了 zipkin,你只须要在pom文件中引入相应的依赖便可。浏览器
2、服务追踪分析
微服务架构上经过业务来划分服务的,了解springcloud架构能够加求求:三五三六二四七二五九,经过REST调用,对外暴露的一个接口,可能须要不少个服务协同才能完成这个接口功能,若是链路上任何一个服务出现问题或者网络超时,都会造成致使接口调用失败。随着业务的不断扩张,服务之间互相调用会愈来愈复杂。网络
随着服务的愈来愈多,对调用链的分析会愈来愈复杂。架构
3、术语
Span:基本工做单元,例如,在一个新建的span中发送一个RPC等同于发送一个回应请求给RPC,span经过一个64位ID惟一标识,trace以另外一个64位ID表示,span还有其余数据信息,好比摘要、时间戳事件、关键值注释(tags)、span的ID、以及进度ID(一般是IP地址)span在不断的启动和中止,同时记录了时间信息,当你建立了一个span,你必须在将来的某个时刻中止它。app
4.1 构建server-zipkin
在spring Cloud为F版本的时候,已经不须要本身构建Zipkin Server了,只须要下载jar便可,下载地址:maven
https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/分布式
也能够在这里下载:
连接: https://pan.baidu.com/s/1w614Z8gJXHtqLUB6dKWOpQ 密码: 26pf
下载完成jar 包以后,须要运行jar,以下:
java -jar zipkin-server-2.10.1-exec.jar
访问浏览器localhost:9494
4.2 建立service-hi
在其pom引入起步依赖spring-cloud-starter-zipkin,代码以下:
<?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>
<groupId>com.forezp</groupId> <artifactId>service-zipkin</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-hi</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.forezp</groupId> <artifactId>sc-f-chapter9</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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
在其配置文件application.yml指定zipkin server的地址,头经过配置“spring.zipkin.base-url”指定:
server.port=8988 spring.zipkin.base-url=http://localhost:9411 spring.application.name=service-hi
经过引入spring-cloud-starter-zipkin依赖和设置spring.zipkin.base-url就能够了。
对外暴露接口:
package com.forezp; import brave.sampler.Sampler; 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; import java.util.logging.Level; import java.util.logging.Logger; @SpringBootApplication @RestController public 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 Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
4.3 建立service-miya
建立过程痛service-hi,引入相同的依赖,配置下spring.zipkin.base-url。
对外暴露接口:
package com.forezp; import brave.sampler.Sampler; 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; import java.util.logging.Level; import java.util.logging.Logger; @SpringBootApplication @RestController public 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(); } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
4.4 启动工程,演示追踪
依次启动上面的工程,打开浏览器访问:http://localhost:9411/,会出现如下界面:
访问:http://localhost:8989/miya,浏览器出现:
i’m service-hi
再打开http://localhost:9411/的界面,点击Dependencies,能够发现服务的依赖关系:
点击find traces,能够看到具体服务相互调用的数据: