当前位置: 移动技术网 > IT编程>开发语言>Java > Springboot 2.0.x 引入链路跟踪Sleuth及Zipkin

Springboot 2.0.x 引入链路跟踪Sleuth及Zipkin

2019年02月28日  | 移动技术网IT编程  | 我要评论

zipkin是一种分布式跟踪系统,它有助于收集解决微服务架构中得延迟问题所需的时序数据,它管理这些数据的收集和查找。

1. 架构概述

跟踪器存在于您的应用程序中,并记录有关发生的操作的时间和元数据。他们经常使用库,因此它们的使用对用户是透明的。例如,已检测的web服务器会在收到请求时以及何时发送响应时进行记录。收集的跟踪数据称为span。

编写仪器是为了安全生产并且开销很小。出于这个原因,它们只在带内传播id,以告诉接收器正在进行跟踪。zipkin带外报告已完成的跨度,类似于应用程序异步报告度量标准的方式。

例如,在跟踪操作并且需要发出传出的http请求时,会添加一些标头来传播id。标头不用于发送操作名称等详细信息。

将数据发送到zipkin的检测应用程序中的组件称为reporter。记者通过几种传输之一将跟踪数据发送到zipkin收集器,这些收集器将跟踪数据保存到存储中。稍后,api会查询存储以向ui提供数据。

描述此流程,如下图表:

 

zipkin作为一个收藏家,一旦跟踪数据到达zipkin收集器守护程序,它就会被zipkin收集器验证,存储和索引查找。

zipkin查询服务,一旦数据被存储和索引,我们需要一种方法来提取它。查询守护程序提供了一个简单的json api,用户查找和检索跟踪。

参考官网:https://zipkin.io/

2. 快速开始

本文,我们将介绍构建和启动zipkin实例,以便在本地检查zipkin。根据官网显示,有三种选择:使用java、docker或从源代码运行。

如果您熟悉docker,这是首选的方法。如果您不熟悉docker,请尝试通过java或源代码运行。

zipkin分为两端,一个是zipkin服务端,一个是zipkin客户端(微服务应用)。客户端会配置服务端的url地址,一旦服务间调用的时候,会被配置在微服务里面的sleuth的监听器监听,并生成相应的trace和span信息发送给服务端。

发送的方式主要有两种,一种是http报文的方式,还有一种是消息总线的方式如:rabbitmq

我们这边采用的是http报文的方式。

zipkin服务端构建

1. 引入依赖

解析:主要引入两个jar:zipkin-server、zipkin-autoconfigure-ui,其中对应的版本为:2.12.2。

zipkin-server 本身包含了 log4j,如果系统已经引入log4j包,为了避免冲突,需要在此处将本jar包去除。

<dependencies>
        <!-- zipkin server -->
        <dependency>
            <groupid>io.zipkin.java</groupid>
            <artifactid>zipkin-server</artifactid>
            <exclusions>
                <exclusion>
                    <groupid>org.springframework.boot</groupid>
                    <artifactid>spring-boot-starter-log4j2</artifactid>
                </exclusion>
            </exclusions>
            <version>${zipkin}</version>
        </dependency>
        <!-- zipkin ui -->
        <dependency>
            <groupid>io.zipkin.java</groupid>
            <artifactid>zipkin-autoconfigure-ui</artifactid>
            <version>${zipkin}</version>
        </dependency>
    </dependencies>

2. 配置文件

spring:
  application:
    name: zipkin-server
  security:
    user:
      name: zhangsan
      password: 111111
---
# 服务端口
server:
  port: 9411
---
eureka:
  instance:
    # 解决健康节点权限问题 将用户名及密码放入eureka的元数据中
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}
  client:
    service-url:
      defaultzone: http://${spring.security.user.name}:${spring.security.user.password}@peer1:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@peer2:8762/eureka/
---
# 客户端负载均衡器
ribbon:
  connecttimeout: 3000 # 客户端连接超时
  readtimeout: 60000 # 客户端读取超时
  maxautoretries: 1 # 当前环境最大重试次数
  maxautoretriesnextserver: 2 # 其他环境最大重试次数
  eureka:
      enabled: true
---
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: false
---
# 关闭自动配置启用所有请求得检测
management:
  metrics:
    web:
      server:
        auto-time-requests: true

注:如果配置文件中没有添加“management.metrics.web.server.auto-time-requests=false”,默认为 开启自动检测请求

在启动zipkin-server过程中,会报以下问题:

java.lang.illegalargumentexception: prometheus requires that all meters with the same name have the same set of tag keys. there is already an existing meter containing tag keys [method, status, uri]. the meter you are attempting to register has keys [exception, method, status, uri].

解决方案:
a、配置文件中增加:management.metrics.web.server.auto-time-requests=false设置为false,默认为true

b、重写webmvctagsprovider,目的是去掉 webmvctags.exception(exception)

package com.sinosoft.config;

import io.micrometer.core.instrument.tag;
import io.micrometer.core.instrument.tags;
import org.springframework.boot.actuate.metrics.web.servlet.webmvctags;
import org.springframework.boot.actuate.metrics.web.servlet.webmvctagsprovider;

import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

/**
 * @classname: mytagsprovider
 * @description: todo
 * @author: created by xxx <a href="xxx@163.com">contact author</a>
 * @date: 2019/2/27 13:49
 * @version: v1.0
 */
public class mytagsprovider implements webmvctagsprovider {

    /**
     * 去掉webmvctags.exception(exception)
     *
     * @param request   请求
     * @param response  响应
     * @param handler   处理
     * @param exception 异常
     * @return
     */
    @override
    public iterable<tag> gettags(httpservletrequest request, httpservletresponse response, object handler, throwable exception) {
        return tags.of(webmvctags.method(request), webmvctags.uri(request, response), webmvctags.status(response));
    }

    @override
    public iterable<tag> getlongrequesttags(httpservletrequest request, object handler) {
        return tags.of(webmvctags.method(request), webmvctags.uri(request, null));
    }
}

然后将重写后的mytagsprovider进行bean注入

package com.sinosoft.config;

import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

/**
 * @classname: tagsprovidebean
 * @description: todo
 * @author: created by xxx <a href="xxx@163.com">contact author</a>
 * @date: 2019/2/27 13:54
 * @version: v1.0
 */
@configuration
public class tagsprovidebean {

    /**
     * 将mytagsprovider注入
     *
     * @return
     */
    @bean
    public mytagsprovider mytagsprovider() {
        return new mytagsprovider();
    }

}

4. 启动zipkin-server服务

package com.sinosoft;

import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.discovery.enablediscoveryclient;
import zipkin2.server.internal.enablezipkinserver;

/**
 * @classname: zipkinapplication
 * @description: 调用链服务启动入口
 * @author: created by xxx <a href="xxx@163.com">contact author</a>
 * @date: 2019/2/27 11:43
 * @version: v1.0
 */
@springbootapplication
@enablediscoveryclient
@enablezipkinserver
public class zipkinapplication {
    public static void main(string[] args) {
        springapplication.run(zipkinapplication.class, args);
    }
}

zipkin客户端构建

1. 引入依赖

<!-- 调用链 zipkin -->
<dependency>
    <groupid>org.springframework.cloud</groupid>
    <artifactid>spring-cloud-starter-zipkin</artifactid>
</dependency>

注:spring-cloud-starter-zipkin 已经包含了 spring-cloud-starter-sleuth

2. 配置文件

---
# 调用链 sleuth + zipkin
spring:
  sleuth:
    web:
      client:
        enabled: true
    sampler:
      probability: 1.0 # 采用比例,默认 0.1 全部采样 1.0
  zipkin:
    base-url: http://www.lhsxpumps.com/_localhost:9411/ # 指定了zipkin服务器的地址

zipkin效果展示

zipkin服务端及客户端启动之后,访问:http://localhost:9411/zipkin/

默认是查找所有的服务对应的链路,如果需要查询指定服务的链路跟踪情况,可以选择对应的服务名、span名称等其他条件,点击查询即可显示本服务下的链路跟踪结果。

链路跟踪明细:

 

 

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网