技术标签: # SpringCloud
Sleuth 通过 traceId 实现了对分布式系统调用链路的跟踪。在一次服务请求链路中,会保持并传递一个 traceId,从而将不同服务的请求跟踪信息串联起来,不同服务的 traceId 相同表示处在同一请求链中。
基于 HTTP 请求的数据传递有两种方式:一种是做为参数传递,另一种是做为头信息传递。而 Sleuth 的 traceId 属于附加信息,不参与实际的业务,所以做为参数传递并不合适,实际也是作为头信息来传递的。
分布式系统中的服务调用链路跟踪在理论上并不复杂,主要有个关键点,一个是为请求链路创建唯一跟踪标识,二个统计各个处理单元的延迟时间。
参考 Spring Cloud系列(十三):分布式服务链路跟踪 Sleuth 中的示例项目,Sleuth 会在请求的 Header 中增加实现跟踪需的信息,给远程调用接口打上断点,使用 request.getHeaderNames() 取出所有头信息。
["x-b3-spanid","x-b3-parentspanid","x-b3-sampled","x-b3-traceid","accept","user-agent","host","connection"]
可以看到,在请求头信息中多了 4 个属性:
Sleuth 会把跟踪数据 (appname、traceId、spanId、exportable) 添加到 Slf4J MDC 中,因此您可以从日志聚合器中的给定跟踪或跨度中提取所有日志,如以下示例日志中所示:
2016-02-02 15:30:57.902 INFO [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ... 2016-02-02 15:30:58.372 ERROR [bar,6bfd228dc00d216b,6bfd228dc00d216b,false] 23030 --- [nio-8081-exec-3] ... 2016-02-02 15:31:01.936 INFO [bar,46ab0d418373cbc9,46ab0d418373cbc9,false] 23030 --- [nio-8081-exec-4] ...
MDC 的实现实际是将需要记录到日志的信息设置到当前线程的上下文(ThreadContext)中。
MDC 中的信息:[appname,traceId,spanId,exportable]
跟踪信息收集默认是 0.1(10%) 的采样比例,可通过 probability 属性修改;或可采用每秒速率来控制采集数据,属性是 rate。
# 跟踪信息收集采样比例,默认 0.1,为 1 是即 100%,收集所有 spring.sleuth.sampler.probability=1 # 每秒速率,即每秒最多能跟踪的请求,rate 优先 spring.sleuth.sampler.rate=50
源码分析基于Spring Cloud Hoxton,Spring Cloud Sleuth版本为2.2.0.RELEASE
SpringBoot 2开始使用了brave框架完成日志收集,brave是zipkin官方提供的java版本客户端,它将收集的跟踪信息,以Span的形式上报给Zipkin系统。
下面假设有client,server两个工程,调用链路如下:
用户 -> client --> server
我们来探讨以下几个问题
用户调用client时,client如何生成Span信息
client调用server时,如何将Span发送到server
server如何接收cliend的Span信息
client,server如何发送Span到zipkin
注意,本文是基于SpringMvc+RestTeamplate的请求调用的,而非WebFlux异步调用。
问题1和问题3,都由LazyTracingFilter处理,它在TraceWebServletAutoConfiguration中初始化。(本文部分源码来自于brave框架)LazyTracingFilter#doFilter -> (brave)TracingFilter#doFilter
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
...
// #1
Span span = handler.handleReceive(new HttpServerRequest(httpRequest));
// #2
request.setAttribute(SpanCustomizer.class.getName(), span.customizer());
request.setAttribute(TraceContext.class.getName(), span.context());
Throwable error = null;
// #3
Scope scope = currentTraceContext.newScope(span.context());
try {
chain.doFilter(httpRequest, httpResponse);
} ...
finally {
// #4
scope.close();
if (servlet.isAsync(httpRequest)) { // we don't have the actual response, handle later
servlet.handleAsync(handler, httpRequest, httpResponse, span);
} else { // we have a synchronous response, so we can finish the span
handler.handleSend(servlet.httpServerResponse(httpRequest, httpResponse), error, span);
}
}
}
public Span handleReceive(HttpServerRequest request) {
// #1
Span span = nextSpan(defaultExtractor.extract(request), request);
// #2
return handleStart(new HttpServerRequest.ToHttpAdapter(request), request.unwrap(), span);
}
LazyTracingClientHttpRequestInterceptor负责实现该功能。它在TraceWebClientAutoConfiguration中构建。LazyTracingClientHttpRequestInterceptor#interceptor -> TracingClientHttpRequestInterceptor#intercept(brave)
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
// #1
Span span = handler.handleSend(new HttpClientRequest(request));
HttpClientResponse response = null;
Throwable error = null;
// #2
try (Tracer.SpanInScope ws = tracer.withSpanInScope(span)) {
// #3
ClientHttpResponse result = execution.execute(request, body);
response = new HttpClientResponse(result);
return result;
} catch (IOException | RuntimeException | Error e) {
error = e;
throw e;
} finally {
// #4
handler.handleReceive(response, error, span);
}
}
public Span handleSend(HttpClientRequest request, Span span) {
...
// #1
defaultInjector.inject(span.context(), request);
// #2
return handleStart(new HttpClientRequest.ToHttpAdapter(request), request.unwrap(), span);
}
那么Spring如何将span发送到zipkin?先看一个brave框架上报Span的简单例子
Sender sender = URLConnectionSender.create("http://localhost:9411/api/v1/spans")
AsyncReporter asyncReporter = AsyncReporter.builder(sender)
.build(SpanBytesEncoder.JSON_V2);
Tracing tracing = Tracing.newBuilder()
.spanReporter(asyncReporter)
.build();
Tracer tracer = tracing.tracer();
Span span = tracer.newTrace().name("encode").start();
...
span.finish();
我们并不要自己上报数据,span.finish()
方法中,brave会帮我们完成上报Span工作。
看一下上例中brave的组件类
在Spring Cloud Sleuth中,
文章浏览阅读120次。1.薪资丰厚: 基本薪资+绩效+项目奖金+年终奖2.福利: 和正式员工福利基本看齐,共享工位,免费夜宵,班车,一流办公环境,月末周六双倍工资3.技术栈:C/C+当用例很多的时候,每次。不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦。不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦虑不焦。
文章浏览阅读871次,点赞11次,收藏3次。*单片机设计介绍, 基于51单片机冰箱温度控制器设计。_基于51单片机的智能冰箱控制系统设计
文章浏览阅读4.8k次。一、sftp服务器进入root模式(下面的操作默认都是在root用户下)#安装openssh-serverapt-get install -y openssh-server创建sftp的组和用户#创建sftp-users组groupadd sftp-users#创建sftp用户目录alicemkdir /home/alice#创建sftp用户alice,并且绑定其主目..._ubuntu sftp服务器查看用户和密码
文章浏览阅读5.9k次,点赞9次,收藏16次。解决了在simulink中使用s-function遇到的报错:State derivatives returned by S-function 'demo' in 'test/S-Function' during flag=1 call must be a real vector of length 2 _state derivatives returned by s-function 'pmsm' in 'ipmsm/ipmsm/s-function1
文章浏览阅读3.1k次。1. 打开配置文件Mac 如下图2. 在文件内部添加这段文字,就可以了:"update_check":false _mac sublime text 取消更新提示
文章浏览阅读548次,点赞10次,收藏6次。Linux系统下DNS配置指南_linux 服务器修改网络dns
文章浏览阅读1.6k次。将Total Commander设置为“默认”文件管理器?法一:开始,运行,输入regedit ,回车: 定位到HKEY_LOCAL_MACHINE_total commander默认文件管理器
文章浏览阅读7.7k次。反序列化无法找到程序集提示找不到程序集. 原因是序列化时把序列化类的命名空间等信息保存了,但应用程序和类库的命名空间可能是不一样的,所以提示找不到程序集. 解决方法如下: 方法1.将dll加入强名称,注册到全局程序集缓存中 方法2.在反序列化使用的IFormatter 对象加入Binder 属性,使其获取要反序列化的对象所在的程序集_未能找到程序集“g:\c#%5cc#%20%e4%b8%8a%e4%bd%8d%e6%9c%ba%5c%e7%a9%ba%e5%8e%8
文章浏览阅读234次。Jet-Lube EZY-Turn Kopr-Kote Deacon Moly-Lith 12|34 21 (Oilfield) 21 (Water Well) 21 Arctic (Oilfield) 21 Arctic (Water Well) 550 Extreme 550 Extreme All Weather 713-670-5700 769 Lubricant 930 Whitmore Drive Alco-EP Alco-EP ECF Alco-EP-73 Plus AP-1 AP-1W _dpezy
文章浏览阅读511次。本文从本人简书博客同步过来在上一篇中我们简要地介绍了 mpi4py 中的单边通信概念,下面我们将介绍单边通信的相关操作。创建/释放窗口对象注意:在使用单边通信操作之前,所有进程都须通过共同参与的创建窗口操作公开声明自己可供访问的内存空间。创建和释放窗口对象的方法(MPI.Win 类方法)接口如下:Create(type cls, memory, int disp_unit=..._win.fence() win.free()
文章浏览阅读551次,点赞23次,收藏7次。mit6.824的lab1,实现MapReduce_mitmapreduce实现csdn
文章浏览阅读6.6k次,点赞10次,收藏82次。在刚开始学Android开发时,下载好了Android studio,但是不知道如何下手,现在就通过一个简单的小项目熟悉如何使用这个软件进行Android开发。前提:下载好Android studio并配置好相关环境。首先介绍一下Android开发过程中需要修改的三类文件:位于java包下的各类activity文件:实现了用户与软件的交互,主要为java代码实现。位于res包下的.xml文件:在layout包里的为布局文件,即Android界面显示的视图,而drawable里则放置了某个控件的_android studio简单项目