目录
The solution is about from Single application single thread to single app multi thread to distributed system. Here is the basic Skeleton.
1. Single-thread
Solution
ThreadLocal: The ThreadLocal construct allows us to store data that will be accessible only by a specific thread.
Examples
1. Spring RequestContextHolder
2. Slf4j MDC (Mapped Diagnositc Contexts)
package org.slf4j;
public class MDC {
//Put a context value as identified by key
//into the current thread's context map.
public static void put(String key, String val);
//Get the context identified by the key parameter.
public static String get(String key);
//Remove the context identified by the key parameter.
public static void remove(String key);
//Clear all entries in the MDC.
public static void clear();
}
Example : Basic MDC usage ( logback-examples/src/main/java/chapters/mdc/SimpleMDC.java)
package chapters.mdc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.log