1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import lombok.extern.slf4j.Slf4j; import org.slf4j.MDC;
import java.net.InetAddress; import java.text.SimpleDateFormat; import java.util.Date; import java.util.UUID;
@Slf4j public class TraceIdGenerator {
public static String setTraceId(String traceId) { ThreadTraceIdUtil.setTraceId(traceId); MDC.put(Constant.TRACE_ID, traceId); return traceId; }
public static void removeTraceId() { ThreadTraceIdUtil.removeTraceId(); MDC.remove(Constant.TRACE_ID); }
public static String createTraceId() { String traceId = getTraceId(); ThreadTraceIdUtil.setTraceId(traceId); return traceId; }
private static String getTraceId() { StringBuilder result = new StringBuilder(); String ip = "";
try { InetAddress address = InetAddress.getLocalHost(); ip = address.getHostAddress(); } catch (Exception var5) { return result.toString(); }
String[] ipAddressInArray = ip.split("\\."); for (int i = 3; i >= 0; --i) { Integer id = Integer.parseInt(ipAddressInArray[3 - i]); result.append(String.format("%02x", id)); }
result.append(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())); result.append(UUID.randomUUID().toString(), 0, 7); return result.toString(); }
public static String validateTraceId(String traceId) { if (null == traceId) { traceId = createTraceId(); } return traceId; } }
|