上一篇简介了ZipkinServer的搭建,但是从Spring boot2.x版本后,Zipkin官网已经不再推荐自己搭建定制Zipkin,而是直接提供了编译好的jar包。详情可以查看官网:
https://zipkin.io/pages/quickstart.html
有了Zipkin Server还不能对微服务的调用链路进行人祸监控,Zipkin Server可以被认为是一个数据处理和展示中心,那它的数据哪里来呢?需要Zipkin Client作为代理连接到Zipkin Server源源不断的上送过来。今天讲解一下如何在微服务中引入Zipkin Client,然后结合Zipkin Server监控各微服务间的调用链路。整体调用链路图如下:
涉及的项目:
注册中心:sc-eureka-server
Zipkinserver:sc-zipkin-server
微服务:sc-zipkin-client-web、sc-zipkin-client-service
1、 新建项目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。
2、 新建配置文件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
`
3、 sc-zipkin-client-service(普通的微服务)项目其他项目文件如下图
4、 新建项目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
5、 新建配置文件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
`
6、 sc-zipkin-client-web(普通的微服务)项目其他项目文件如下图
7、 验证
项目启动顺序:
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