<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot+MDC實(shí)現(xiàn)全鏈路調(diào)用日志跟蹤,這才叫優(yōu)雅!

          共 9577字,需瀏覽 20分鐘

           ·

          2022-01-14 07:31

          點(diǎn)擊關(guān)注公眾號(hào),Java干貨及時(shí)送達(dá)??

          文章來(lái)源: https://sourl.cn/WbZ9W8

          之前有一篇文章簡(jiǎn)單的介紹過(guò)MDC,這次結(jié)合具體的案例、生產(chǎn)中的具體問(wèn)題深入了解一下MDC。


          MDC 介紹




          1、簡(jiǎn)介:

          MDC(Mapped Diagnostic Context,映射調(diào)試上下文)是?log4j?、logbacklog4j2?提供的一種方便在多線程條件下記錄日志的功能。MDC?可以看成是一個(gè)與當(dāng)前線程綁定的哈希表,可以往其中添加鍵值對(duì)。MDC 中包含的內(nèi)容可以被同一線程中執(zhí)行的代碼所訪問(wèn)。

          當(dāng)前線程的子線程會(huì)繼承其父線程中的 MDC 的內(nèi)容。當(dāng)需要記錄日志時(shí),只需要從 MDC 中獲取所需的信息即可。MDC 的內(nèi)容則由程序在適當(dāng)?shù)臅r(shí)候保存進(jìn)去。對(duì)于一個(gè) Web 應(yīng)用來(lái)說(shuō),通常是在請(qǐng)求被處理的最開(kāi)始保存這些數(shù)據(jù)。

          2、API說(shuō)明:

          • clear()?:移除所有MDC
          • get (String key)?:獲取當(dāng)前線程MDC中指定key的值
          • getContext()?:獲取當(dāng)前線程MDC的MDC
          • put(String key, Object o)?:往當(dāng)前線程的MDC中存入指定的鍵值對(duì)
          • remove(String key)?:刪除當(dāng)前線程MDC中指定的鍵值對(duì)

          3、優(yōu)點(diǎn):

          代碼簡(jiǎn)潔,日志風(fēng)格統(tǒng)一,不需要在log打印中手動(dòng)拼寫(xiě)traceId,即LOGGER.info("traceId:{} ", traceId)。

          MDC 使用




          1、添加攔截器

          public?class?LogInterceptor?implements?HandlerInterceptor?{
          ????@Override
          ????public?boolean?preHandle(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler)?throws?Exception?{
          ????????//如果有上層調(diào)用就用上層的ID
          ????????String?traceId?=?request.getHeader(Constants.TRACE_ID);
          ????????if?(traceId?==?null)?{
          ????????????traceId?=?TraceIdUtil.getTraceId();
          ????????}

          ????????MDC.put(Constants.TRACE_ID,?traceId);
          ????????return?true;
          ????}

          ????@Override
          ????public?void?postHandle(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler,?ModelAndView?modelAndView)
          ????????????throws?Exception?
          {
          ????}

          ????@Override
          ????public?void?afterCompletion(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler,?Exception?ex)
          ????????????throws?Exception?
          {
          ????????//調(diào)用結(jié)束后刪除
          ????????MDC.remove(Constants.TRACE_ID);
          ????}
          }

          2、修改日志格式

          <property?name="pattern">[TRACEID:%X{traceId}]?%d{HH:mm:ss.SSS}?%-5level?%class{-1}.%M()/%L?-?%msg%xEx%nproperty>

          重點(diǎn)是%X{traceId},traceId和MDC中的鍵名稱(chēng)一致

          簡(jiǎn)單使用就這么容易,但是在有些情況下traceId將獲取不到。


          MDC 存在的問(wèn)題




          • 子線程中打印日志丟失traceId
          • HTTP調(diào)用丟失traceId

          丟失traceId的情況,來(lái)一個(gè)再解決一個(gè),絕不提前優(yōu)化。


          解決 MDC 存在的問(wèn)題




          子線程日志打印丟失traceId

          子線程在打印日志的過(guò)程中traceId將丟失,解決方式為重寫(xiě)線程池,對(duì)于直接new創(chuàng)建線程的情況不考略【實(shí)際應(yīng)用中應(yīng)該避免這種用法】,重寫(xiě)線程池?zé)o非是對(duì)任務(wù)進(jìn)行一次封裝。

          線程池封裝類(lèi):ThreadPoolExecutorMdcWrapper.java

          public?class?ThreadPoolExecutorMdcWrapper?extends?ThreadPoolExecutor?{
          ????public?ThreadPoolExecutorMdcWrapper(int?corePoolSize,?int?maximumPoolSize,?long?keepAliveTime,?TimeUnit?unit,
          ????????????????????????????????????????BlockingQueue?workQueue)
          ?
          {
          ????????super(corePoolSize,?maximumPoolSize,?keepAliveTime,?unit,?workQueue);
          ????}

          ????public?ThreadPoolExecutorMdcWrapper(int?corePoolSize,?int?maximumPoolSize,?long?keepAliveTime,?TimeUnit?unit,
          ????????????????????????????????????????BlockingQueue?workQueue,?ThreadFactory?threadFactory)
          ?
          {
          ????????super(corePoolSize,?maximumPoolSize,?keepAliveTime,?unit,?workQueue,?threadFactory);
          ????}

          ????public?ThreadPoolExecutorMdcWrapper(int?corePoolSize,?int?maximumPoolSize,?long?keepAliveTime,?TimeUnit?unit,
          ????????????????????????????????????????BlockingQueue?workQueue,?RejectedExecutionHandler?handler)
          ?
          {
          ????????super(corePoolSize,?maximumPoolSize,?keepAliveTime,?unit,?workQueue,?handler);
          ????}

          ????public?ThreadPoolExecutorMdcWrapper(int?corePoolSize,?int?maximumPoolSize,?long?keepAliveTime,?TimeUnit?unit,
          ????????????????????????????????????????BlockingQueue?workQueue,?ThreadFactory?threadFactory,
          ????????????????????????????????????????RejectedExecutionHandler?handler)
          ?
          {
          ????????super(corePoolSize,?maximumPoolSize,?keepAliveTime,?unit,?workQueue,?threadFactory,?handler);
          ????}

          ????@Override
          ????public?void?execute(Runnable?task)?{
          ????????super.execute(ThreadMdcUtil.wrap(task,?MDC.getCopyOfContextMap()));
          ????}

          ????@Override
          ????public??Future?submit(Runnable?task,?T?result)?{
          ????????return?super.submit(ThreadMdcUtil.wrap(task,?MDC.getCopyOfContextMap()),?result);
          ????}

          ????@Override
          ????public??Future?submit(Callable?task)?{
          ????????return?super.submit(ThreadMdcUtil.wrap(task,?MDC.getCopyOfContextMap()));
          ????}

          ????@Override
          ????public?Future?submit(Runnable?task)?{
          ????????return?super.submit(ThreadMdcUtil.wrap(task,?MDC.getCopyOfContextMap()));
          ????}
          }

          說(shuō)明:

          • 繼承ThreadPoolExecutor類(lèi),重新執(zhí)行任務(wù)的方法
          • 通過(guò)ThreadMdcUtil對(duì)任務(wù)進(jìn)行一次包裝

          線程traceId封裝工具類(lèi):ThreadMdcUtil.java

          public?class?ThreadMdcUtil?{
          ????public?static?void?setTraceIdIfAbsent()?{
          ????????if?(MDC.get(Constants.TRACE_ID)?==?null)?{
          ????????????MDC.put(Constants.TRACE_ID,?TraceIdUtil.getTraceId());
          ????????}
          ????}

          ????public?static??Callable?wrap(final?Callable?callable,?final?Map?context)?{
          ????????return?()?->?{
          ????????????if?(context?==?null)?{
          ????????????????MDC.clear();
          ????????????}?else?{
          ????????????????MDC.setContextMap(context);
          ????????????}
          ????????????setTraceIdIfAbsent();
          ????????????try?{
          ????????????????return?callable.call();
          ????????????}?finally?{
          ????????????????MDC.clear();
          ????????????}
          ????????};
          ????}

          ????public?static?Runnable?wrap(final?Runnable?runnable,?final?Map?context)?{
          ????????return?()?->?{
          ????????????if?(context?==?null)?{
          ????????????????MDC.clear();
          ????????????}?else?{
          ????????????????MDC.setContextMap(context);
          ????????????}
          ????????????setTraceIdIfAbsent();
          ????????????try?{
          ????????????????runnable.run();
          ????????????}?finally?{
          ????????????????MDC.clear();
          ????????????}
          ????????};
          ????}
          }

          說(shuō)明【以封裝Runnable為例】:

          • 判斷當(dāng)前線程對(duì)應(yīng)MDC的Map是否存在,存在則設(shè)置
          • 設(shè)置MDC中的traceId值,不存在則新生成,針對(duì)不是子線程的情況,如果是子線程,MDC中traceId不為null
          • 執(zhí)行run方法

          代碼等同于以下寫(xiě)法,會(huì)更直觀

          public?static?Runnable?wrap(final?Runnable?runnable,?final?Map?context)?{
          ????????return?new?Runnable()?{
          ????????????@Override
          ????????????public?void?run()?{
          ????????????????if?(context?==?null)?{
          ????????????????????MDC.clear();
          ????????????????}?else?{
          ????????????????????MDC.setContextMap(context);
          ????????????????}
          ????????????????setTraceIdIfAbsent();
          ????????????????try?{
          ????????????????????runnable.run();
          ????????????????}?finally?{
          ????????????????????MDC.clear();
          ????????????????}
          ????????????}
          ????????};
          ????}

          重新返回的是包裝后的Runnable,在該任務(wù)執(zhí)行之前【runnable.run()】先將主線程的Map設(shè)置到當(dāng)前線程中【 即MDC.setContextMap(context)】,這樣子線程和主線程MDC對(duì)應(yīng)的Map就是一樣的了。

          HTTP調(diào)用丟失traceId

          在使用HTTP調(diào)用第三方服務(wù)接口時(shí)traceId將丟失,需要對(duì)HTTP調(diào)用工具進(jìn)行改造,在發(fā)送時(shí)在request header中添加traceId,在下層被調(diào)用方添加攔截器獲取header中的traceId添加到MDC中。

          HTTP調(diào)用有多種方式,比較常見(jiàn)的有HttpClientOKHttp、RestTemplate,所以只給出這幾種HTTP調(diào)用的解決方式。

          1、HttpClient:

          實(shí)現(xiàn)HttpClient攔截器:

          public?class?HttpClientTraceIdInterceptor?implements?HttpRequestInterceptor?{
          ????@Override
          ????public?void?process(HttpRequest?httpRequest,?HttpContext?httpContext)?throws?HttpException,?IOException?{
          ????????String?traceId?=?MDC.get(Constants.TRACE_ID);
          ????????//當(dāng)前線程調(diào)用中有traceId,則將該traceId進(jìn)行透?jìng)?/span>
          ????????if?(traceId?!=?null)?{
          ????????????//添加請(qǐng)求體
          ????????????httpRequest.addHeader(Constants.TRACE_ID,?traceId);
          ????????}
          ????}
          }

          實(shí)現(xiàn)HttpRequestInterceptor接口并重寫(xiě)process方法

          如果調(diào)用線程中含有traceId,則需要將獲取到的traceId通過(guò)request中的header向下透?jìng)飨氯ァ?/span>

          HttpClient添加攔截器:

          private?static?CloseableHttpClient?httpClient?=?HttpClientBuilder.create()
          ????????????.addInterceptorFirst(new?HttpClientTraceIdInterceptor())
          ????????????.build();

          通過(guò)addInterceptorFirst方法為HttpClient添加攔截器。

          2、OKHttp:

          實(shí)現(xiàn)OKHttp攔截器:

          public?class?OkHttpTraceIdInterceptor?implements?Interceptor?{
          ????@Override
          ????public?Response?intercept(Chain?chain)?throws?IOException?{
          ????????String?traceId?=?MDC.get(Constants.TRACE_ID);
          ????????Request?request?=?null;
          ????????if?(traceId?!=?null)?{
          ????????????//添加請(qǐng)求體
          ????????????request?=?chain.request().newBuilder().addHeader(Constants.TRACE_ID,?traceId).build();
          ????????}
          ????????Response?originResponse?=?chain.proceed(request);

          ????????return?originResponse;
          ????}
          }

          實(shí)現(xiàn)Interceptor攔截器,重寫(xiě)interceptor方法,實(shí)現(xiàn)邏輯和HttpClient差不多,如果能夠獲取到當(dāng)前線程的traceId則向下透?jìng)鳌?/span>

          為OkHttp添加攔截器:

          ??private?static?OkHttpClient?client?=?new?OkHttpClient.Builder()
          ????????????.addNetworkInterceptor(new?OkHttpTraceIdInterceptor())
          ????????????.build();

          調(diào)用addNetworkInterceptor方法添加攔截器。

          3、RestTemplate:

          實(shí)現(xiàn)RestTemplate攔截器:

          public?class?RestTemplateTraceIdInterceptor?implements?ClientHttpRequestInterceptor?{
          ????@Override
          ????public?ClientHttpResponse?intercept(HttpRequest?httpRequest,?byte[]?bytes,?ClientHttpRequestExecution?clientHttpRequestExecution)?throws?IOException?{
          ????????String?traceId?=?MDC.get(Constants.TRACE_ID);
          ????????if?(traceId?!=?null)?{
          ????????????httpRequest.getHeaders().add(Constants.TRACE_ID,?traceId);
          ????????}

          ????????return?clientHttpRequestExecution.execute(httpRequest,?bytes);
          ????}
          }

          實(shí)現(xiàn)ClientHttpRequestInterceptor接口,并重寫(xiě)intercept方法,其余邏輯都是一樣的不重復(fù)說(shuō)明。

          為RestTemplate添加攔截器:

          restTemplate.setInterceptors(Arrays.asList(new?RestTemplateTraceIdInterceptor()));

          調(diào)用setInterceptors方法添加攔截器。

          4、第三方服務(wù)攔截器:

          HTTP調(diào)用第三方服務(wù)接口全流程traceId需要第三方服務(wù)配合,第三方服務(wù)需要添加攔截器拿到request header中的traceId并添加到MDC中。

          public?class?LogInterceptor?implements?HandlerInterceptor?{
          ????@Override
          ????public?boolean?preHandle(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler)?throws?Exception?{
          ????????//如果有上層調(diào)用就用上層的ID
          ????????String?traceId?=?request.getHeader(Constants.TRACE_ID);
          ????????if?(traceId?==?null)?{
          ????????????traceId?=?TraceIdUtils.getTraceId();
          ????????}
          ????????
          ????????MDC.put("traceId",?traceId);
          ????????return?true;
          ????}

          ????@Override
          ????public?void?postHandle(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler,?ModelAndView?modelAndView)
          ????????????throws?Exception?
          {
          ????}

          ????@Override
          ????public?void?afterCompletion(HttpServletRequest?request,?HttpServletResponse?response,?Object?handler,?Exception?ex)
          ????????????throws?Exception?
          {
          ????????MDC.remove(Constants.TRACE_ID);
          ????}
          }

          說(shuō)明:

          • 先從request header中獲取traceId
          • 從request header中獲取不到traceId則說(shuō)明不是第三方調(diào)用,直接生成一個(gè)新的traceId
          • 將生成的traceId存入MDC中

          除了需要添加攔截器之外,還需要在日志格式中添加traceId的打印,如下:

          ?<property?name="pattern">[TRACEID:%X{traceId}]?%d{HH:mm:ss.SSS}?%-5level?%class{-1}.%M()/%L?-?%msg%xEx%nproperty>

          注意:需要添加%X{traceId}

          1.?SpringBoot天生自帶Buff工具類(lèi)你都用過(guò)哪些?

          2.?Spring Boot 如何解決多個(gè)定時(shí)任務(wù)阻塞問(wèn)題?

          3.?西安一碼通兩次崩潰,技術(shù)原因是什么?

          4.?支付寶架構(gòu)師眼中的高并發(fā)架構(gòu),真是絕了!

          最近面試BAT,整理一份面試資料Java面試BATJ通關(guān)手冊(cè),覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫(kù)、數(shù)據(jù)結(jié)構(gòu)等等。

          獲取方式:點(diǎn)“在看”,關(guān)注公眾號(hào)并回復(fù)?Java?領(lǐng)取,更多內(nèi)容陸續(xù)奉上。

          PS:因公眾號(hào)平臺(tái)更改了推送規(guī)則,如果不想錯(cuò)過(guò)內(nèi)容,記得讀完點(diǎn)一下在看,加個(gè)星標(biāo),這樣每次新文章推送才會(huì)第一時(shí)間出現(xiàn)在你的訂閱列表里。

          點(diǎn)“在看”支持一下呀,謝謝啦??!

          瀏覽 39
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲 变态 欧美 另类 精品 | 青青草娱乐在线 | 伊人婷婷色五月色婷婷区 | 欧美日韩视频在线 | 成人污污污网站 |