背景 上一篇只是单纯的从原理上以及控制台上去实践系统之间的打通,但是如果能从页面上去看每一个请求日志的链路情况就更好了。其实zipkin是提供了一个UI后台管理给到我们的。
**注意点:**关于 Zipkin 的服务端,在使用 Spring Boot 2.x 版本后,官方就不推荐自行定制编译了,反而是直接提供了编译好的 jar 包来给我们使用。具体请查阅:https://zipkin.io/pages/quickstart.html (最直接、权威的就是阅读官方网站)
1 2 curl -sSL https://zipkin.io/quickstart.sh | bash -s java -jar zipkin.jar
快速上手 Zipkin 分为两端,一个是 Zipkin 服务端,一个是 Zipkin 客户端,客户端也就是微服务的应用。 客户端会配置服务端的 URL 地址,一旦发生服务间的调用的时候,会被配置在微服务里面的 Sleuth 的监听器监听,并生成相应的 Trace 和 Span 信息发送给服务端。 发送的方式主要有两种,一种是 HTTP 报文的方式,还有一种是消息总线的方式如 RabbitMQ。
不论哪种方式,我们都需要:
一个 Eureka 服务注册中心,这里我们就用之前的 eureka
项目来当注册中心。
一个 Zipkin 服务端。
三个微服务应用,trace-a
和 trace-b
和 trace-c
,其中 trace-a
中有一个 REST 接口 /trace-a
,调用该接口后将触发对 trace-b
应用的调用,最后调用trace-c
。其实2个服务就够了~
环境说明 也是因为一些环境问题,总是导致ui后管一直没有数据,这次参考了很多文章一级官网网址来操作的,才得以显示数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <parent > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-parent</artifactId > <version > 2.1.2.RELEASE</version > </parent > <properties > <spring-cloud-dependencies.version > Finchley.RELEASE</spring-cloud-dependencies.version > </properties > <dependencyManagement > <dependencies > <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-dependencies</artifactId > <version > ${spring-cloud-dependencies.version}</version > <type > pom</type > <scope > import</scope > </dependency > </dependencies > </dependencyManagement >
方式一:通过HTTP 客户端的pom文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <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-zipkin</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-logging</artifactId > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > </dependency > </dependencies >
客户端的properties文件内容如下:
1 2 3 4 5 6 7 8 9 10 11 spring.application.name =trace-a server.port =10001 eureka.client.serviceUrl.defaultZone =http://localhost:8001/eureka/ spring.sleuth.web.client.enabled =true spring.sleuth.sampler.probability =1.0 spring.zipkin.base-url =http://localhost:9411 logging.level.org.springframework.web.servlet.DispatcherServlet =DEBUG
访问层部分Java代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 package com.allei;import java.util.concurrent.TimeUnit;import lombok.extern.slf4j.Slf4j;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;@RestController @EnableDiscoveryClient @SpringBootApplication @Slf4j public class TraceAApplication { @Bean @LoadBalanced RestTemplate restTemplate () { return new RestTemplate (); } @RequestMapping(value = "/trace-a", method = RequestMethod.GET) public String trace () throws InterruptedException { log.info("call trace-a----->" ); TimeUnit.SECONDS.sleep(1l ); return restTemplate().getForEntity("http://trace-b/trace-b" , String.class).getBody(); } public static void main (String[] args) { SpringApplication.run(TraceAApplication.class, args); } }
还是确定一下注册中心,保证服务都有起来。
通过上面的脚本拉取最新的zipkin服务,此时的版本是:2.19.2
1 2 3 4 5 6 7 8 9 ➜ Desktop curl -sSL https://zipkin.io/quickstart.sh | bash -s Thank you for trying Zipkin! This installer is provided as a quick-start helper, so you can try Zipkin out without a lengthy installation process. Fetching version number of latest io.zipkin:zipkin-server release... Latest release of io.zipkin:zipkin-server seems to be 2.19.2 Downloading io.zipkin:zipkin-server:2.19.2:exec to zipkin.jar...
并且启动服务:嗯~ logo已经更换的更加的骚气~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ➜ Desktop java -jar zipkin.jar oo oooo oooooo oooooooo oooooooooo oooooooooooo ooooooo ooooooo oooooo ooooooo oooooo ooooooo oooooo o o oooooo oooooo oo oo oooooo ooooooo oooo oooo ooooooo oooooo ooooo ooooo ooooooo oooooo oooooo oooooo ooooooo oooooooo oo oo oooooooo ooooooooooooo oo oo ooooooooooooo oooooooooooo oooooooooooo oooooooo oooooooo oooo oooo ________ ____ _ _____ _ _ |__ /_ _| _ \| |/ /_ _| \ | | / / | || |_) | ' / | || \| | / /_ | || __/| . \ | || |\ | |____|___|_| |_|\_\___|_| \_| :: version 2.19.2 :: commit 56d907b ::
当浏览器访问trace-a
服务的时候,去后管页面查看就能看到服务列表以及日志情况。
图一:
图二:
方式二:RabbitMQ 安装RabbitMQ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ➜ ~ brew install rabbitmq Updating Homebrew... ==> Auto-updated Homebrew! Updated 4 taps (homebrew/cask, caskroom/versions, caskroom/cask and adoptopenjdk/openjdk). ==> Downloading https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.0/rabbitmq-server-generic-unix-3.8.0.tar.xz ==> Downloading from https://github-production-release-asset-2e65be.s3.amazonaws.com/924551/71891480-e44b-11e9-80fd-c06739f04c5e?X-Amz-Algorith # ==> /usr/bin/unzip -qq -j /usr/local/Cellar/rabbitmq/3.8.0/plugins/rabbitmq_management-3.8.0.ez rabbitmq_management-3.8.0/priv/www/cli/rabbitmq ==> Caveats Management Plugin enabled by default at http://localhost:15672 Bash completion has been installed to: /usr/local/etc/bash_completion.d To have launchd start rabbitmq now and restart at login: brew services start rabbitmq Or, if you don't want/need a background service you can just run: rabbitmq-server ==> Summary 🍺 /usr/local/Cellar/rabbitmq/3.8.0: 277 files, 18.2MB, built in 34 seconds ==> `brew cleanup` has not been run in 30 days, running now... Removing: /usr/local/Cellar/jpeg/9b... (20 files, 724KB) Removing: /usr/local/Cellar/libpng/1.6.34... (26 files, 1.2MB) Removing: /usr/local/Cellar/libtiff/4.0.8_5... (245 files, 3.4MB) Removing: /usr/local/Cellar/wxmac/3.0.3.1_1... (810 files, 24.3MB) Removing: /Users/chenyuan/Library/Caches/Homebrew/Cask/minikube--0.25.0... (41.3MB) Removing: /Users/chenyuan/Library/Logs/Homebrew/go... (64B) Removing: /Users/chenyuan/Library/Logs/Homebrew/readline... (64B) Removing: /Users/chenyuan/Library/Logs/Homebrew/sqlite... (64B) Removing: /Users/chenyuan/Library/Logs/Homebrew/kubernetes-cli... (64B) Pruned 0 symbolic links and 2 directories from /usr/local
安装的路径在:/usr/local/Cellar/rabbitmq/3.8.0
,利用brew安装软件速度可能会很慢,大家可以去更换一下源就变好了。但这个其实也不一定。
启动RabbitMQ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ➜ sbin rabbitmq-server # # # # # Doc guides: https://rabbitmq.com/documentation.html Support: https://rabbitmq.com/contact.html Tutorials: https://rabbitmq.com/getstarted.html Monitoring: https://rabbitmq.com/monitoring.html Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log /usr/local/var/log/rabbitmq/rabbit@localhost_upgrade.log Config file(s): (none) Starting broker... completed with 6 plugins.
默认密码账号为guest/guest。登录进入后就能看到界面了。
配置客户端pom文件:
1 2 3 4 5 # 新加入 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-stream-binder-rabbit</artifactId > </dependency >
为了觉得不是http的模式导致的数据,故意让地址写错
1 spring.zipkin.base-url =http://localhost:9412
然后客户端就什么都不用管了。但是服务端因为是下载的Jar的方式,让其支持RabbitMQ只能通过环境变量的方式来启动。
1 RABBIT_ADDRESSES=localhost java -jar zipkin.jar
这个时候会发现RabbitMQ会有一个zipkin的队列。
可配置的环境变量如下表所示:
属性
环境变量
描述
zipkin.collector.rabbitmq.concurrency
RABBIT_CONCURRENCY
并发消费者数量,默认为 1
zipkin.collector.rabbitmq.connection-timeout
RABBIT_CONNECTION_TIMEOUT
建立连接时的超时时间,默认为 60000
毫秒,即 1 分钟
zipkin.collector.rabbitmq.queue
RABBIT_QUEUE
从中获取 span 信息的队列,默认为 zipkin
zipkin.collector.rabbitmq.uri
RABBIT_URI
符合 RabbitMQ URI 规范 的 URI,例如 amqp://user:pass@host:10000/vhost
如果设置了 URI,则以下属性将被忽略。
属性
环境变量
描述
zipkin.collector.rabbitmq.addresses
RABBIT_ADDRESSES
用逗号分隔的 RabbitMQ 地址列表,例如 localhost:5672,localhost:5673
zipkin.collector.rabbitmq.password
RABBIT_PASSWORD
连接到 RabbitMQ 时使用的密码,默认为 guest
zipkin.collector.rabbitmq.username
RABBIT_USER
连接到 RabbitMQ 时使用的用户名,默认为 guest
zipkin.collector.rabbitmq.virtual-host
RABBIT_VIRTUAL_HOST
使用的 RabbitMQ virtual host,默认为 /
zipkin.collector.rabbitmq.use-ssl
RABBIT_USE_SSL
设置为 true
则用 SSL 的方式与 RabbitMQ 建立链接
然后访问 http://localhost:10001/trace-a 并刷新 Zipkin UI,看到如下内容,就说明 Sleuth+Zipkin+RabbitMQ 整合成功了。
参考地址