- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章spring-cloud Sleuth的使用方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
一直没弄明白sleuth的tracercontext是如何创建和传递的,闲来无事研究了一下。由于对sleuth的源码不熟悉,准备通过debug brave.tracer的nextid()方法,查看方法调用栈来找来龙去脉.
首先创建两个service a和b,记作srva、srvb,在srva中添加testa controller,sevb中添加testb controller,testa中通过feign调用testb.
先看当用户通过浏览器调用srva的时候,srva是作为server的.
configuration: tracewebservletautoconfiguration==>tracingfilter tracehttpautoconfiguration==>httptracing traceautoconfiguration==>tracing sleuthlogautoconfiguration.slf4jconfiguration==>currenttracecontext 。
配置中,tracingfilter在实例化时需要一个httptracing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public
static
filter create(httptracing httptracing) {
return
new
tracingfilter(httptracing);
}
//servlet运行时类
final
servletruntime servlet = servletruntime.get();
//slf4jcurrenttracecontext
final
currenttracecontext currenttracecontext;
final
tracer tracer;
final
httpserverhandler<httpservletrequest, httpservletresponse> handler;
//tracecontext的数据提取器
final
tracecontext.extractor<httpservletrequest> extractor;
tracingfilter(httptracing httptracing) {
tracer = httptracing.tracing().tracer();
currenttracecontext = httptracing.tracing().currenttracecontext();
handler = httpserverhandler.create(httptracing, adapter);
extractor = httptracing.tracing().propagation().extractor(getter);
}
|
httptracing builder模式构造时接收一个tracing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
tracing tracing;
//客户端span解析器
httpclientparser clientparser;
string servername;
//服务端span解析器
httpserverparser serverparser;
httpsampler clientsampler, serversampler;
builder(tracing tracing) {
if
(tracing ==
null
)
throw
new
nullpointerexception(
"tracing == null"
);
final
errorparser errorparser = tracing.errorparser();
this
.tracing = tracing;
this
.servername =
""
;
// override to re-use any custom error parser from the tracing component
this
.clientparser =
new
httpclientparser() {
@override
protected
errorparser errorparser() {
return
errorparser;
}
};
this
.serverparser =
new
httpserverparser() {
@override
protected
errorparser errorparser() {
return
errorparser;
}
};
this
.clientsampler = httpsampler.trace_id;
this
.serversampler(httpsampler.trace_id);
}
|
tracing实例化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@bean
@conditionalonmissingbean
// note: stable bean name as might be used outside sleuth
tracing tracing(
@value
(
"${spring.zipkin.service.name:${spring.application.name:default}}"
) string servicename,
propagation.factory factory,
currenttracecontext currenttracecontext,
reporter<zipkin2.span> reporter,
sampler sampler,
errorparser errorparser,
sleuthproperties sleuthproperties
) {
return
tracing.newbuilder()
.sampler(sampler)
.errorparser(errorparser)
.localservicename(servicename)
//extrafieldpropagation.factory
.propagationfactory(factory)
.currenttracecontext(currenttracecontext)
.spanreporter(adjustedreporter(reporter))
.traceid128bit(sleuthproperties.istraceid128())
.supportsjoin(sleuthproperties.issupportsjoin())
.build();
}
|
下面看tracingfilter的dofilter:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
span span = handler.handlereceive(extractor, httprequest);
// add attributes for explicit access to customization or span context
request.setattribute(spancustomizer.
class
.getname(), span.customizer());
request.setattribute(tracecontext.
class
.getname(), span.context());
throwable error =
null
;
scope scope = currenttracecontext.newscope(span.context());
try
{
// any downstream code can see tracer.currentspan() or use tracer.currentspancustomizer()
chain.dofilter(httprequest, httpresponse);
}
catch
(ioexception | servletexception | runtimeexception | error e) {
error = e;
throw
e;
}
finally
{
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(adapter.adaptresponse(httprequest, httpresponse), error, span);
}
}
}
|
在sleuthlogautoconfiguration中如果有slfj的包,则注入currenttracecontext:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@configuration
@conditionalonclass
(mdc.
class
)
@enableconfigurationproperties
(sleuthslf4jproperties.
class
)
protected
static
class
slf4jconfiguration {
@bean
@conditionalonproperty
(value =
"spring.sleuth.log.slf4j.enabled"
, matchifmissing =
true
)
@conditionalonmissingbean
public
currenttracecontext slf4jspanlogger() {
return
slf4jcurrenttracecontext.create();
}
...
}
|
slf4jcurrenttracecontext中,delegate就是currenttracecontext.default.inheritable():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
public
static
final
class
default
extends
currenttracecontext {
static
final
threadlocal<tracecontext>
default
=
new
threadlocal<>();
// inheritable as brave 3's threadlocalserverclientandlocalspanstate was inheritable
static
final
inheritablethreadlocal<tracecontext> inheritable =
new
inheritablethreadlocal<>();
final
threadlocal<tracecontext> local;
//静态方法create,local对象为threadlocal类型
/** uses a non-inheritable static thread local */
public
static
currenttracecontext create() {
return
new
default
(
default
);
}
//local对象为inheritablethreadlocal类型
//官方文档指出,inheritable方法在线程池的环境中需谨慎使用,可能会取出错误的tracecontext,这样会导致span等信息会记录并关联到错误的traceid上
/**
* uses an inheritable static thread local which allows arbitrary calls to {@link
* thread#start()} to automatically inherit this context. this feature is available as it is was
* the default in brave 3, because some users couldn't control threads in their applications.
*
* <p>this can be a problem in scenarios such as thread pool expansion, leading to data being
* recorded in the wrong span, or spans with the wrong parent. if you are impacted by this,
* switch to {@link #create()}.
*/
public
static
currenttracecontext inheritable() {
return
new
default
(inheritable);
}
default
(threadlocal<tracecontext> local) {
if
(local ==
null
)
throw
new
nullpointerexception(
"local == null"
);
this
.local = local;
}
@override
public
tracecontext get() {
return
local.get();
}
//替换当前tracecontext,close方法将之前的tracecontext设置回去
//scope接口继承了closeable接口,在try中使用会自动调用close方法,为了避免用户忘记close方法,还提供了runnable,callable,executor,executorservice包装方法
@override
public
scope newscope(
@nullable
tracecontext currentspan) {
final
tracecontext previous = local.get();
local.set(currentspan);
class
defaultcurrenttracecontextscope
implements
scope {
@override
public
void
close() {
local.set(previous);
}
}
return
new
defaultcurrenttracecontextscope();
}
}
|
slf4jcurrenttracecontext的delegate使用的就是一个inheritablethreadlocal,inheritablethreadlocal在创建子线程的时候,会将父线程的inheritablethreadlocals继承下来。这样就实现了tracecontext在父子线程中的传递.
看一下currenttracecontext的maybescope:
1
2
3
4
5
6
7
8
9
10
11
|
//返回一个新的scope,如果当前scope就是传入的scope,返回一个空scope
public
scope maybescope(
@nullable
tracecontext currentspan) {
//获取当前tracecontext
tracecontext currentscope = get();
//如果传入的tracecontext为空,且当前tracecontext为空返回空scope
if
(currentspan ==
null
) {
if
(currentscope ==
null
)
return
scope.noop;
return
newscope(
null
);
}
return
currentspan.equals(currentscope) ? scope.noop : newscope(currentspan);
}
|
tracingfilter中httpserverhandler解析request:请输入代码 。
2.srva请求到servb时作为client.
traceloadbalancerfeignclient-->loadbalancerfeignclient-->feignloadbalancer-->lazytracingfeignclient-->client 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://segmentfault.com/a/1190000018115247 。
最后此篇关于spring-cloud Sleuth的使用方法的文章就讲到这里了,如果你想了解更多关于spring-cloud Sleuth的使用方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有一个 Cloud Run 服务,它通过 SQLAlchemy 访问 Cloud SQL 实例.但是,在 Cloud Run 的日志中,我看到 CloudSQL connection failed.
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4年前关闭。 Improve t
在将 docker 容器镜像部署到 Cloud Run 时,我可以选择一个区域,这很好。 Cloud Run 将构建委托(delegate)给 Cloud Build,后者显然会创建两个存储桶来实现这
我正在尝试将 Cloud Functions 用作由 PubSub 触发的异步后台工作程序,并进行更长时间的工作(以分钟为单位)。完整代码在这里https://github.com/zdenulo/c
这是/etc/cloud/cloud.cfg的内容Ubuntu云16.04镜像: # The top level settings are used as module # and system co
如何从 Google Cloud Function 启动 Cloud Dataflow 作业?我想使用 Google Cloud Functions 作为启用跨服务组合的机制。 最佳答案 我已经包含了
我想使用 Cloud Shell 在我的第二代 Cloud Sql 实例上运行数据库迁移。 我找到了一个 example in the docs关于如何使用 gcloud 进行连接.但是当我运行命令时
我正在尝试使用 Google Cloud PubSub和我的 Google Cloud Dataproc群集,我收到如下身份验证范围错误: { "code" : 403, "errors" :
这是我的用例。 我已经有一个以私有(private)模式部署的 Cloud Run 服务。 (与云功能相同的问题) 我正在开发使用此 Cloud Run 的新服务。我在应用程序中使用默认凭据进行身份验
如何连接到 Cloud SQL 上的数据库,而无需在容器中添加我的凭据文件? 最佳答案 使用 UNIX 域套接字 (Java) 从云运行(完全托管)连接到云 SQL At this time Clou
我有一个google-cloud-ml作业,需要从gs存储桶加载numpy .npz文件。我遵循了this example上关于如何从gs加载.npy文件的操作,但是由于.npz文件已压缩,因此它对我
我想创建链接到另一个项目中的 Cloud Source Repository 的 Cloud Build 触发器。但是当我在应该选择存储库的步骤中时,列表是空的。我尝试了不同的许可,但没有运气。谁能告
向 Twilio 发送 SMS 时,Twilio 会向指定的 URL 发送多个请求,以通过 Webhook 提供该 SMS 传送的状态。我想让这个回调异步,所以我开发了一个 Cloud Functio
我需要更改我的项目 ID,因为要验证的 Firebase 身份验证链接在链接上显示了项目 ID,并且由于品牌 reshape ,项目名称已更改。根据我发现的信息,更改项目 ID 似乎不太可能。我正在考
用于部署我的 Angular 应用程序的 CI/CD 管道已关闭,但我看到 Google Cloud Run 在容器镜像更新后没有部署新修订版。 我已将 Cloud Build 设置为在 GitHub
报价https://cloud.google.com/load-balancing/docs/https/setting-up-https-serverless#enabling While Goog
Cloud Spanner 提供了两种不同的 API。 Cloud Spanner 读取与 Cloud Spanner SQL API 之间有什么区别? 最佳答案 在幕后,它们都使用相同的执行机制,因
我是 GCP 堆栈的新手,所以我对用于存储数据的 GCP 技术数量感到非常困惑: https://cloud.google.com/products/storage 虽然上面的文章中没有提到googl
我发现 Google Cloud Functions 的网络出站费用令人惊讶,我正在尝试了解发生这种情况的原因以及如何避免这种情况。 Stackdriver 监控表明有问题的函数是我的 ingest
我使用 Prisma使用 Cloud Run 和 Cloud SQL。在向 prisma.schema 提供 DATABASE_URL 后,它会在运行时抛出一个错误。 Can't reach data
我是一名优秀的程序员,十分优秀!