Springcloud2.0 sleuth zipkin rabbitmq

Springcloud2.0 sleuth zipkin rabbitmq

Sleuth介绍

Note: 直接摘抄网站https://blog.csdn.net/forezp/article/details/76795269 已经有搭建的详细步骤

微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元。由于服务单元数量众多,业务的复杂性,如果出现了错误和异常,很难去定位。主要体现在,一个请求可能需要调用很多个服务,而内部服务的调用复杂性,决定了问题难以定位。所以微服务架构中,必须实现分布式链路追踪,去跟进一个请求到底有哪些服务参与,参与的顺序又是怎样的,从而达到每个请求的步骤清晰可见,出了问题,很快定位。

举个例子,在微服务系统中,一个来自用户的请求,请求先达到前端A(如前端界面),然后通过远程调用,达到系统的中间件B、C(如负载均衡、网关等),最后达到后端服务D、E,后端经过一系列的业务逻辑计算最后将数据返回给用户。对于这样一个请求,经历了这么多个服务,怎么样将它的请求过程的数据记录下来呢?这就需要用到服务链路追踪。

Google开源的 Dapper链路追踪组件,并在2010年发表了论文《Dapper, a Large-Scale Distributed Systems Tracing Infrastructure》,这篇文章是业内实现链路追踪的标杆和理论基础,具有非常大的参考价值。 目前,链路追踪组件有Google的Dapper,Twitter 的Zipkin,以及阿里的Eagleeye (鹰眼)等,它们都是非常优秀的链路追踪开源组件。

Spring Cloud基本术语在Github已经有做了很多解释参考:spring-cloud-sleuth

Sleuth 与Zipkin结合

网上已经有很多Sleuth与Zipkin结合,无非就是使用@EnableZipkinServer创建ZipkinSever但到SpringCloud2.0后,已经明确不在提供这样的方式,因为Zipkin当时提供这个注解不是让每个用户自己去声明ZipkinSever,如果要创建ZipkinServer有如下两种方式:

  1. JAR方式:下载最新JAR直接部署,参考地址 https://github.com/openzipkin/zipkin
  2. Docker: 使用Docker部署,参考地址 https://github.com/openzipkin/docker-zipkin

以下是Zipkin的说明

https://github.com/openzipkin/zipkin/commit/3f299cda195af7b1685a11e262dcd6a554dec2a1

Especially lately, we have had a large number of people having problems
with unnecessarily custom servers. Some are due to not knowing Sleuth's
stream server is obviated by our rabbit support. Some are due to blogs
which unfortunately recommend starting Zipkin in the IDE via a custom
server.

Through discussion, we decided the easiest way to let users know custom
servers are unsupported is by deprecation. Deprecation shows up in the
IDE and will alert those doing blogs or otherwise that they are
suggesting discouraged practice. It also sends a clear signal to those
who need to make custom servers that while doing so is possible, it is
something the customizer needs to accept support reponsibility of.

以下是SpringCloud官网说明:

http://cloud.spring.io/spring-cloud-static/spring-cloud-sleuth/2.0.0.M9/single/spring-cloud-sleuth.html#_zipkin_stream_span_consumer

[Important]	Important
We recommend using Zipkins native support for message-based span sending. Starting from the Edgware release, the Zipkin Stream server is deprecated. In the Finchley release, it got removed.

See the Dalston Documentaion for how to create a Stream Zipkin server.

案例实战

项目实战前提是项目已经有Apigateway、RegisterSterver、Userservice三个应用服务和rabbitmq服务。Userservice为对应的具体服务。

采用sleuth+zipkin+rabbitmq进行链路监控

  • sleuth: 负责发送监控trace日志
  • zipkin-server: 从队列获取消息并且插入对应的存储介质,有Elasticsearch、Mysql 等,项目采用Elasticsearch,并且提供UI访问,端口为9411
  • rabbitmq: 队列存储消息
  • zipkin-dependencies: 采用Spark job统计分析服务相互依赖关系

大致组件流程:sleuth=>rabbitmq=>zipkin-server=>mysql=>zipkin-dependencies

部署zipkinserver

zipkinserver 有多种存储方式:mem, mysql, cassandra, elasticsearch。本例采用mysql 方式。

  1. zipkinserver jar包准备:在zipkin github下载最新的release jar包。
  2. zipkin-dependencies jar包准备: 在zipkin-dependencies下载最新的release jar包

执行启动脚本分别启动zipkinserver和zipkin-dependencies。启动参数可参见:https://github.com/openzipkin/zipkin/tree/master/zipkin-server

https://github.com/openzipkin/zipkin-dependencies

mysql脚本路径:https://github.com/openzipkin/zipkin/blob/master/zipkin-storage/mysql/src/main/resources/mysql.sql

STORAGE_TYPE=mysql MYSQL_USER=root MYSQL_DB=hctp_zipkin_dev MYSQL_PASS=root RABBIT_ADDRESSES=localhost MYSQL_HOST=192.168.11.237 java -jar zipkin-server-2.7.1-exec.jar

STORAGE_TYPE=mysql MYSQL_USER=root MYSQL_DB=hctp_zipkin_dev MYSQL_PASS=root MYSQL_HOST=192.168.11.237 java -jar zipkin-dependencies-1.11.3.jar

项目启动后将监听队列默认队列zipkin

 image

Sleuth 依赖

Sleuth只要依赖jar就很容易的与zipkin进行集成,由于我们项目采用rabbitmq所以要增加rabbitmq集成

  1. Apigateway、Userservice依赖
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-zipkin</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.amqp</groupId>
		<artifactId>spring-rabbit</artifactId>
	</dependency>

项目启动后将在rabbitmq创建sleuth exchange和zipkin queue

 imageimage

  1. bootstrap.yml配置
spring:
  zipkin:
    #内存方式配置,不需要
    #base-url: http://localhost:9411
    #日志发送队列类型
    sender:
      type: rabbit
    #发送的队列名称,默认zipkin,正常情况不要修改因为zipkin-server默认监控队列也是zipkin
    rabbitmq:
      queue: zipkin
  sleuth:
  #统计收集百分比默认0.1,正式环境可不配置
    sampler:
      percentage: 1.0

启动Apigateway、RegisterSterver、Userservice三个应用服务。

测试

  1. Postman发送请求 image
  2. 查看zipkin server http://localhost:9411/zipkin image

3、查看服务依赖 http://localhost:9411/zipkin/dependency/image

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值