spring cloud sleuth
新建spring boot工程trace-1,添加pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>org.springframework.cloud</groupId>-->
<!--<artifactId>spring-cloud-sleuth-stream</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>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Trace1Application
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
@EnableDiscoveryClient
@SpringBootApplication
public class Trace1Application { public static void main(String[] args) {
SpringApplication.run(Trace1Application.class, args);
} private final Logger logger= LoggerFactory.getLogger(getClass());
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
@RequestMapping(value = "/trace-1",method = RequestMethod.GET)
public String trace(){
logger.info("===call trace-1===");
return restTemplate().getForEntity("http://trace-2/trace-2",String.class).getBody();
}
}
配置
spring.application.name=trace-1
server.port=9101
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
再建一个trace-2,依赖同上
Trace2Application
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; @RestController
@EnableDiscoveryClient
@SpringBootApplication
public class Trace2Application { public static void main(String[] args) {
SpringApplication.run(Trace2Application.class, args);
} private final Logger logger= LoggerFactory.getLogger(getClass()); @RequestMapping(value = "/trace-2",method = RequestMethod.GET)
public String trace(){
logger.info("===call trace-2===");
return "Trace";
}
}
启动之前的eureka-server,启动trace-1和trace-2
访问:http://localhost:9101/trace-1
在控制台中查看日志
trace-1
trace-2
可以看到trace-1中的TraceId c44f784f0a901bd8 已经被传到trace-2中了,这里就实现了服务的跟踪
这里的第二个值是TraceId,第三个值是SpanId,第四个值表示是否将信息输出到Zipkin等服务中收集
这里需要设置一个收集的频率
spring.sleuth.sampler.percentage=1
默认是0.1,改成1方便测试
将trace-1和trace-2中的pom依赖取消注释
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
在docker中运行Zipkin
docker run -d -p 9411:9411 openzipkin/zipkin
配置中添加Zipkin地址
spring.zipkin.base-url=http://10.202.203.29:9411
运行trace-1,trace-2,打开:http://localhost:9101/trace-1 多刷新几次
可以看到第四个值是true
打开Zipkin地址:http://10.202.203.29:9411/zipkin/ 点击查找
查看依赖分析
spring cloud sleuth的更多相关文章
- springcloud(十二):使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请求可能最终需要调用很多次后端服务才能完成,当整个请求变慢或不可用时,我们是无法得知该请求是由某个或某些后端服务引起的,这时就需要解决如何快读定位 ...
- Spring Cloud Sleuth服务链路追踪(zipkin)(转)
这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 一.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案, ...
- SpringCloud(7)服务链路追踪Spring Cloud Sleuth
1.简介 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可.本文主要讲述服务追踪组件zipki ...
- 第八篇: 服务链路追踪(Spring Cloud Sleuth)
一.简介 一个分布式系统由若干分布式服务构成,每一个请求会经过多个业务系统并留下足迹,但是这些分散的数据对于问题排查,或是流程优化都很有限. 要能做到追踪每个请求的完整链路调用,收集链路调用上每个 ...
- 浅尝Spring Cloud Sleuth
Spring Cloud Sleuth提供了分布式追踪(distributed tracing)的一个解决方案.其基本思路是在服务调用的请求和响应中加入ID,标明上下游请求的关系.利用这些信息,可以方 ...
- Spring Cloud Sleuth超详细实战
为什么需要Spring Cloud Sleuth 微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元.由于服务单元数量众多,业务的复杂性,如果出现了错误和异常,很难去 ...
- 史上最简单的SpringCloud教程 | 第九篇: 服务链路追踪(Spring Cloud Sleuth)
这篇文章主要讲述服务追踪组件zipkin,Spring Cloud Sleuth集成了zipkin组件. 注意情况: 该案例使用的spring-boot版本1.5.x,没使用2.0.x, 另外本文图3 ...
- 服务链路追踪(Spring Cloud Sleuth)
sleuth:英 [slu:θ] 美 [sluθ] n.足迹,警犬,侦探vi.做侦探 微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元.由于服务单元数量众多,业务的 ...
- 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 Sleuth 服务跟踪 将跟踪信息存储到数据库
参见上一篇博客:Spring Cloud Sleuth 服务跟踪 参考:zipkin使用mysql保存数据 主要在跟踪服务上配置: 在数据库创建数据库表:(可不创建,在classpath中添加对应的s ...
随机推荐
- java基础-day14
第01天 java面向对象 今日内容介绍 u 接口 u 匿名对象&final u 多态 第1章 接口 1.1 接口的概述 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的 ...
- Foundation-NSRunLoop
Objective-C之run loop详解 Objective-C之run loop详解 RunLoop 详解
- hdu 2688
这题,因为要统计之前有多少个数比当前的数小,所以我用的树状数组 基本代码就是这样,还是比较好想的,只不过我没想出来罢了 #include <iostream> #include <c ...
- tarjan算法--求解无向图的割点和桥
1.桥:是存在于无向图中的这样的一条边,如果去掉这一条边,那么整张无向图会分为两部分,这样的一条边称为桥 也就是说 无向连通图中,如果删除某边后,图变成不连通,则称该边为桥 2.割点:无向连通图中,如 ...
- 验证手机格式的js代码
function isMobil(s) { var patrn = /(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/; ...
- Python自动化开发 - 网络编程
本节内容 1.客户端/服务器架构 2.OSI七层 3.socket层 4.socket是什么 5.套接字发展史及分类 6.套接字工作流程 一.客户端/服务器架构 即Client/Server架构,包括 ...
- 实验4 IIC通讯与EEPROM接口
1. 用C语言编程,利用定时器产生一个0~99秒变化的秒表,并且显示在数码管上,每过一秒将这个变化写入实验板上AT24C02,当关闭实验板电源,并再次打开实验板电源时,单片机从AT24C0 ...
- 验证FluentValidation
FluentValidation https://www.xcode.me/code/fluentvalidation-dot-net-library 这里写得很详细了
- SecureCrt使用SSH2登陆海康相机
海康相机登陆SSH2: 1.安装SecureCRT2.登陆到相机,系统-安全管理-安全服务,勾选启用SSh3,打开SecureCRT,快速链接4.选择协议SSh2,主机Ip填入相机IP,端口默认22, ...
- WPF学习笔记(3):ListView根据内容自动调整列宽
DataGrid中,只要不设置DataGrid的宽度和列宽度,或者将宽度设置为Auto,那么表格就会根据内容自动调整宽度,以显示所有内容.但如果是ListView,按以上方法设置,却达不到列宽自动调整 ...