我在spring-boot应用程序中使用spring-cloud-slueth-core-1.3.3库中的TraceAsyncRestTemplate
来实现对请求的并行处理。
我的RX I / O调度程序线程中有我的请求ID和其他请求参数(使用Rx Java并将其配置为复制请求上下文)但是AsyncRestTemplate
一旦调出I / O调度程序线程,请求参数不再存在于它们中。经过调查后,我意识到这种情况正在发生,因为AsyncRestTemplate
使用Apache HttpAsynClient
来启动没有父上下文的子线程(I / O转发线程)。
记录快照:
Rx Java Threads有请求Context ====>
2018-05-09 09:30:38,947 [RxIoScheduler-4] INFO APP = abcd | REQUEST_ID = IWAN3 | TRACE_ID = f70f4f94824b83dc | SPAN_ID = f70f4f94824b83dc | CLIENT_ID = CLIENTID | CLIENT_VERSION = 1.0 | UUID = 9c68ac15-148f-4a4c-aca1-704836f341ad
I / O dsipatcher缺少请求上下文=====>
2018-05-09 09:30:38,454 [I / O调度员1] DEBUG APP = abcd | REQUEST_ID = | TRACE_ID = | SPAN_ID = | CLIENT_ID = | CLIENT_VERSION = | UUID =
我在配置中使用了bean HttpComponentsAsynClientHttpRequestFactory
,它启用了HttpAsynClient
:
private AsyncClientHttpRequestFactory asyncClientHttpRequestFactory()
{
return new HttpComponentsAsyncClientHttpRequestFactory();
}
如果我没有配置这个bean,那么TraceAsyncRestTemplate
会尝试使用DefaultRestTemplate,并开始执行Synchronized。
来自 Slueth lib的配置:
private TraceAsyncClientHttpRequestFactoryWrapper traceAsyncClientHttpRequestFactory() {
ClientHttpRequestFactory clientFactory = this.clientHttpRequestFactory;
AsyncClientHttpRequestFactory asyncClientFactory = this.asyncClientHttpRequestFactory;
if (clientFactory == null) {
clientFactory = this.defaultClientHttpRequestFactory(this.tracer);
}
if (asyncClientFactory == null) {
asyncClientFactory = clientFactory instanceof AsyncClientHttpRequestFactory ? (AsyncClientHttpRequestFactory)clientFactory : this.defaultClientHttpRequestFactory(this.tracer);
}
return new TraceAsyncClientHttpRequestFactoryWrapper(this.tracer, this.spanInjector, (AsyncClientHttpRequestFactory)asyncClientFactory, (ClientHttpRequestFactory)clientFactory, this.httpTraceKeysInjector);
}