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

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

应用程序用户向Zipkin报告时序数据,Zipkin UI还提供了一个依赖关系图,显示了每个应用程序通过的跟踪请求数。如果要解决延迟问题或错误,可以根据应用程序,跟中长度,注释或时间戳对所有跟踪进行筛选或排序。选择跟踪后,您可以看到每个跨度所需的总跟踪时间百分比,从而可以识别应用程序呈现的问题。

官网:https://zipkin.io/

快速开始

以下的过程中,我们将介绍构建和启动Zipkin实例,以便在本地检查Zipkin。根据官网显示,有三种选择:使用Java、Docker或从源代码运行。

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

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

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

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

Zipkin服务端

1. 引入依赖

<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>
${zipkin}:<zipkin>2.12.2</zipkin>

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

3. 重写 WebMvcTagsProvider

解决问题:

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

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="[email protected]">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

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="[email protected]">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. 启动服务

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="[email protected]">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://localhost:9411/ # 指定了Zipkin服务器的地址

Zipkin效果展示

 

猜你喜欢

转载自www.cnblogs.com/xushuyi/p/10443769.html