Spring Cloud sleuth with zipkin over RabbitMQ教程
文章目录
Spring Cloud sleuth with zipkin over RabbitMQ demo
本项目是sleuth和zipkin在spring cloud环境中使用,其中sleuth和zipkin是通过RabbitMQ进行通信,同时zipkin的数据是存储在mysql中。
Spring Cloud的版本是目前最新的Greenwich.SR2版本,对应的Spring boot是2.1.8.RELEASE。
本教程要解决的问题:
- zipkin server的搭建(基于mysql和rabbitMQ)
- 客户端环境的依赖
- 如何调用
zipkin server的搭建(基于mysql和rabbitMQ)
最新的zipkin官网建议使用zipkin提供的官方包来启动zipkin server。 步骤如下:
- 下载最新的zipkin server jar包: - curl -sSL https://zipkin.io/quickstart.sh | bash -s 
- 配置环境变量,并启动zipkin server,见startServer.sh: 
#!/bin/bash
#rabbit mq config
export RABBIT_CONCURRENCY=1
export RABBIT_CONNECTION_TIMEOUT=60000
export RABBIT_QUEUE=zipkin
export RABBIT_ADDRESSES=127.0.0.1:5672
export RABBIT_PASSWORD=guest
export RABBIT_USER=guest
export RABBIT_VIRTUAL_HOST=zipkin
export RABBIT_USE_SSL=false
#mysql config
export STORAGE_TYPE=mysql
export MYSQL_DB=zipkin
export MYSQL_USER=root
export MYSQL_PASS=123456
export MYSQL_HOST=127.0.0.1
export MYSQL_TCP_PORT=3306
export MYSQL_MAX_CONNECTIONS=10
export MYSQL_USE_SSL=false
nohup java -jar zipkin.jar  &
echo $! > pid.txt
请将rabbit mq 和 mysql 的配置修改成你对应的环境变量。
- mysql数据库脚本:
这里我也列出来了:
--
-- Copyright 2015-2019 The OpenZipkin Authors
--
-- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
-- in compliance with the License. You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software distributed under the License
-- is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
-- or implied. See the License for the specific language governing permissions and limitations under
-- the License.
--
CREATE TABLE IF NOT EXISTS zipkin_spans (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL,
  `id` BIGINT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `remote_service_name` VARCHAR(255),
  `parent_id` BIGINT,
  `debug` BIT(1),
  `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
  `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
  PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';
CREATE TABLE IF NOT EXISTS zipkin_annotations (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
  `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
  `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
  `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
  `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
  `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
  `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
  `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';
CREATE TABLE IF NOT EXISTS zipkin_dependencies (
  `day` DATE NOT NULL,
  `parent` VARCHAR(255) NOT NULL,
  `child` VARCHAR(255) NOT NULL,
  `call_count` BIGINT,
  `error_count` BIGINT,
  PRIMARY KEY (`day`, `parent`, `child`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
在正式环境中,官方推荐的使用Elastricsearch做数据存储,因为zipkin收集的数据会比较多,使用mysql可能会有性能问题。后面有机会我们再讲怎么用Elastricsearch作数据存储。
- 运行 sh startServer.sh即可启动zipkin server.
客户端环境的依赖
如果想要在客户端使用sleuth+ rabbitMQ,需要如下配置:
<dependencyManagement>
      <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>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
</dependency>
本实例中我们使用了eureka, 其实它不是必须的。大家在实际使用中可以自己取舍。
我们看一下zipkin客户端的配置文件:
spring:
  application:
    name: service2
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    virtual-host: zipkin
  zipkin:
    sender:
      type: rabbit
    rabbitmq:
      queue: zipkin
  sleuth:
    sampler:
      probability: 1.0
spring.application.name 很好理解,就是应用程序的名字,会被默认作为zipkin服务的名字。
我们使用rabbitMQ ,所以需要spring.rabbitmq的配置信息。
spring.zipkin.sender.type=rabbit 表示我们需要使用rabbit MQ来收集信息。当然你也可以设置成为web或者kafka。
这里spring.zipkin.rabbitmq.queue=zipkin表示使用MQ时候的queue名字,默认是zipkin。
spring.sleuth.sampler.probability=1.0 这个是采样信息,1.0表示是100%采集。如果要在线上使用,可以自定义这个百分比。
如何调用
最后我们看下如何调用。
在service2中,我们定义了如下的方法:
@RestController
@RequestMapping("/serviceTwo")
public class ServiceTwoController {
    @GetMapping("callServiceTwo")
    public String callServiceOne(){
        log.info("service two is called!");
        return "service two is called!";
    }
}
我们在service1中用restTemplet来调用它:
@RestController
@RequestMapping("/serviceOne")
public class ServiceOneController {
    @GetMapping("callServiceOne")
    public String callServiceOne(){
        log.info("service one is called!");
        restTemplate().getForObject("http://localhost:9000/serviceTwo/callServiceTwo",String.class);
        return "service one and two are called!";
    }
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
这样,我们用get 去请求http://loalhost/serviceOne/callServiceOne 就会将调用信息发送到MQ,并被zipkin Server 处理。 我们就可以在zipkin web页面看到调用信息啦 。
have fun !
更多教程请参考 flydean的博客
Spring Cloud sleuth with zipkin over RabbitMQ教程的更多相关文章
- 跟我学SpringCloud | 第十一篇:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
		SpringCloud系列教程 | 第十一篇:使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪 Springboot: 2.1.6.RELEASE SpringCloud: ... 
- Distributed traceability with Spring Cloud: Sleuth and Zipkin
		I. Sleuth 0. Concept Trace A set of spans that form a call tree structure, forms the trace of the re ... 
- spring cloud 入门系列八:使用spring cloud sleuth整合zipkin进行服务链路追踪
		好久没有写博客了,主要是最近有些忙,今天忙里偷闲来一篇. =======我是华丽的分割线========== 微服务架构是一种分布式架构,微服务系统按照业务划分服务单元,一个微服务往往会有很多个服务单 ... 
- Spring Cloud Sleuth 和 Zipkin 进行分布式跟踪使用指南
		分布式跟踪允许您跟踪分布式系统中的请求.本文通过了解如何使用 Spring Cloud Sleuth 和 Zipkin 来做到这一点. 对于一个做所有事情的大型应用程序(我们通常将其称为单体应用程序) ... 
- springcloud --- spring cloud sleuth和zipkin日志管理(spring boot 2.18)
		前言 在spring cloud分布式架构中,系统被拆分成了许多个服务单元,业务复杂性提高.如果出现了异常情况,很难定位到错误位置,所以需要实现分布式链路追踪,跟进一个请求有哪些服务参与,参与的顺序如 ... 
- springcloud(十二):使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
		随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请求可能最终需要调用很多次后端服务才能完成,当整个请求变慢或不可用时,我们是无法得知该请求是由某个或某些后端服务引起的,这时就需要解决如何快读定位 ... 
- 【spring cloud】spring cloud Sleuth 和Zipkin 进行分布式链路跟踪
		spring cloud 分布式微服务架构下,所有请求都去找网关,对外返回也是统一的结果,或者成功,或者失败. 但是如果失败,那分布式系统之间的服务调用可能非常复杂,那么要定位到发生错误的具体位置,就 ... 
- 使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
		原文:http://www.cnblogs.com/ityouknow/p/8403388.html 随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请求可能最终需要调用很多次后端服务才能完成, ... 
- spring cloud深入学习(十三)-----使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
		随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请求可能最终需要调用很多次后端服务才能完成,当整个请求变慢或不可用时,我们是无法得知该请求是由某个或某些后端服务引起的,这时就需要解决如何快读定位 ... 
随机推荐
- minIO分布式集群搭建+nginx负载均衡
			暂时关闭防火墙 systemctl stop firewalld 查看防火墙状态 systemctl status firewalld 赋予最高权限 chmod +x minio !/bin/bash ... 
- 【Java技术系列】爱情36技之Bug大战
			1. 鲁迅先生说:程序员,天不怕地不怕,就怕小虫儿爬呀爬,爬呀爬. 随着时间的推移,鲁迅先生又说:真正勇猛的程序员,敢于让虫子面对惨淡的虫生. 虫子在程序员心中是啥东西?虫子的学名为 Bug,是多少入 ... 
- 在ES5实现ES6中的Object.is方法
			ES6中对象的扩展里面添加了一个Object.is方法,用于比较两个值是否严格相等.内部计算与 === 行为基本一致.那么我们怎么在不支持这个方法的ES5中实现呢? 首先我们需要搞清楚两点,1:Obj ... 
- C++模板心得
			C++模板心得 我开始学模板的时候一脸懵逼,真的看不懂模板是怎么作用的.因为大多数人的代码把模板声明和函数.类的声明分行写,让我以为模板的作用是全局的,实际上应该像如下理解. 函数模板 templat ... 
- 路由与交换,cisco路由器配置,基础知识点(二)
			1.进退用户/特权/全局模式 (1)从用户模式进入特权模式 enable (2)从特权模式进入全局配置模式 configure terminal (3)从其他模式回到特权模式 end (4)从特权模式 ... 
- javascript入门 之 ztree(四 自定义Icon)
			<!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - Standard Data </TITLE& ... 
- 【第一章】黎姿的python学习笔记
- 【python实现卷积神经网络】卷积层Conv2D实现(带stride、padding)
			关于卷积操作是如何进行的就不必多说了,结合代码一步一步来看卷积层是怎么实现的. 代码来源:https://github.com/eriklindernoren/ML-From-Scratch 先看一下 ... 
- 两个div在同一行显示
			栅格系统需要引用bootstrap插件 <script src="~/Scripts/BootStrap/bootstrap.js"> </script> ... 
- stand up meeting 11/19/2015
			队员 今日工作 工作耗时/h 明日计划 计划耗时/h 冯晓云 利用昨天编写的调用必应词典API的DLL,完成了UWP版本查词APP的试水,证实了DLL可调和在线查词的可行性:和其他部分的同学就接口数据 ... 
