最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java多線程使用mdc追蹤日志方式

 更新時(shí)間:2021年09月23日 09:48:14   作者:致林  
這篇文章主要介紹了java多線程使用mdc追蹤日志方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

多線程使用mdc追蹤日志

背景

多線程情況下,子線程的sl4j打印日志缺少traceId等信息,導(dǎo)致定位問(wèn)題不方便

解決方案

  • 打印日志時(shí)添加用戶ID、trackId等信息,缺點(diǎn)是每個(gè)日志都要手動(dòng)添加
  • 使用mdc直接拷貝父線程值

實(shí)現(xiàn)

// 新建線程時(shí):
Map<String, String> mdcContextMap = MDC.getCopyOfContextMap()
// 子線程運(yùn)行時(shí):
if(null != mdcContextMap){
    MDC.setContextMap(mdcContextMap);
}
// 銷(xiāo)毀線程時(shí)
MDC.clear();

參考

import org.slf4j.MDC;
import java.util.Map;
import java.util.concurrent.*;
/**
 * A SLF4J MDC-compatible {@link ThreadPoolExecutor}.
 * <p/>
 * In general, MDC is used to store diagnostic information (e.g. a user's session id) in per-thread variables, to facilitate
 * logging. However, although MDC data is passed to thread children, this doesn't work when threads are reused in a
 * thread pool. This is a drop-in replacement for {@link ThreadPoolExecutor} sets MDC data before each task appropriately.
 * <p/>
 * Created by jlevy.
 * Date: 6/14/13
 */
public class MdcThreadPoolExecutor extends ThreadPoolExecutor {
    final private boolean useFixedContext;
    final private Map<String, Object> fixedContext;
    /**
     * Pool where task threads take MDC from the submitting thread.
     */
    public static MdcThreadPoolExecutor newWithInheritedMdc(int corePoolSize, int maximumPoolSize, long keepAliveTime,
                                                            TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        return new MdcThreadPoolExecutor(null, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }
    /**
     * Pool where task threads take fixed MDC from the thread that creates the pool.
     */
    @SuppressWarnings("unchecked")
    public static MdcThreadPoolExecutor newWithCurrentMdc(int corePoolSize, int maximumPoolSize, long keepAliveTime,
                                                          TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        return new MdcThreadPoolExecutor(MDC.getCopyOfContextMap(), corePoolSize, maximumPoolSize, keepAliveTime, unit,
                workQueue);
    }
    /**
     * Pool where task threads always have a specified, fixed MDC.
     */
    public static MdcThreadPoolExecutor newWithFixedMdc(Map<String, Object> fixedContext, int corePoolSize,
                                                        int maximumPoolSize, long keepAliveTime, TimeUnit unit,
                                                        BlockingQueue<Runnable> workQueue) {
        return new MdcThreadPoolExecutor(fixedContext, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }
    private MdcThreadPoolExecutor(Map<String, Object> fixedContext, int corePoolSize, int maximumPoolSize,
                                  long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
        this.fixedContext = fixedContext;
        useFixedContext = (fixedContext != null);
    }
    @SuppressWarnings("unchecked")
    private Map<String, Object> getContextForTask() {
        return useFixedContext ? fixedContext : MDC.getCopyOfContextMap();
    }
    /**
     * All executions will have MDC injected. {@code ThreadPoolExecutor}'s submission methods ({@code submit()} etc.)
     * all delegate to this.
     */
    @Override
    public void execute(Runnable command) {
        super.execute(wrap(command, getContextForTask()));
    }
    public static Runnable wrap(final Runnable runnable, final Map<String, Object> context) {
        return new Runnable() {
            @Override
            public void run() {
                Map previous = MDC.getCopyOfContextMap();
                if (context == null) {
                    MDC.clear();
                } else {
                    MDC.setContextMap(context);
                }
                try {
                    runnable.run();
                } finally {
                    if (previous == null) {
                        MDC.clear();
                    } else {
                        MDC.setContextMap(previous);
                    }
                }
            }
        };
    }
}

多線程日志追蹤

主要目的是記錄工作中的一些編程思想和細(xì)節(jié),以便后來(lái)查閱。

1.問(wèn)題描述

由于項(xiàng)目中設(shè)計(jì)高并發(fā)內(nèi)容,涉及到一個(gè)線程創(chuàng)建多個(gè)子線程的情況。 那么,如何跟蹤日志,識(shí)別子線程是由哪個(gè)主線程創(chuàng)建的,屬于哪個(gè)request請(qǐng)求。

例如, 在現(xiàn)有項(xiàng)目中,一個(gè)設(shè)備信息上傳的請(qǐng)求(包括基本數(shù)據(jù)和異常數(shù)據(jù)兩種數(shù)據(jù)),然后主線程創(chuàng)建兩個(gè)子線程,來(lái)處理基本數(shù)據(jù)和異常數(shù)據(jù)。

簡(jiǎn)化代碼如下:

public class mainApp {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                //接收到一個(gè)request
                System.out.println("[Thread-"+ Thread.currentThread().getId() +"]開(kāi)始發(fā)起請(qǐng)求");
                String[] data = {"異常數(shù)據(jù)","基本數(shù)據(jù)"};
                //創(chuàng)建子線程1,處理異常數(shù)據(jù)
                MThread mThread1 = new MThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("[Thread-"+ Thread.currentThread().getId() +"]處理了" + data[0]);
                    }
                });
                創(chuàng)建子線程2,處理普通數(shù)據(jù)
                MThread mThread2 = new MThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("[Thread-"+ Thread.currentThread().getId() +"]處理了"  + data[1]);
                    }
                });
                new Thread(mThread1).start();
                new Thread(mThread2).start(); 
            }
        });
        t.start();
    }
}
 
class MThread implements Runnable { 
    private Runnable r; 
    public MThread(Runnable r) {
        this.r = r;
    }
 
    @Override
    public void run() {
        r.run();
    }
}

運(yùn)行結(jié)果如下:

一個(gè)請(qǐng)求有三個(gè)線程,如果有多個(gè)請(qǐng)求,運(yùn)行結(jié)果如下:

從日志中無(wú)法看出他們之間的所屬關(guān)系(判斷不出來(lái)他們是否是處理同一個(gè)request請(qǐng)求的)。如果某一個(gè)線程出現(xiàn)問(wèn)題,我們也很難快速定位是哪個(gè)請(qǐng)求的處理結(jié)果。

2. 代理實(shí)現(xiàn)日志追蹤

因此,我們使用MDC來(lái)在日志中增加traceId(同一個(gè)請(qǐng)求的多個(gè)線程擁有同一個(gè)traceId)。

思路如下:

1. 在request進(jìn)來(lái)的時(shí)候, 利用AOP為每個(gè)request創(chuàng)建一個(gè)traceId(保證每個(gè)request的traceId不同, 同一個(gè)request的traceId相同)

2. 創(chuàng)建子線程的時(shí)候, 將traceId通過(guò)動(dòng)態(tài)代理的方式,傳遞到子線程中

public class mainApp {
    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //AOP 生成一個(gè)traceId
                MDC.put("traceId", UUID.randomUUID().toString().replace("-", ""));
                //接收到一個(gè)request
                System.out.println("[Thread-"+ Thread.currentThread().getId() +"]traceId["+ MDC.get("traceId") +"]開(kāi)始發(fā)起請(qǐng)求");
                String[] data = {"異常數(shù)據(jù)","基本數(shù)據(jù)"};
 
                MThread mThread1 = new MThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("[Thread-"+ Thread.currentThread().getId() +"]traceId["+ MDC.get("traceId") +"]處理了" + data[0]);
                    }
                }, MDC.getCopyOfContextMap());
                MThread mThread2 = new MThread(new Runnable() {
                    @Override
                    public void run() {
                        System.out.println("[Thread-"+ Thread.currentThread().getId() +"]traceId["+ MDC.get("traceId") +"]處理了"  + data[1]);
                    }
                }, MDC.getCopyOfContextMap());
                new Thread(mThread1).start();
                new Thread(mThread2).start(); 
            }
        };
        new Thread(runnable).start();
        new Thread(runnable).start();
    }
}
 
class MThread implements Runnable { 
    private Runnable r; 
    public MThread(Runnable r, Map<String, String> parentThreadMap) {
        LogProxy logProxy = new LogProxy(r, parentThreadMap);
        Runnable rProxy = (Runnable) Proxy.newProxyInstance(r.getClass().getClassLoader(), r.getClass().getInterfaces(), logProxy);
        this.r = rProxy;
    }
 
    @Override
    public void run() {
        r.run();
    }
}
 
//日志代理
class LogProxy implements InvocationHandler {
    private Runnable r;
    private  Map<String, String> parentThreadMap;
    public LogProxy(Runnable r, Map<String, String> parentThreadMap) {
        this.r = r;
        this.parentThreadMap = parentThreadMap;
    }
 
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().equals("run")) {
            MDC.setContextMap(parentThreadMap);
        }
        return method.invoke(r, args);
    }
}

運(yùn)行結(jié)果如下:

兩個(gè)請(qǐng)求, 同一個(gè)請(qǐng)求的traceId相同,不同請(qǐng)求的traceId不同。 完美實(shí)現(xiàn)多線程的日志追蹤。

實(shí)際WEB項(xiàng)目中,只需要在logback日志配置文件中,

logging.pattern.console參數(shù)增[%X{traceId}]即可在LOGGER日志中打印traceId的信息。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot集成mybatis-plus全過(guò)程

    springboot集成mybatis-plus全過(guò)程

    本文詳細(xì)介紹了如何在SpringBoot環(huán)境下集成MyBatis-Plus,包括配置maven依賴、application.yaml文件、創(chuàng)建數(shù)據(jù)庫(kù)和Java實(shí)體類、Mapper層、Service層和Controller層的設(shè)置,同時(shí),還涵蓋了時(shí)間自動(dòng)填充、分頁(yè)查詢、多對(duì)一和一對(duì)多的數(shù)據(jù)庫(kù)映射關(guān)系設(shè)置
    2024-09-09
  • 淺談web服務(wù)器項(xiàng)目中request請(qǐng)求和response的相關(guān)響應(yīng)處理

    淺談web服務(wù)器項(xiàng)目中request請(qǐng)求和response的相關(guān)響應(yīng)處理

    這篇文章主要介紹了淺談web服務(wù)器項(xiàng)目中request請(qǐng)求和response的相關(guān)響應(yīng)處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Java多線程實(shí)現(xiàn)Runnable方式

    Java多線程實(shí)現(xiàn)Runnable方式

    這篇文章主要為大家詳細(xì)介紹了Java多線程如何實(shí)現(xiàn)Runnable方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • 解決SpringBoot連接SqlServer出現(xiàn)的問(wèn)題

    解決SpringBoot連接SqlServer出現(xiàn)的問(wèn)題

    在嘗試通過(guò)SSL與SQL?Server建立安全連接時(shí),如果遇到“PKIX?path?building?failed”錯(cuò)誤,可能是因?yàn)槲茨苷_配置或信任服務(wù)器證書(shū),當(dāng)"Encrypt"屬性設(shè)置為"true"且"trustServerCertificate"屬性設(shè)置為"false"時(shí),要求驅(qū)動(dòng)程序使用安全套接字層(SSL)加密與SQL?Server建立連接
    2024-10-10
  • Java的動(dòng)態(tài)代理模式之Cglib代理詳解

    Java的動(dòng)態(tài)代理模式之Cglib代理詳解

    這篇文章主要介紹了Java的動(dòng)態(tài)代理模式之Cglib代理詳解,Cglib代理也叫作?子類代理,它是在內(nèi)存中構(gòu)建一個(gè)子類對(duì)象從而實(shí)現(xiàn)對(duì)目標(biāo)對(duì)象功能擴(kuò)展,?有些書(shū)也將Cglib代理歸屬到動(dòng)態(tài)代理,需要的朋友可以參考下
    2023-11-11
  • springboot簡(jiǎn)單集成Security配置的教程

    springboot簡(jiǎn)單集成Security配置的教程

    這篇文章主要介紹了springboot簡(jiǎn)單集成Security配置的教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java?設(shè)計(jì)模式中的命令模式詳情

    Java?設(shè)計(jì)模式中的命令模式詳情

    這篇文章主要介紹了Java?設(shè)計(jì)模式中的命令模式詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-07-07
  • servlet3新特性_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    servlet3新特性_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要為大家詳細(xì)介紹了servlet3新特性的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • java項(xiàng)目啟動(dòng)失敗的問(wèn)題及解決

    java項(xiàng)目啟動(dòng)失敗的問(wèn)題及解決

    這篇文章主要介紹了java項(xiàng)目啟動(dòng)失敗的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Java使用迭代器Iterator遍歷集合

    Java使用迭代器Iterator遍歷集合

    Iterator對(duì)象稱為迭代器(設(shè)計(jì)模式的一種),主要用于遍歷 Collection 集合中的元素。本文就來(lái)和大家詳細(xì)聊聊Java如何使用迭代器Iterator實(shí)現(xiàn)遍歷集合,感興趣的可以跟隨小編一起學(xué)習(xí)一下
    2022-12-12

最新評(píng)論

雅江县| 宝山区| 潜山县| 河池市| 贵阳市| 英德市| 虞城县| 大田县| 盱眙县| 澜沧| 登封市| 湛江市| 句容市| 阳原县| 民乐县| 临漳县| 昭苏县| 永寿县| 兴宁市| 会昌县| 会宁县| 广河县| 安多县| 青冈县| 冕宁县| 太原市| 浪卡子县| 若尔盖县| 江阴市| 东兰县| 新营市| 邵阳市| 延长县| 榆树市| 衡阳县| 丹凤县| 大庆市| 竹溪县| 腾冲县| 清原| 沧州市|