Skip to content
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

异步发送restTemplate请求获取不到MDC内容 #3

Open
purgeyao opened this issue Mar 31, 2020 · 0 comments
Open

异步发送restTemplate请求获取不到MDC内容 #3

purgeyao opened this issue Mar 31, 2020 · 0 comments

Comments

@purgeyao
Copy link
Member

客户端:
TraceContentFactory#buildTraceContent 异步获取MDC.getCopyOfContextMap();
导致这个线程无法获取到id,因此只有服务名加入Map<String, String> traceContentMap进行发送。

服务端:
客户端异步发送只发送了服务名称,导致TraceContentFactory#storageMDC 方法重新生成id,
// "ParentName" 根据 headerTraceId 判断为空 赋值为 this

    public static void storageMDC(Map<String, String> traceContentMap) {
        String headerTraceId = traceContentMap.get(Constants.LEGACY_TRACE_ID_NAME);
        log.debug("Trace traceId {}", headerTraceId);
        // 如果为空,则表示第一次访问,即上游服务端的请求
        if (StringUtils.isEmpty(headerTraceId)) {
            MDC.put(Constants.LEGACY_TRACE_ID_NAME, TraceIdUtil.traceIdString());
        } else {
            MDC.put(Constants.LEGACY_TRACE_ID_NAME, headerTraceId);
        }

        // "ParentName"
        String headerParentName = traceContentMap.get(Constants.LEGACY_PARENT_SERVICE_NAME);
        if (StringUtils.isEmpty(headerTraceId)) {
            MDC.put(Constants.LEGACY_PARENT_SERVICE_NAME, "this");
        } else {
            MDC.put(Constants.LEGACY_PARENT_SERVICE_NAME, headerParentName);
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant