2017-11-10 2003 views
0

我正在使用grpc-java,並有3個服務,A,B和C.我稱服務A,然後服務A調用B和C.我在調用B和C時使用Hystrix。C又產生另一個線程撥打另一項服務。如何將traceId從gRPC的上下文傳遞到另一個線程/線程池?

我有圍繞traceId傳遞的ClientInterceptors和ServerInterceptors。只要它是gRPC工作線程,我就可以在上下文和日誌中看到traceIds,但在調用移動到另一個線程時會丟失它們 - RxIoScheduler線程或Hystrix線程。如何在不同線程之間以及不同執行程序服務和線程池之間的請求之間傳遞traceId?

回答

0

儘管可以以細粒度的方式傳播(如executor.execute(Context.current().wrap(runnable))),但您應該嘗試將Context傳播集成到跨線程工作傳輸中。對於許多應用中,會盡快,因爲它是建立像wrapping the "main" executor簡單:

executor = Context.currentContextExecutor(executor); 
// executor now auto-propagates 

在你的應用程序的開始做這一次,然後你大多停止擔心傳播。

但是應用程序會有所不同。例如,創建應用程序Thread小號直接或許應該做一個ThreadFactory中傳播調用線程的ContextThread

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)); 
    } 
} 
相關問題