SpringCloud系列十二:SpringCloudSleuth(SpringCloudSleuth 简介、SpringCloudSleuth 基本配置、数据采集)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅。
1、概念:SpringCloudSleuth
2、具体内容
Sleuth 是一种提供的跟踪服务,也就是说利用 sleuth 技术可以实现完整的微服务的访问路径的跟踪操作。
2.1、SpringCloudSleuth 简介
微服务可以将整个的系统拆分为无数个子系统,于是这样一来就有可能出现几种可怕的场景:
· 代码的调试:
|- 你的系统有可能变慢了,于是这个时候就需要去追踪每一个微服务的执行的速度;
|- 如果现在你的微服务采用了串联的方式进行了互相调用,那么如何确认某一个微服务出现了问题呢?
· 微服务混合调用:
|- 现在微服务变为了环形调用,那么这些关系该如何描述出来?
在创建微服务的时候一定要有一些合适的开发契约,所有的开发者以及服务的调用者要按照统一的方式进行程序的调用处理, 这样才可以成为一个优秀的微服务设计。
所以在 SpringCloud 之中提供的 Sleuth 技术就可以实现微服务的调用跟踪,也就是说它可以自动的形成一个调用连接线,通过这个连接线使得开发者可以轻松的找到所有微服务间关系,同时也可以获取微服务所耗费的时间, 这样就可以进行微服务调用状态的监控以及相应的数据分析。
Span 里面包含有如下内容:
· cs-Client Sent:客户端发出一个请求,描述的是一个 Span 开始;
· sr-Server Received:服务端接收请求,利用 sr-cs 就属于发送的网络延迟;
· ss-Server Sent:服务端发送请求(回应处理),ss-sr 就属于服务端的消耗时间;
· cr-Client Received:客户端接收到服务端数据,cr-ss 就表示回复所需要的时间。
2.2、SpringCloudSleuth 基本配置
SpringCloudSleuth 使用的核心组件在于 Twitter 推出的 zipkin 监控组件,所以本次的配置的模块一定要包含 zipkin 相关配置依赖,本次实现一个基础的调用逻辑:consumer-zuul-dept。
1、 【microcloud-sleuth-8601】通过“microcloud-provider-company-8101”项目复制得来;
2、 【microcloud-sleuth-8601】修改 pom.xml 配置文件:
· 由于 sleuth 的应用比较复杂,而且也牵扯到埋点的数据分析,本次不使用安全处理模块:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
3、 【microcloud-sleuth-8601】修改 application.yml 配置文件:
server:
port: 8601
spring:
application:
name: microcloud-zipkin-server
4、 【microcloud-sleuth-8601】修改程序启动类:
package cn.study.microcloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import zipkin.server.EnableZipkinServer;
@SpringBootApplication
@EnableCircuitBreaker
@EnableZipkinServer
public class Zipkin_8601_StartSpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(Zipkin_8601_StartSpringCloudApplication.class, args);
}
}
5、 修改 hosts 配置文件,追加一个新的主机名称映射:
127.0.0.1 zipkin.com
6、 【microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001】修改 pom.xml 配置文件,追加 zipkin 相关依赖程序包:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
7、 【microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001】修改 application.yml 配置文件:
spring:
zipkin:
base-url: http://zipkin.com:8601 # 所有的数据提交到此服务之中
sleuth:
sampler:
percentage: 1.0 # 定义抽样比率,默认为0.1
application:
name: microcloud-consumer-feign
一定要有每一个微服务的名字,这样会比较好观察程序的执行轨迹。
8、 依次启动所有的服务:microcloud-sleuth-8601、microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001;
输入访问地址:http://zipkin.com:8601;就可以看到各个微服务之间的调用关系了
2.3、数据采集
现在已经成功的实现了一个 SpringCloudSleuth 基础操作,但是需要考虑一个实际的问题,现在所有的统计的汇总操作都是记录在内存之中的,也就是说如果你现在已经关闭了 zipkin 服务端,那么这些统计信息就将消失,很明显这样的做法并不符合实际要求,数据应该被记录下来,而且有可能你很多的微服务要发送大量的数据信息进入,为了解决这种高并发的问题,可以结合消息组件(Stream)进行缓存处理,而且本次为了方便可以将统计的结果保存在数据库之中(mysql)。
1、 需要创建数据库脚本,脚本是从官网拷贝下来的直接复制使用即可:
DROP DATABASE IF EXISTS zipkin ;
CREATE DATABASE zipkin CHARACTER SET UTF8 ;
USE zipkin ;
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,
`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'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`);
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`);
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`);
ALTER TABLE zipkin_spans ADD INDEX(`name`);
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`);
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`);
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) ;
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`);
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`);
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`);
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`);
CREATE TABLE IF NOT EXISTS zipkin_dependencies (
`day` DATE NOT NULL,
`parent` VARCHAR(255) NOT NULL,
`child` VARCHAR(255) NOT NULL,
`call_count` BIGINT
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);
2、 【microcloud-sleuth-8601】修改 pom.xml 配置文件,追加相关的依赖程序包:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency> -->
3、 【microcloud-sleuth-8601】修改 application.yml 配置文件:
server:
port: 8601
spring:
rabbitmq:
host: rabbitmq-server
port: 5672
username: studyjava
password: hello
virtual-host: /
datasource:
driver-class-name: org.gjt.mm.mysql.Driver # 配置MySQL的驱动程序类
url: jdbc:mysql://localhost:3306/zipkin # 数据库连接地址
username: root # 数据库用户名
password: mysqladmin # 数据库连接密码
initialize: true
application:
name: microcloud-zipkin-server
zipkin:
storage: # 设置zipkin收集的信息通过mysql进行存储
type: mysql
4、 【microcloud-sleuth-8601】可以打开安全配置项:
<dependency>
<groupId>cn.study</groupId>
<artifactId>microcloud-security</artifactId>
</dependency>
5、 【microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001】修改 pom.xml 配置文件:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency> -->
6、 【microcloud-consumer-feign、microcloud-zuul-gateway-9501、microcloud-provider-dept-8001】修改 application.yml 配置文件:
spring:
rabbitmq:
host: rabbitmq-server
port: 5672
username: studyjava
password: hello
virtual-host: /
同时删除掉已有的 zipkin.base-url 的配置项。
7、 【microcloud-sleuth-8601】修改启动程序类的使用注解:
package cn.study.microcloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.sleuth.zipkin.stream.EnableZipkinStreamServer; import zipkin.server.EnableZipkinServer;
@SpringBootApplication
@EnableCircuitBreaker
@EnableZipkinStreamServer
public class Zipkin_8601_StartSpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(Zipkin_8601_StartSpringCloudApplication.class, args);
}
}
8、 此时依次启动各个微服务之后所有的信息都将被记录到 MySQL 数据库之中,这样即使当前的 zipkin 服务关闭了,那么也可以进行信息的持久化存储,下次启动之后依然可以读取到执行顺序。
SpringCloud系列十二:SpringCloudSleuth(SpringCloudSleuth 简介、SpringCloudSleuth 基本配置、数据采集)的更多相关文章
- SpringCloud系列十二:手动创建Feign
1. 回顾 上文讲解了自定义Feign.但是在某些场景下,前文自定义Feign的方式满足不了需求,此时可使用Feign Builder API手动创建Feign. 本文围绕以下场景,为大家讲解如何手动 ...
- 爬虫系列(十二) selenium的基本使用
一.selenium 简介 随着网络技术的发展,目前大部分网站都采用动态加载技术,常见的有 JavaScript 动态渲染和 Ajax 动态加载 对于爬取这些网站,一般有两种思路: 分析 Ajax 请 ...
- Web 前端开发精华文章推荐(jQuery、HTML5、CSS3)【系列十二】
2012年12月12日,[<Web 前端开发人员和设计师必读文章>系列十二]和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HT ...
- SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据
原文:SQL Server 2008空间数据应用系列十二:Bing Maps中呈现GeoRSS订阅的空间数据 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Se ...
- Alamofire源码解读系列(十二)之请求(Request)
本篇是Alamofire中的请求抽象层的讲解 前言 在Alamofire中,围绕着Request,设计了很多额外的特性,这也恰恰表明,Request是所有请求的基础部分和发起点.这无疑给我们一个Req ...
- struts2官方 中文教程 系列十二:控制标签
介绍 struts2有一些控制语句的标签,本教程中我们将讨论如何使用 if 和iterator 标签.更多的控制标签可以参见 tags reference. 到此我们新建一个struts2 web 项 ...
- Alamofire源码解读系列(十二)之时间轴(Timeline)
本篇带来Alamofire中关于Timeline的一些思路 前言 Timeline翻译后的意思是时间轴,可以表示一个事件从开始到结束的时间节点.时间轴的概念能够应用在很多地方,比如说微博的主页就是一个 ...
- 跟我学SpringCloud | 第十二篇:Spring Cloud Gateway初探
SpringCloud系列教程 | 第十二篇:Spring Cloud Gateway初探 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如 ...
- SpringBoot系列(十二)过滤器配置详解
SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...
随机推荐
- Anconda 3.7安装以及使用详细教程
Anconda 3.7安装以及使用详细教程 2019-04-17 22:42:03 一.下载anconda 3.7 链接地址:官方地址 二.安装 双击下载好的Anaconda3-2019.03- ...
- pycharm的list中copy的应用
#拷贝的意思 li = [11,22,33,44] v = li.copy() print(v) #输出结果 [11,22,33,44] #浅拷贝
- Sublime Text3 & MinGW & LLVM CLang 安装配置C-C++编译环境
Sublime Text是一款强大的跨平台代码编辑器,小巧而且丰富实用的功能是Visual Studio不能比拟的,但是编译运行是一个软肋,本文通过在sublime中配置g++编译器实现程序的编译功能 ...
- 动态规划——Freedom Trail
题目:https://leetcode.com/problems/freedom-trail/ 额...不解释大意了,题目我也不想写过程了有点繁琐,直接给出代码: public int findRot ...
- [HEOI/TJOI2016]序列
Description: 给你一个序列,每个数可能变化为另一个数,每次最多有一个数变化 求最长的子序列,无论如何变化,这个子序列都不下降 Hint: \(n \le 10^5\) Solution: ...
- 实现logstash6.4.3 同步mysql数据到Elasticsearch6.4.3
本文旨在实践把mysql已有的数据同步到elasticsearch中,使用的版本是6.4.3,对于其它6.x版本理应是一样的处理方式. 本文目录: 1.初始化Elasticsearch 6.4.3 1 ...
- linux-centos基本使用(一)
1. 基本配置 1.常用软件安装 yum install -y bash-completion vim lrzsz wget expect net-tools nc nmap tree dos2uni ...
- 一个for实现9*9乘法表
今天看到别人一个博客提出来一个非常有趣的题目,写一个9*9的乘法表,要求只使用且仅使用一个for来实现9*9乘法表的打印.(不使用if,switch,?:) 可以用任何语言实现,下面是博主给的ja ...
- MyBatis(五)select返回list数据
(1)接口中编写方法 public List<Emp> getEmps(String lastName); (2)编写Mapper文件 <select id="getEmp ...
- Java课程之团队开发(NABCD需求分析)
N.需求 1.学生基本的录入课程功能 2.学生对于空教室使用的需求(自习或者是活动占用) 3.学生对于具体课程的查询需求 A.做法 1.制作出基于安卓的课程查询,录入以及教室查询应用软件 B.好处 1 ...