Spring cloud(Finchley)微服务框架,sleuth整合zipkin链路追踪失效的问题

一、首先说问题:

  • 1、springCloud在使用链路追踪组件sleuth整合zipkin的过程中链路追踪信息切都是正常;
  • 2、微服务太多需要使用组件Config对每个微服务的的配置文件进行统一管理;
  • 3、config对微服务进行管理,但是如果某一个配置发生改变,如果每个微服务都要重启才能获取最新的配置文件就不合理,那么springCloud提供了spring cloud Bus组件来刷新配置,使用消息中间件(官方推荐RabbitMQ)不需要重启微服务就可以获取最新配置。
  • 4、当spring cloud Bus 配置完成之后zipkin完全收集不到链路追踪信息了。。。

二、寻找解决方案

最开始遇到这个问题时,认为是自己配置在哪里有问题,反复确认没有问题。然后开始狂论坛,问大牛。得到的答案是springcloud不成熟,可能存在bug,既然有bug(我也不知道该怎么解决),但是如果链路追踪不能使用,对整个微服务的调用依赖,每个微服务的调用请求耗时就没有办法定位,对后期的维护是不利的。只能放弃分布式配置,但是也不好,如果修改了一个配置文件,每个微服务都要重启,对于后期的维护也是不利的。只能继续寻找解决方案。

三、问题分析

  • 1、出现这个问题,首先想到的是要去官方文档上寻找答案;丢一个springcloud文档:springcloud Finchley SR2
  • 2、使用springcloud Bus需要使用rabbitMQ,但是zipkin收集信息是http(文档)
If you want both Sleuth and Zipkin, add the spring-cloud-starter-zipkin dependency.

The following example shows how to do so for Maven:

Maven. 

<dependencyManagement> 1
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-dependencies</artifactId>
              <version>${release.train.version}</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
</dependencyManagement>

<dependency> 2
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
1、We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2、Add the dependency to spring-cloud-starter-zipkin.

同时zipkin也支持使用RabbitMQ来收集链路信息,猜想是不是在配置中使用了RabbitMQ收集,而HTTP的方式失效了,那么就有两个解决方案:其中一就是只是用HTTP的方式,不是用RabbitMQ;二:就是将zipkin收集链路信息的方式修改为RabbitMQ。方式一简单有效,但是没有找到相应的配置,那么就使用方案二,在官方文档中也有描述将zipkin的获取方式修改为rabbitMq或者kafka:

If you want to use RabbitMQ or Kafka instead of HTTP, add the spring-rabbit or spring-kafka dependency. The default destination name is zipkin.

If using Kafka, you must set the property spring.zipkin.sender.type property accordingly:

spring.zipkin.sender.type: kafka
[Caution]	Caution
spring-cloud-sleuth-stream is deprecated and incompatible with these destinations.

If you want Sleuth over RabbitMQ, add the spring-cloud-starter-zipkin and spring-rabbit dependencies.

The following example shows how to do so for Gradle:

Maven. 

<dependencyManagement> 1
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-dependencies</artifactId>
              <version>${release.train.version}</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
</dependencyManagement>

<dependency> 2
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency> 3
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
</dependency>
1、We recommend that you add the dependency management through the Spring BOM so that you need not manage versions yourself.

2、Add the dependency to spring-cloud-starter-zipkin. That way, all nested dependencies get downloaded.

3、To automatically configure RabbitMQ, add the spring-rabbit dependency.

通过官方文档的说明按照上述的三个步骤就能实现,但是按照上述的步骤依旧是没有追踪到链路信息。

上述的配置说明微服务的客户端应该是没有问题的,那么问题可能出现在zipkin服务器上。

四、zipkin服务器的问题

从spring boot 2.0开始官方就官方不再支持使用自建Zipkin Server的方式进行服务链路追踪,而是直接提供了编译好的 jar 包来给我们使用。
但是从1.0时代的配置中应该是可以看出问题,在1.0的Zipkin Service中需要加入sleuth和rabbit的依赖,同时需要配置rabbitMQ消息的地址,但是使用jar包直接启动,并没有指定rabbit的地址。
查询spring-cloud-sleuth在github上的源码,在源码中会有一些人遇到一些问题,会不会有同样的问题,解决方案。其中有一个问题:

Question: How to configure Sleuth with Zipkin over RabbitMQ

有一个回答:

I also have the same problem and the workaround provided in Gitter has helped me to solve it until the permanent solution is in place. For the benefit of others here is the workaround.

Explicitly configure spring.rabbitmq.addresses property to point to the credentials of the bounded RabbitMQ service. For example, if the bounded service is called rabbitmq the following configuration will work:

spring.rabbitmq.addresses=${vcap.services.rabbitmq.credentials.uri}

也就是说在启动zipkin的时候指定一下rabbitMq的地址。问题

五、解决方案

1、在需要收集链路追踪的客户端添加依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
        </dependency>

2、下载zipk的jar包这里使用了zipkin-server-2.11.1-exec
在启动zipkin的之后设置参数

java -jar zipkin.jar --zipkin.collector.rabbitmq.addresses=localhost

或者使用docker
添加参数

docker run -d -e zipkin.collector.rabbitmq.addresses=localhost -p 9411:9411 openzipkin/zipkin 

在docker启动后还是有问题,最后发现是localhost的问题,将localhost修改成rabbit对应的服务器IP地址就好了。
启动之后在RabbitMq中会出现一个zipkin的队列。

另一种方式,使用http就比较简单,在项目中加入

  zipkin:
    base-url: http://ip:9411
    # 使用http的方式收集链路追踪信息,默认是使用rabbitMQ,这样在使用了spring cloud Bus之后链路追踪就不会失效了
    sender:
      type: web

但是推荐使用rabbit。
最后在zipkin的页面上就可以看到链路追踪的信息了

猜你喜欢

转载自blog.csdn.net/chenfengdejuanlian/article/details/86506715