2017-11-10 26 views
0

grpc-java를 사용하고 있으며 서비스 A, B 및 C가 3 개 있습니다. 서비스 A를 호출하고 서비스 A와 B 및 C를 호출합니다. B와 C에 대한 호출에서 Hystrix를 사용하고 있습니다. C가 차례 차례 다른 스레드를 생성합니다. 다른 서비스를 호출합니다.gRPC의 컨텍스트에서 다른 스레드/threadPool로 traceId를 전달하는 방법은 무엇입니까?

traceId를 전달하는 ClientInterceptors 및 ServerInterceptors가 있습니다. 컨텍스트에서 traceIds를 볼 수 있으며 gRPC 작업자 스레드 일 때만 기록하지만 호출이 다른 스레드 (RxIoScheduler 스레드 또는 Hystrix 스레드)로 이동하면 잃게됩니다. traceId를 다른 스레드의 요청과 다른 실행자 서비스와 스레드 풀 사이의 요청에 전달하려면 어떻게해야합니까?

답변

0

미세한 방식으로 전파하는 것이 가능하지만 (executor.execute(Context.current().wrap(runnable))) Context 전파를 스레드 간 작업 전송으로 통합해야합니다. 많은 애플리케이션의 경우, 그 즉시이 생성 될 때 wrapping the "main" executor 것처럼 간단 할 것 :

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

응용 프로그램의 시작 부분에 한 번 그렇게 한 다음 대부분의 전파에 대해 걱정 중지합니다.

그러나 응용 프로그램은 다양합니다.

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)); 
    } 
} 
예를 들어, 생성 응용 프로그램 Thread의 직접 아마도 호출 스레드의 Context Thread에 전파합니다 ThreadFactory을해야한다