欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

sleuth+zipkin+kafka+elasticsearch搭建分布式链路追踪系统(一)

程序员文章站 2024-01-27 22:34:34
...

环境准备

kafka的安装教程,https://blog.csdn.net/u012394095/article/details/81385414

elasticsearch安装 非常简单,从网上下载安装包,在本机上安装就好了。

搭建zipkin-server

zipkin-parent的pom文件

    <properties>
        <zipkin.stream.version>1.2.2.RELEASE</zipkin.stream.version>
        <sleuth.version>1.2.6.RELEASE</sleuth.version>
        <bind.kafka.version>1.2.1.RELEASE</bind.kafka.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

zipkin-server的pom文件

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
            <version>${zipkin.stream.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
            <version>${bind.kafka.version}</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-ui</artifactId>
        </dependency>

        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
            <version>2.4.2</version>
        </dependency>

yml文件

spring:
  application:
    name: sleuthServer
  zipkin:
    enabled: true
  sleuth:
    sampler:
      percentage: 1.0
  cloud:
      stream:
        kafka:
          binder:
            brokers: localhost:9092
            zkNodes: localhost:2181
  #ES配置
zipkin:
  storage:
    type: elasticsearch
    elasticsearch:
      hosts: http://10.208.204.46:9200
      cluster: elasticsearch
      index: zipkin
      index-shards: 1
      index-replicas: 1
server:
  port: 9411
eureka:
  client:
    serviceUrl:
      defaultZone: 'http://10.208.204.46:8081/eureka/'
  instance:
      preferIpAddress: true
      instanceId: ${spring.cloud.client.ipAddress}:${server.port}

启动类修改


@SpringBootApplication
@EnableZipkinStreamServer
@EnableEurekaClient
public class SleuthServerApplication {


    public static void main(String[] args) {
        SpringApplication.run(SleuthServerApplication.class, args);
    }
}

添加@EnableZipkinStreamServer注解,表示开启zipkin服务

搭建zipkin-consumer

修改pom文件

<!--引入zipkin绑定-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
  <version>${zipkin.stream.version}</version>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-sleuth</artifactId>
  <version>${sleuth.version}</version>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-stream-binder-kafka</artifactId>
  <version>${bind.kafka.version}</version>
</dependency>

修改yml文件

spring:
  sleuth:
    sampler:
      percentage: 1  # 采样百分比,我这里配置了百分之百,
  cloud:
      stream:
        kafka:
          binder:
            brokers: localhost:9092   # kafka的节点信息 
            zkNodes: localhost:2181   # zookeeper节点信息

搭建zipkin-provider

修改的方式和搭建zipkin-consumer一模一样,就是为了接入链路追踪,修改pom文件,修改配置文件即可

测试

通过consumer调用provider的接口,调用之后,通过访问 http://localhost:9411/zipkin/ 可以看到如下页面。

sleuth+zipkin+kafka+elasticsearch搭建分布式链路追踪系统(一)

全局调用链
sleuth+zipkin+kafka+elasticsearch搭建分布式链路追踪系统(一)

注意点

在consumer或者provider启动的过程中,可能会报错

sleuth+zipkin+kafka+elasticsearch搭建分布式链路追踪系统(一)
这是由于zipkin对feign的切面使用的表达式版本比较高,aspectjweaver这个jar包需要是1.8.10的、引入如下jar即可解决

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.10</version>
</dependency>

sleuth+zipkin+kafka+elasticsearch搭建分布式链路追踪系统(一)