上一篇简介了ZipkinServer的搭建,可是从Spring boot2.x版本后,Zipkin官网已经再也不推荐本身搭建定制Zipkin,而是直接提供了编译好的jar包。详情能够查看官网:web
https://zipkin.io/pages/quickstart.htmlspring
有了Zipkin Server还不能对微服务的调用链路进行人祸监控,Zipkin Server能够被认为是一个数据处理和展现中心,那它的数据哪里来呢?须要Zipkin Client做为代理链接到Zipkin Server源源不断的上送过来。今天讲解一下如何在微服务中引入Zipkin Client,而后结合Zipkin Server监控各微服务间的调用链路。总体调用链路图以下:apache
涉及的项目:app
注册中心:sc-eureka-servermaven
Zipkinserver:sc-zipkin-serveride
微服务:sc-zipkin-client-web、sc-zipkin-client-servicespring-boot
一、 新建项目sc-zipkin-client-service,对应的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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud</groupId>
<artifactId>sc-zipkin-client-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-zipkin-client-service</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-web</artifactId>
</dependency>
</dependencies>
</project>
备注:主要引入了spring-cloud-starter-zipkin,说明这是一个zipkin client。
二、 新建配置文件application.yml
eureka:
client:
serviceUrl:
defaultZone:http://localhost:5001/eureka/
server:
port: 9201
spring:
application:
name: sc-zipkin-client-service
zipkin:
base-url:http://localhost:9000
`
三、 sc-zipkin-client-service(普通的微服务)项目其余项目文件以下图
四、 新建项目sc-zipkin-client-web,对应的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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-cloud</groupId>
<artifactId>sc-zipkin-client-web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-zipkin-client-web</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
`备注:一样引入了spring-cloud-starter-zipkin,说明是一个zipkin client
五、 新建配置文件application.yml
eureka:
client:
serviceUrl:
defaultZone:http://localhost:5001/eureka/
server:
port: 9202
spring:
application:
name: sc-zipkin-client-web
zipkin:
base-url: http://localhost:9000
`
六、 sc-zipkin-client-web(普通的微服务)项目其余项目文件以下图
七、 验证
项目启动顺序:
sc-eureka-server
sc-zipkin-server
sc-zipkin-client-service
sc-zipkin-client-web
访问注册中心:http://127.0.0.1:5001/
服务都已经注册成功
访问Zinkin Server:http://localhost:9000/zipkin/
目前zipkin server没有记录任何的微服务调用链路数据。
分别访问接口:
http://127.0.0.1:9202/user/listUser
http://127.0.0.1:9202/user/getUser/1
再次查看Zipkin Server(若是没有出现能够多访问几回接口,Zipkin须要更多的监控数据)
源码:
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-zipkin-client-web
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/sc-zipkin-client-service