问题背景
上一篇文章使用了sleuth日志形式的链路追踪,但微服务过多的时候,这种模式显得非常臃肿无力,因此这个篇章使用zipkin可视化界面来查看追踪链路
- 默认已安装JDK
- 可以根据前几篇的文章和这篇这篇文章自己创建工程,也可以直接使用测试源码
- zipkin服务端安装包下载
- zipkin依赖是包含sleuth依赖的
- zipkin源码官网文档
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+FeignClient+Nacos通过网关远程调用微服务(一)
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Sleuth链路追踪(二)
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Zipkin安装部署可视化http方式链路追踪(三)
Gateway+Nacos+Sleuth+Zipkin网关链路追踪(测试及源码),Gateway+Nacos+Zipkin持久化mysql存储rabbitmq消息队列链路追踪(四)
zipkin的下载安装部署
1 前面几篇文章讲了nacos服务端的安装,这篇文章讲解一下zipkin的安装,zipkin有官方提供的jar包可以直接时候,本文作为测试工程就直接下载,没有自己创建了
2 下载zipkin服务端,讲一个通用的方法,以后大家也可以以这种方式下载
3 打开 https://search.maven.org/ 网站
4 在红框中输入需要查找的jar包,这里输入zipkin-server
5 选择红框44所有版本
6 我选择的是最新版本,2.23.16版,下载exec.jar版本才可以执行,官网下载慢,也可以下载我上传的,在上文注意事项中
7 把java放入linux运行,可以在本地运行,但前提都要有JDK环境,我个人把日志打印出来了
nohup java -jar zipkin-server-2.23.16-exec.jar >> zipkin.log &
8 默认端口为9411,登录http://127.0.0.1:9411/zipkin,这里使用自己的IP即可
项目搭建
1 根据上一篇sleuth文章,更改sleuth的依赖为zipkin,zipkin依赖是包含sleuth依赖的
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
2 gateway的pom
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yg</groupId>
<artifactId>gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gateway</name>
<description>gateway</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<!-- <version>2.1.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- <scope>test</scope>-->
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!-- <version>2.1.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3 service的公共pom
<?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.yg</groupId>
<artifactId>sleuthTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>service1</module>
<module>service2</module>
</modules>
<name>sleuthTest</name>
<description>sleuthTest</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<!-- <version>2.1.0.RELEASE</version>-->
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--Spring Cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
4 gateway添加application配置
server:
port: 3900
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
routes:
- id: service2 #设置路由id(理论上是可以随便写的)
uri: lb://service2 #设置路由的url lb://nacos服务注册名称 lb://service1 http://localhost:3901
predicates:
- Path=/three/** #路径匹配规则,微服务必须有一个统一的入口,不然网关不能访问,@RequestMapping("/three")
- id: service1 #设置路由id(理论上是可以随便写的)
uri: lb://service1 #设置路由的url lb://nacos服务注册名称 lb://service1 http://localhost:3901
predicates:
- Path=/four/** #路径匹配规则,@RequestMapping("/four")
zipkin:
base-url: http://10.10.195.199:9411/
sender:
type: web
locator:
discovery:
enabled: true
sleuth:
sampler:
probability: 1.0
5 service1添加application配置
server:
port: 3901
spring:
application:
name: service1
cloud:
nacos:
discovery:
server-addr: localhost:8848
zipkin:
base-url: http://10.10.195.199:9411/
sender:
type: web
locator:
discovery:
enabled: true
sleuth:
sampler:
probability: 1.0
6 service2添加application配置
server:
port: 3902
spring:
application:
name: service2
cloud:
nacos:
discovery:
server-addr: localhost:8848
zipkin:
base-url: http://10.10.195.199:9411/
sender:
type: web
locator:
discovery:
enabled: true
sleuth:
sampler:
probability: 1.0
7 其他不用更改
测试步骤
1 启动nacos服务端,zipkin服务端,gateway,service1,service2
2 查看注册中心http://localhost:8848/nacos,密码和账号都为:nacos
3 使用网关路由调用service1微服务API,service1微服务调用service2
4 查看zipkin,点击RUN QUERY,显示调用链路
5 点击红框,查看依赖链路
6 通过注册中心使用微服务名显示链路关系
心得
- 通过zipkin可视化界面看起来更清晰
- 这个篇章讲的是http链路关系,此方式是同步的方式,微服务过多会产生一定延时,下个篇章介绍使用rabbitMQ的方式
作为程序员第 20 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha ...