调用链跟踪是微服务架构中的基础能力,Spring Cloud Zipkin+Sleuth为我们提供了该能力。首先我们先建立Zipkin服务端,它需要集成Eureka,用于发现服务提供方和消费方,进行数据的采集存储、数据分析与UI展示;然后我们在服务提供方和消费方中植入Zipkin+Sleuth作为客户端,进行追踪数据的生成与上报。
zikpin服务端(三板斧):
1、pom里引入第三方(Twitter的)zipkin的jar包:
<?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> <groupId>com.example</groupId> <artifactId>zipkin</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>2.11.8</version> <exclusions> <exclusion> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.11.8</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR2</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>
注意标黄部分,日志jar包冲突会让zipkin无法启动的。
2、application:
#本机端口 server.port=8899 #服务名 spring.application.name=zipkin #注册中心地址 eureka.client.service-url.defaultZone=http://localhost:8888/eureka/ #关闭自动收集web请求 management.metrics.web.server.auto-time-requests=false
3、主类通过@EnableZipkinServer启动zipkin:
package hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import zipkin2.server.internal.EnableZipkinServer; @EnableZipkinServer @EnableEurekaClient @SpringBootApplication public class ZipkinApplication { public static void main(String[] args) { SpringApplication.run(ZipkinApplication.class, args); } }
zipkin客户端只需在pom里引入spring-cloud-starter-zipkin即可:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
我们在a-bootiful-client、a-beautiful-client和a-feign-client中都引入zipkin客户端,现在打开localhost:8899点击Find Traces即可搜到它们最近1个小时的调用链:
随便点一个看看: