实用的ID生成器
/** * ClassName: IDGenerator <br/> * 业务层id生成器 */ public final class IDGenerator { private static final long BASE_TS = 1478016000; private static int serviceUniqueIndex = RandomUtils.nextInt(0, 16); private static final char[] URL_SAFE_ENCODE_TABLE = { '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', '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', '-', '_' }; private static final byte[] DECODE_TABLE = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 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, -1, -1, -1, -1, 63, -1, 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 }; private static final AtomicInteger counter = new AtomicInteger(0); private IDGenerator() { } public static String generate(IDTypeEnum idType) { byte[] barr = new byte[48]; // 5位给type int2bytes(idType.getCode(), barr, 0, 5); // 29位给秒级时间戳 int ts = (int) (System.currentTimeMillis() / 1000 - BASE_TS); int2bytes(ts, barr, 5, 34); //4位给机器,vtd-admin + osp-tdapi,算上预发布一共有12台注册的机器 int2bytes(serviceUniqueIndex, barr, 34, 38); // 10位给自增 int auto = counter.incrementAndGet() % 1024; int2bytes(auto, barr, 38, 48); return bitMaptoString(barr); } public static IDTypeEnum getIDType(String id) { if (id == null || id.length() != 8) { return null; } try { int type = DECODE_TABLE[id.charAt(0)] >> 1; return EnumUtils.valueOfInt(IDTypeEnum.class, type); } catch (Exception e) { return null; } } private static String bitMaptoString(byte[] bitmap) { assert bitmap.length % 6 == 0; int len = bitmap.length / 6; StringBuilder sb = new StringBuilder(); for (int i = 0; i < len; i++) { int baseIdx = i * 6; int idx = bitmap[baseIdx] << 5 | bitmap[baseIdx + 1] << 4 | bitmap[baseIdx + 2] << 3 | bitmap[baseIdx + 3] << 2 | bitmap[baseIdx + 4] << 1 | bitmap[baseIdx + 5]; sb.append(URL_SAFE_ENCODE_TABLE[idx]); } return sb.toString(); } private static void int2bytes(int val, byte[] arr, int st, int ed) { int len = ed - st; int value = val; for (int i = 1; i <= len; i++) { arr[ed - i] = (byte) (value & 0x1); value >>= 1; } } public static long toLongId(String id) { if (id != null && id.length() == 8) { byte[] bytes = id.getBytes(); if (bytes.length == 8) { return ((((long) bytes[0] & 0xff) << 56) | (((long) bytes[1] & 0xff) << 48) | (((long) bytes[2] & 0xff) << 40) | (((long) bytes[3] & 0xff) << 32) | (((long) bytes[4] & 0xff) << 24) | (((long) bytes[5] & 0xff) << 16) | (((long) bytes[6] & 0xff) << 8) | (((long) bytes[7] & 0xff) << 0)); } } return 0; } public static String fromLong(long id) { StringBuilder sb = new StringBuilder(); sb.append((char)(id >> 56 & 0xff)); sb.append((char)(id >> 48 & 0xff)); sb.append((char)(id >> 40 & 0xff)); sb.append((char)(id >> 32 & 0xff)); sb.append((char)(id >> 24 & 0xff)); sb.append((char)(id >> 16 & 0xff)); sb.append((char)(id >> 8 & 0xff)); sb.append((char)(id & 0xff)); return sb.toString(); } public static Set<String> batchGenerate(IDTypeEnum idType, int size) { Set<String> results = new HashSet<>(); if (size <= 0) { return results; } while (results.size() != size) { results.add(IDGenerator.generate(idType)); } return results; } }
调用:
public static void main(String[] args) { String id = IDGenerator.generate(IDTypeEnum.USER_ID); System.out.println(id); }
public enum IDTypeEnum { USER_ID(1), ; private final int code; IDTypeEnum(int code) { this.code = code; } public int getCode() { return code; } }