modify trade id
This commit is contained in:
parent
c63f3104b7
commit
2131194de6
@ -4,6 +4,8 @@ import com.google.common.base.Strings;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author haiyangjin
|
||||
* @date 2023/7/23
|
||||
@ -14,13 +16,12 @@ public class TraceUtils {
|
||||
/**
|
||||
* 多设置一个key = TraceId, value为traceId的变量到MDC. 以兼容目前的logback-spring.xml的配置
|
||||
*/
|
||||
public static final String TRACE_ID_IN_MDC = "TraceId";
|
||||
public static final String TRACE_ID_IN_MDC = "traceId";
|
||||
|
||||
public String getOrCreateTraceId() {
|
||||
String res = MDC.get(TRACE_ID);
|
||||
String res = MDC.get(TRACE_ID_IN_MDC);
|
||||
if (Strings.isNullOrEmpty(res)) {
|
||||
// TODO uuid
|
||||
// res = UUIDBuilder.generateShortUuid();
|
||||
res = UUIDBuilder.generateShortUuid();
|
||||
MDC.put(TRACE_ID, res);
|
||||
MDC.put(TRACE_ID_IN_MDC, res);
|
||||
}
|
||||
|
||||
@ -0,0 +1,56 @@
|
||||
package cn.axzo.framework.rocketmq.utils;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author haiyangjin
|
||||
* @date 2023/9/1
|
||||
*/
|
||||
public class UUIDBuilder {
|
||||
private static final String SPLITOR = "-";
|
||||
private static final String BLANK = "";
|
||||
|
||||
public static String BASE_STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
|
||||
public static String[] BASE_CHARS = new String[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
|
||||
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7",
|
||||
"8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
|
||||
"T", "U", "V", "W", "X", "Y", "Z"};
|
||||
|
||||
// UUID 32
|
||||
public static String generateLongUuid() {
|
||||
return generateLongUuid(true);
|
||||
}
|
||||
|
||||
// UUID 32
|
||||
public static String generateLongUuid(boolean isUpperCase) {
|
||||
String uuid = UUID.randomUUID().toString().replace(SPLITOR, BLANK);
|
||||
if (isUpperCase) {
|
||||
return uuid.toUpperCase();
|
||||
}
|
||||
return uuid.toLowerCase();
|
||||
}
|
||||
|
||||
// UUID 8
|
||||
public static String generateShortUuid() {
|
||||
StringBuilder shortBuffer = new StringBuilder(8);
|
||||
String uuid = UUID.randomUUID().toString().replace(SPLITOR, BLANK);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
String str = uuid.substring(i * 4, i * 4 + 4);
|
||||
int x = Integer.parseInt(str, 16);
|
||||
shortBuffer.append(BASE_CHARS[x % 0x3E]);
|
||||
}
|
||||
return shortBuffer.toString();
|
||||
}
|
||||
|
||||
public static String getRandomString(int length) {
|
||||
Random random = new Random();
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
int number = random.nextInt(BASE_STRING.length());
|
||||
sb.append(BASE_STRING.charAt(number));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user