-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
在Reactor Webclient的场景下,使用TTL MDC不生效 #5
Comments
需要与 Reactive 框架的 执行 集成 TTL @naturallight
|
static { | |
// WIP! Not works yet!! | |
// TTL integration for Reactor | |
Schedulers.addExecutorServiceDecorator("TransmittableThreadLocal", | |
(scheduler, scheduledExecutorService) -> TtlExecutors.getTtlScheduledExecutorService(scheduledExecutorService)); | |
Hooks.onEachOperator("TransmittableThreadLocal", objectPublisher -> new Publisher<Object>() { | |
final Object capture = capture(); | |
@Override | |
public void subscribe(Subscriber<? super Object> s) { | |
final Object backup = replay(capture); | |
try { | |
objectPublisher.subscribe(s); | |
} finally { | |
restore(backup); | |
} | |
} | |
}); | |
} |
看了示例,加入了Hooks这段逻辑后,运行用例会报这样的异常: java.lang.ExceptionInInitializerError
at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.build(DefaultWebClientBuilder.java:212)
at org.springframework.web.reactive.function.client.WebClient.create(WebClient.java:134)
at com.aksk.webfluxdemo.Log4j2Test.testLogWithWebClient(Log4j2Test.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassCastException: com.aksk.webfluxdemo.Log4j2Test$1 cannot be cast to reactor.core.publisher.Mono
at reactor.core.publisher.Mono.onAssembly(Mono.java:4282)
at reactor.core.publisher.Mono.error(Mono.java:261)
at org.springframework.web.reactive.function.client.DefaultWebClient.<clinit>(DefaultWebClient.java:70)
... 25 more |
是的。 这个 需要找到 像 把 传递入 |
请教一下找到这种hook方式了么 |
WebClient.create().method(HttpMethod.POST)
.uri(URI.create("http://127.0.0.1:8013/api/flow/test/a"))
.accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON)
.retrieve().bodyToMono(Map.class)
.subscribe(TtlWrappers.wrapConsumer((map) -> {
try {
Thread.sleep(ThreadLocalRandom.current().nextInt(0,50));
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!Objects.equals(SampleController.LOCAL.get(), num)) {
System.out.println("aaaaaaaaaaa");
}
}));
|
样例代码如下:
log4j2.xml配置如下:
输出入下:
其中,reactor-http-nio-X应该是netty的线程,期望在第二次发请求时,记录的requestid为2。然而现状是,只有在主线程中记录了2,调用webclient后仍然记录的是第一次的requestid
The text was updated successfully, but these errors were encountered: