Zipkin是一个分布式跟踪系统,Sleuth负责为服务之间的调用提供链路追踪,而Zipkin用于收集在微服务体系结构中延迟问题排除所需的定时数据。通俗来说就是收集各微服务应用的接口调用过程所消耗的时间数据,方便开发人员快速定位解决接口性能问题。
spring-cloud-starter-zipkin中已包含了spring-cloud-starter-sleuth与spring-cloud-sleuth-zipkin的依赖,所以使用sleuth-zipkin的SpringCloud项目只需添加spring-cloud-starter-zipkin依赖即可
根模块
-
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <properties> <zipkin-version>2.11.8</zipkin-version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> <version>2.0.1.RELEASE</version> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>${zipkin-version}</version> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>${zipkin-version}</version> </dependency> </dependencies> </dependencyManagement>
cloud-zipkin模块
-
pom.xml
<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> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> </dependency> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> </dependency> </dependencies>
-
ZipkinApplication.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import zipkin2.server.internal.EnableZipkinServer; @SpringBootApplication @EnableZipkinServer public class ZipkinApplication { public static void main(String[] args) { SpringApplication.run(ZipkinApplication.class); } }
-
application.xml
server: port: 9411 #port: 9000 spring: application: name: zipkin #zipkin: #base-url: http://localhost:9000/ management: metrics: web: server: #避免访问zipkin页面报java.lang.IllegalArgumentException auto-time-requests: false
cloud-consumer模块
-
pom.xml
<dependencies> <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>
-
ConsumerApplication.java
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class); } @GetMapping("/index") public String index() { return "index"; } }
-
application.yml
server: port: 9999 spring: application: name: consumer sleuth: sampler: probability: 1.0 #2.0后percentage改为probability
执行结果
访问消费者接口http://localhost:9999/index,在刷新zipkin页面便可发现consumer服务已添加到Service Name选择框,点击下侧服务名上的蓝色框即可查看该次接口调用的详细信息。
注:若不调用相应接口对应的服务名不会显示到选择框,一开始还以为自己配置错了。
Demo Github url:https://github.com/Wilson-He/spring-cloud-zipkin-demo