Zipkin链路追踪HTTP与RabbitMQ方式

背景

上一篇只是单纯的从原理上以及控制台上去实践系统之间的打通,但是如果能从页面上去看每一个请求日志的链路情况就更好了。其实zipkin是提供了一个UI后台管理给到我们的。

注意点:关于 Zipkin 的服务端,在使用 Spring Boot 2.x 版本后,官方就不推荐自行定制编译了,反而是直接提供了编译好的 jar 包来给我们使用。具体请查阅:https://zipkin.io/pages/quickstart.html (最直接、权威的就是阅读官方网站)

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-atrace-btrace-c,其中 trace-a 中有一个 REST 接口 /trace-a,调用该接口后将触发对 trace-b 应用的调用,最后调用trace-c。其实2个服务就够了~

环境说明

也是因为一些环境问题,总是导致ui后管一直没有数据,这次参考了很多文章一级官网网址来操作的,才得以显示数据。

<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文件内容如下:

<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文件内容如下:

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

# log trace detail
logging.level.org.springframework.web.servlet.DispatcherServlet=DEBUG  

访问层部分Java代码如下:

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);
    }
}

还是确定一下注册中心,保证服务都有起来。

http://static.cyblogs.com/WX20200113-155940@2x.png

通过上面的脚本拉取最新的zipkin服务,此时的版本是:2.19.2

➜  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已经更换的更加的骚气~

➜  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

     &#95;&#95;&#95;&#95;&#95;&#95;&#95;&#95; &#95;&#95;&#95;&#95;  _  &#95;&#95;&#95;&#95;&#95; _   _
    |__  /_ _|  _ \| |/ /_ _| \ | |
      / / | || |_) | ' / | ||  \| |
     / /_ | ||  __/| . \ | || |\  |
    |&#95;&#95;&#95;&#95;|___|_|   |_|\_\___|_| \_|

:: version 2.19.2 :: commit 56d907b ::

当浏览器访问trace-a服务的时候,去后管页面查看就能看到服务列表以及日志情况。

图一:

http://static.cyblogs.com/WX20200113-145151@2x.png

图二:

http://static.cyblogs.com/WX20200113-145220@2x.png

方式二:RabbitMQ

安装RabbitMQ
➜  ~  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
######################################################################## 100.0%
==> /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