【Spring Cloud 基础设施搭建系列】Spring Cloud Demo项目 使用Spring Cloud Sleuth+Zipkin实现微服务的链路追踪


我们知道,微服务之间通过网络进行通信。如果能够跟踪每个请求,了解请求经过哪些微服务(从而了解信息是如何在服务之间流动)、请求耗费时间、网络延迟、业务逻辑耗费时间等指标,那么就能更好地分析系统瓶颈、解决系统问题。因此,微服务跟踪很有必要。

spring cloud sleuth提供了服务链路追踪,并兼容了zipkin,Zipkin是一个链路跟踪工具,可以用来监控微服务集群中调用链路的通畅情况。

我这里就不花太多时间去介绍Sleuth和Zipkin的一些概念,有兴趣的可以拉到文章最后,看看参考中的一些链接,也许可以帮助到你。好吧,我们直接入正题,开始讲环境和配置的搭建吧。

搭建链路追踪服务端(Zipkin服务端)

在 Spring Boot 2.0 版本之后,官方已不推荐自己搭建定制了,而是直接提供了编译好的 jar 包。详情可以查看官网:https://zipkin.io/pages/quickstart.html

但是我这里还是讲一下自己怎么搭建zipkin服务端,还有遇到的一些坑。

创建Sleuth服务

  1. 创建Zipkin服务端,创建一个module,取名为cloud-zipkin。
  2. 添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.cc.cloud</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-zipkin</artifactId>

    <dependencies>
        <dependency>
            <!-- zipkin服务端 -->
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-server</artifactId>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-ui</artifactId>
            <version>2.11.1</version>
        </dependency>
    </dependencies>

</project>

我们启动我们的cloud-zipkiin服务,结果发现在控制台报错了,报错如下:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/kevincai/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.11.1/log4j-slf4j-impl-2.11.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/kevincai/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Exception in thread "main" java.lang.StackOverflowError
	at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:110)
	at org.apache.logging.log4j.util.StackLocator.getCallerClass(StackLocator.java:123)
	at org.apache.logging.log4j.util.StackLocatorUtil.getCallerClass(StackLocatorUtil.java:55)
	at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:42)
	at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
	at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
	at org.apache.logging.slf4j.SLF4JLoggerContext.getLogger(SLF4JLoggerContext.java:39)
	at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:37)
	at org.apache.logging.slf4j.Log4jLoggerFactory.newLogger(Log4jLoggerFactory.java:29)
	at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:52)
	at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:29)
	at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)

解决方法如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.cc.cloud</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-zipkin</artifactId>

    <dependencies>
        <dependency>
            <!-- zipkin服务端 -->
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-server</artifactId>
            <!--排除-->
            <!-- 解决报错maven依赖jar包冲突 Class path contains multiple SLF4J bindings.-->
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-slf4j-impl</artifactId>
                </exclusion>
            </exclusions>
            <version>2.11.1</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-ui</artifactId>
            <version>2.11.1</version>
        </dependency>
    </dependencies>

</project>

参考:【maven】排除maven中jar包依赖的解决过程 例子:spring cloud启动zipkin,报错maven依赖jar包冲突 Class path contains multiple SLF4J bindings.

然后我们重新启动服务。

在这里插入图片描述

接着访问:http://localhost:9411/zipkin/

在这里插入图片描述

客户端配置链路追踪

  1. 现在我们往cloud-service-order和cloud-service-member中添加zipkin的依赖。
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
  1. 现在我们添加如下配置:
spring:
  zipkin:
    enabled: true
    base-url: http://localhost:9411
  sleuth:
    sampler:
      #收集追踪信息的比率,如果是0.1则表示只记录10%的追踪数据,如果要全部追踪,设置为1(实际场景不推荐,因为会造成不小的性能消耗)
      probability: 1

然后我们启动cloud-eureka,cloud-zuul,cloud-service-order,cloud-service-member服务。

这个时候我们分别访问。http://localhost:8769/api/cloud-order/order/members 和 http://localhost:8769/api/cloud-member/member/orders

然后这时我们在看zipkin服务端访问页面依赖分析,可以看到cloud-service-order和cloud-service-member互相依赖调用。(网上说要互相调用才可以看到效果,实际测试发现,单向调用也是可以看到依赖分析的)

下面是互相调用的图:

在这里插入图片描述

下面是member调用order的图:

在这里插入图片描述

zipkin上面还有很多其他的功能,这里就不一一介绍了,有兴趣的可以去官网看看,或者参考文章最后的参考链接。

在这里插入图片描述

还有一个坑,我之前配置的时候遇到过,但是这次重新配置好像没发现有问题?可能是之前的配置有些问题?不过可以在这里说一下问题和解决方法。

问题就是配置完上面的设置之后,发现zipkin无法收到服务监控信息,也就是看不到上面界面的这些依赖分析图。

解决方法就是加入配置zipkin.sender.type=web

zipkin:
    enabled: true
    base-url: http://localhost:9411
    sender:
      type: web  #zipkin终接收不了数据,尝试添加zipkin.sender.type=WEB

参考:解决spring cloud Finchley.SR2版本zipkin无法收到服务监控信息

总结:不推荐个人手动搭建zipkin服务端,还是按照官网,去官网下载包,然后启动zipkin服务。免得个人搭建遇到太多的坑。

参考

SpringCloud与Docker微服务架构实战-完整版.pdf

史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)

解决spring cloud Finchley.SR2版本zipkin无法收到服务监控信息

zipkin链路追踪详解

微服务架构搭建:Consul+sleuth+zipkin+Feign/Ribbon+SpringConfig+Zuul+Hystrix Dash-Board-Turbine

ZIPKIN的搭建使用过程

【SpringCloud Greenwich版本】第九章链路追踪(Sleuth)

spring cloud zipkin ( Sleuth )服务链路追踪

学习微服务的服务链路追踪——Spring Cloud Sleuth+zipkin

Spring Cloud 中整合Zipkin进行服务跟踪

Spring Cloud Sleuth和zipkin微服务跟踪

Spring Cloud 中整合Zipkin进行服务跟踪

zipkin

【maven】排除maven中jar包依赖的解决过程 例子:spring cloud启动zipkin,报错maven依赖jar包冲突 Class path contains multiple SLF4J bindings.

Spring Cloud 应用篇 之 Spring Cloud Sleuth + Zipkin(一)链路监控

Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】

源代码

https://gitee.com/cckevincyh/spring-cloud-demo/tree/sleuth-zipkin

发布了647 篇原创文章 · 获赞 816 · 访问量 98万+

猜你喜欢

转载自blog.csdn.net/cckevincyh/article/details/102017835