原文地址:http://www.cnblogs.com/skyblog/p/6213683.html

随着微服务数量不断增长,需要跟踪一个请求从一个微服务到下一个微服务的传播过程, Spring Cloud Sleuth 正是解决这个问题,它在日志中引入唯一ID,以保证微服务调用之间的一致性,这样你就能跟踪某个请求是如何从一个微服务传递到下一个。

  如果你有使用AOP拦截Servlet的经验,做一个基于AOP的简单服务统计和跟踪很容易。但要像Zipkin那样能够跟踪服务调用链就比较困难了。所谓调用链,就是A服务调用B服务,B服务又调用了C、D服务。这样一个链要想统计跟踪,要写不少代码。而Spring Cloud Sleuth能让你不写一行代码的情况下完成这些。

  本文涉及5个spring boot工程:

 

  前三个验证基本的Sleuth使用,后面两个演示如果使用消息中间件作为Zipkin的源,以及如果将Zipkin的监控数据配置到mysql数据库。由于前者和后者在配置方面相差较大,因此有必要将其分开展示。简单的Sleuth应用,在这里是指Sleuth http应用,就是Sleuth通过http请求的方式将监控日志推送到Zipkin服务器。如图所示,cloud-sleuth-server其实就是Zipkin Server它接收来自微服务cloud-sleuth-service1和cloud-sleuth-service2的监控日志推送,cloud-sleuth-service1调用了cloud-sleuth-service2,形成一个简单的调用链。其中cloud-sleuth-service1 pom配置:

  

<dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
</dependencies>

SleuthClientApplication应用:

@SpringBootApplication
public class SleuthClientApplication { @Bean
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(SleuthClientApplication.class, args);
}
} @RestController
class HomeController { private static final Log log = LogFactory.getLog(HomeController.class);
@Autowired
private RestTemplate restTemplate; private String url="http://localhost:9986"; @RequestMapping("/service1")
public String service1() throws Exception {
log.info("service1");
Thread.sleep(200L);
String s = this.restTemplate.getForObject(url + "/service2", String.class);
return s;
}
}
   

  

application.properties:
spring.application.name=sleuthService1
server.port=
spring.zipkin.baseUrl=http://localhost:
spring.zipkin.enabled=true

  只需要在pom文件里配置spring-cloud-sleuth-zipkin并且在配置文件里配置spring.zipkin.baseUrl,那么service1就可以把监控日志发送给目标是baseUrl指向的Zipkin服务器,就是会自动调用zipkin的某个rest接口将监控日志传给它。

  Zipkin Server的pom配置:

 

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin</groupId>
<artifactId>zipkin-ui</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

SleuthServerApplication:

@SpringBootApplication
@EnableZipkinServer
public class SleuthServerApplication { public static void main(String[] args) {
SpringApplication.run(SleuthServerApplication.class,args);
}
}
 
 

  application.properties配置:

spring.application.name=sleuthServer
server.port=

  至于service2,配置和service1一样,就是Application里面增加了servcie2 rest接口:

@SpringBootApplication
public class SleuthClientApplication { public static void main(String[] args) {
SpringApplication.run(SleuthClientApplication.class, args);
}
} @RestController
class HiController { private static final Log log = LogFactory.getLog(HiController.class); @RequestMapping("/service2")
public String hi() throws Exception {
log.info("service2");
Thread.sleep(100L);
return "service2";
}
}
 
 

  运行后,访问service1,再按照zipkin server配置的端口打开zipkin server的监控页面,运行结果如下:

  点开某一个请求后,会看到详细的调用链请求信息:

点开调用链的某个请求可以看更详细的请求信息:

  上图所示,依次是:

  Sleuthservcie1 Client Send:service1发起请求service2的时间戳。

  Sleuthservcie2 Server Receive:service2收到请求的时间戳。

  Sleuthservcie2 Server Send:service2处理完发送结果时间戳。

  Sleuthservcie1 Client Receive:service1收到结果的时间戳。

  图中最上方就是这个请求所用的总的时间,各个环节的处理请求时间一目了然。

  如果是生产环境,一般需要用到第二套方案。就是先把日志发送到消息队列,然后再由zipkin接受,zipkin接收后保存日志到数据库。具体的配置也很简单,要注意的地方就是spring
官方的例子找不到消息中间件的具体配置,可能是在某个地方有默认,笔者还是自己配置了一下:

  spring.sleuth.sampler.percentage=.

  spring.rabbitmq.host=...

  spring.rabbitmq.port=

  spring.rabbitmq.username=guest

  spring.rabbitmq.password=guest

  spring.rabbitmq.virtualHost=/

  消息队列只需要配置上就会自动创建队列。数据库配置也是如此,其官方例子中也有,只需配置上就会自动建表。

  spring.datasource.schema=classpath:/mysql.sql

  spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:/test

  spring.datasource.username=root

  spring.datasource.password=

  spring.datasource.initialize=true

  spring.datasource.continueOnError=true

  spring.sleuth.enabled=false

  zipkin.storage.type=mysql

  具体参照源码,源码在老地方:https://git.oschina.net/zhou666/spring-cloud-7simple

Spring Sleuth和Zipkin跟踪微服务的更多相关文章

  1. 使用Spring Sleuth和Zipkin跟踪微服务

    随着微服务数量不断增长,需要跟踪一个请求从一个微服务到下一个微服务的传播过程, Spring Cloud Sleuth 正是解决这个问题,它在日志中引入唯一ID,以保证微服务调用之间的一致性,这样你就 ...

  2. SpringCloud初体验:六、利用 Sleuth 和 Zipkin 给微服务加上链路监控追踪查看功能

    首先:装上 Zipkin 服务,收集调用链跟踪数据,体验时装在了本机docker上, 方便快捷 docker run -d -p : openzipkin/zipkin 安装后访问地址也是 9411端 ...

  3. 阿里高级架构师教你使用Spring Cloud Sleuth跟踪微服务

    随着微服务数量不断增长,需要跟踪一个请求从一个微服务到下一个微服务的传播过程,Spring Cloud Sleuth 正是解决这个问题,它在日志中引入唯一ID,以保证微服务调用之间的一致性,这样你就能 ...

  4. Zipkin和微服务链路跟踪

    https://cloud.tencent.com/developer/article/1082821 Zipkin和微服务链路跟踪 本期分享的内容是有关zipkin和分布式跟踪的内容. 首先,我们还 ...

  5. 【译文】用Spring Cloud和Docker搭建微服务平台

    by Kenny Bastani Sunday, July 12, 2015 转自:http://www.kennybastani.com/2015/07/spring-cloud-docker-mi ...

  6. Spring Cloud和Docker搭建微服务平台

    用Spring Cloud和Docker搭建微服务平台 This blog series will introduce you to some of the foundational concepts ...

  7. 从 Spring Cloud 开始,聊聊微服务架构实践之路

    [编者的话]随着公司业务量的飞速发展,平台面临的挑战已经远远大于业务,需求量不断增加,技术人员数量增加,面临的复杂度也大大增加.在这个背景下,平台的技术架构也完成了从传统的单体应用到微服务化的演进. ...

  8. 基于Spring Boot、Spring Cloud、Docker的微服务系统架构实践

    由于最近公司业务需要,需要搭建基于Spring Cloud的微服务系统.遍访各大搜索引擎,发现国内资料少之又少,也难怪,国内Dubbo正统治着天下.但是,一个技术总有它的瓶颈,Dubbo也有它捉襟见肘 ...

  9. 使用 Spring Cloud 和 Docker 构建微服务架构

    如何使用Spring Boot.Spring Cloud.Docker和Netflix的一些开源工具来构建一个微服务架构. 本文通过使用Spring Boot.Spring Cloud和Docker构 ...

随机推荐

  1. [thinkphp] 获取根目录绝对路径

    $root = realpath(__ROOT__);

  2. P1059 明明的随机数【去重排序】

    题目描述 明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应 ...

  3. 暴力 【p4092】[HEOI2016/TJOI2016]树

    Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下两种操作: 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其 ...

  4. [ARC100]E:Or Plus Max(FZT)

    https://arc100.contest.atcoder.jp/tasks/arc100_c 一个很自然的想法是,对于每个K求出i or j=k的所有a[i]+a[j]的最大值ans[k],答案就 ...

  5. [CTSC2017]吉夫特(Lucas定理,DP)

    送70分,预处理组合数是否为偶数即可. 剩下的数据,根据Lucas定理的推论可得当且仅当n&m=n的时候,C(n,m)为奇数.这样就可以直接DP了,对于每个数,考虑它对后面的数的影响即可,直接 ...

  6. [BZOJ 4117] Weather Report

    Link: BZOJ 4117 传送门 Solution: 第一次写$Huffman Tree$相关,发现就是个合并果子? 此题可以将每一种情况的概率和排列总数算出,接下来就是按照$Haffman T ...

  7. 【贪心】Codeforces Round #402 (Div. 2) C. Dishonest Sellers

    按照b[i]-a[i],对物品从大到小排序,如果这个值大于零,肯定要立刻购买,倘若小于0了,但是没买够K个的话,也得立刻购买. #include<cstdio> #include<a ...

  8. 【kd-tree】bzoj3489 A simple rmq problem

    Orz zyf教给蒟蒻做法 蒟蒻并不会这题正解……(可持久化树套树?...Orz 对于每个点,我们可以求出pre[i],nex[i],那么询问的答案就是:求max (a[i]),其中 i 满足(pre ...

  9. 【线段树】bzoj3922 Karin的弹幕

    设置一个值K. d<=K:建立多组线段树:d>K:暴力. 最优时间复杂度的伪计算: O(n*K*logn(建树)+m*logn(询问类型1)+m*n/K(询问类型2)+m*K*logn(修 ...

  10. 1.4(学习笔记)JSP自定义标签

    一.JSP自定义标签 JSP自定义标签,可以通过实现Tag接口.继承TagSupport类来设置标签功能. 后续通过配置文件将标签和具体的实现类关联. 二.自定义第一个标签(实现Tag接口) 自定义标 ...