Spring Cloud -Hoxton.RELEASE(七):监控中心-Sleuth+Zipkin

Spring Cloud Sleuth

Sleuth官网对Sleuth描述如下:

Spring Cloud Sleuth implements a distributed tracing solution for Spring Cloud, borrowing heavily from Dapper, Zipkin and HTrace. For most users Sleuth should be invisible, and all your interactions with external systems should be instrumented automatically. You can capture data simply in logs, or by sending it to a remote collector service.

Spring Cloud Sleuth为Spring Cloud实现了分布式跟踪解决方案,大量借用 Dapper、Zipkin 和 HTrace。对于大多数用户,Sleuth 应该是不可见的,并且您与外部系统的所有交互都应自动进行检测。只需在日志中捕获数据,也可以将其发送到远程收集器服务。

简单来讲官方对Sleuth的定位是捕获和发送日志
我们用来和Zipkin结合使用。

Zipkin

Zipkin的官网对Zipkin的描述如下:

Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in service architectures. Features include both the collection and lookup of this data.

Zipkin是一种分布式跟踪系统。它有助于收集解决服务体系结构中的延迟问题所需的计时数据。功能包括收集和查找此数据。

简单来讲官方对Zipkin的定位是跟踪,收集和查找日志

通过web收集日志信息

从Spring Boot 2.x版本开始官方就建议使用官方编译的Jar包启动Zipkin服务了。
最新版下载地址是:https://search.maven.org/remote_content?g=io.zipkin&a=zipkin-server&v=LATEST&c=exec

下载完成后为方便可以改名为zipkin.jar,然后在所在目录下运行powershell命令java -jar zipkin.jar,稍后访问http://localhost:9411/zipkin/
01.jpg
但是这时候还没有日志来源,所以接下来我们改造之前的producer-client项目和feign项目,让他们通过Sleuth来发送日志到Zipkin。

producer-client项目添加Sleuth和Zipkin依赖

修改producer-client项目的pom.xml为:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.liuzhuoming23</groupId>
<artifactId>server-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>

<artifactId>producer-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>producer-client</name>
<description>Demo project for Spring Cloud</description>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

修改application.yml为:

server:
port: 8010

spring:
application:
name: @pom.artifactId@
cloud:
bus:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
sleuth:
web:
client:
#是否开启sleuth
enabled: true
sampler:
#收集日志的比例,1.0=100%,默认0.1=10%
probability: 1.0
zipkin:
#zipkin地址
base-url: http://localhost:9411/
locator:
discovery:
#启用通过服务发现查找主机名
enabled: true
sender:
#日志收集方式为web
type: web

management:
endpoints:
web:
exposure:
include: refresh,info,health

然后重启producer-client项目。

feign项目添加Sleuth和Zipkin依赖

同producer-client项目,自行添加,不再赘述。
之后同样重启feign项目。

发送及收集查询日志

像之前一样用Postman请求http://localhost:8030/feign/port/🐕,然后访问http://localhost:9411/zipkin/,点击主页的查看按钮会显示已经统计到的日志列表:
02.jpg
可以看到请求的链路日志已经统计在Zipkin里面了,点击展开详情:
03.jpg
即可以看到微服务里面的请求顺序和花费时间等详情。

通过RabbitMQ收集日志信息

直接通过web方式收集日志会让服务和Zipkin服务过于耦合,所以我们可以使用RabbitMQ来中转收集到的日志信息来解耦。
首先关闭Zipkin服务,并运行java -jar zipkin.jar --zipkin.collector.rabbitmq.uri=amqp://guest:guest@localhost:5672命令启动Zipkin服务。
启动完成之后打开RabbitMQ的管理页面http://localhost:15672,进入Queue选项卡,会发现已经添加了zipkin的队列:
04.jpg
然后修改producer-client项目和feign项目。

修改producer-client项目为通过RabbitMQ收集日志信息

因为producer-client项目已经添加了RabbitMQ的依赖,所以不需要做改动。
修改application.yml为:

server:
port: 8010

spring:
application:
name: @pom.artifactId@
cloud:
bus:
enabled: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
sleuth:
web:
client:
enabled: true
sampler:
probability: 1.0
zipkin:
base-url: http://localhost:9411/
locator:
discovery:
enabled: true
sender:
#使用rabbit方式发送日志
type: rabbit

management:
endpoints:
web:
exposure:
include: refresh,info,health

之后重启项目。

修改feign项目为通过RabbitMQ收集日志信息

修改pom.xml为:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.liuzhuoming23</groupId>
<artifactId>route-center</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<packaging>jar</packaging>

<artifactId>feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>feign</name>
<description>Demo project for Spring Cloud</description>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

修改application.yml为:

server:
port: 8020

spring:
application:
name: @pom.artifactId@
sleuth:
web:
client:
enabled: true
sampler:
probability: 1.0
zipkin:
base-url: http://localhost:9411/
locator:
discovery:
enabled: true
sender:
type: rabbit
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest

eureka:
client:
service-url:
defaultZone: http://admin:admin@localhost:8000/eureka/

feign:
hystrix:
enabled: true

之后重启项目。

发送及收集查询日志

和web方式步骤完全相同,自行尝试,不再赘述。