问题
I am using grpc-java and have 3 services, A, B and C. I call service A and then service A calls B and C. I am using Hystrix in the calls to B and C. C in turn spawns another thread to call another service.
I have ClientInterceptors and ServerInterceptors which passes around the traceId. I can see the traceIds in the Context and logs as long as it is a gRPC worker thread but lose them when the call moves to another thread - RxIoScheduler thread or Hystrix thread. How do I pass the traceId around between requests on different threads and between different executor service and thread pools?
回答1:
While it is possible to propagate in a fine-grained way (like executor.execute(Context.current().wrap(runnable))), you should try to integrate Context propagation into cross-thread work transfer. For many applications, that'd be as simple as wrapping the "main" executor as soon as it is created:
executor = Context.currentContextExecutor(executor);
// executor now auto-propagates
Do that once at the beginning of your application and then you mostly stop worrying about propagation.
But applications will vary. For example, applications that create Threads directly should probably make a ThreadFactory that propagates the calling thread's Context to the Thread:
class PropagatingThreadFactory implements ThreadFactory {
private final ThreadFactory delegate;
public PropagatingThreadFactory(ThreadFactory d) {delegate = d;}
@Override public Thread newThread(Runnable r) {
return delegate.newThread(Context.current().wrap(r));
}
}
来源:https://stackoverflow.com/questions/47231289/how-to-pass-on-a-traceid-from-grpcs-context-to-another-thread-threadpool