基本思路:
设计模式:单例模式
是否加锁:是 synchronized
获取最后一次生成的时间戳值T0
限定初始时间为2023-08-01 00:00:00,获取当前时间时间戳T1,T1与初始时间的毫秒差值T2,转为16进制,转为字符串为r1,获取该字符串的长度L1
获取L2 (length - L1) ,获取L2位数字的16进制自增数值范围,取最大值max
现数据库批量导入数据速度为 n条/ms
平均步长为max/n,(0~平均步长)的平均数为max/n/2,假设使用平均步长最为随机步长范围,最终的值与max相差较远,大约后一半的数字没有被使用
将平均步长*2-平均步长*容错因子(0.1)的值作为我们随机步长的范围 容错因子:减小溢出概率
随机步长step = max/n*2 - max/n*0.1
获取T1
如果T1 == T0,序列值seqNum = seqNum + step (转为16进制),若seqNum > max,该线程暂停1毫秒后刷新r1
如果T1 > T0,序列值seqNum = 0 + step
设置T0
代码实现如下:
/*** 生成短id* @author mayu*/
public class ShortIdWorker {/*** 初始时间限定为2023-08-01 00:00:00*/private final static long START_STAMP = 1690819200000L;/*** 容错因子*/private final static int FAULT_TOLERANCE_FACTOR = 10;/*** 默认长度*/private final static int DEFAULT_ID_LENGTH = 12;/*** 数据库每毫秒可保存的数据,结合列的数量取值,建议实测后更改*/private final static int DEFAULT_TRANSFER_SPEED_PER_MILLISECOND = 50;private final int length;private final int transferSpeedPerMillisecond;/*** 上次运行时间*/private long lastStamp = -1L;/*** 增长序列*/private int seqNum;private static ShortIdWorker instance;/*** 单例模式*/public static ShortIdWorker getInstance() {if (null == instance) {instance = new ShortIdWorker();}return instance;}public static ShortIdWorker newInstance(int length, int transferSpeedPerMillisecond) {return new ShortIdWorker(length, transferSpeedPerMillisecond);}/*** 默认使用12位id,数据库每毫秒新增数据为50条*/private ShortIdWorker() {this(DEFAULT_ID_LENGTH, DEFAULT_TRANSFER_SPEED_PER_MILLISECOND);}private ShortIdWorker(int length, int transferSpeedPerMillisecond) {this.length = length;this.transferSpeedPerMillisecond = transferSpeedPerMillisecond;}/*** @return 生成后的id* <p>* 例:757b12c001d3* 共length位id,前x位为时间戳差值的16进制,后y位为不固定步长的自增序列*/public synchronized String nextId() {long now = now();// 获取16进制时间戳前缀String stampPrefix = getStampStr(now);// 获取第二段增长序列的长度l2int l2 = this.length - stampPrefix.length();// 获取l2位16进制的最大值int max = IntStream.range(0, l2).map(i -> 16).reduce(1, (a, b) -> a * b) - 1;// 获取增长的平均步长averageStepLengthint averageStepLength = max / this.transferSpeedPerMillisecond;// 取步长范围// averageStepLength的平均值是averageStepLength/2,累加的情况下会有后一半的空间浪费问题,故取值为averageStepLength*2,平均值为averageStepLength// 取随机数的结果不可控,上行中列举的只是近似值,为防止多次溢出影响程序执行时间,再减去容错因子,减小溢出概率(容错因子建议在本地系统实测后更改)int randomStepLengthMax = (averageStepLength << 1) - (averageStepLength / FAULT_TOLERANCE_FACTOR);// 在步长范围内获取随机步长int randomStepLength = new Random().nextInt(randomStepLengthMax) + 1;// 当上次运行时间小于当前时间或第一次运行时,增长序列赋值为随机步长,设置最后运行时间if (this.lastStamp < now || this.lastStamp == -1L) {this.seqNum = randomStepLength;this.lastStamp = now;// 当上次运行时间与当前运行时间处于同一毫秒时} else if (this.lastStamp == now) {// 增长序列以随机步长为步长递增this.seqNum += randomStepLength;// 当增长序列大于最大值时if (this.seqNum > max) {// 程序暂停一毫秒LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(1));// 重新获取前缀,增长序列重新开始this.seqNum = randomStepLength;Long newNow = now();this.lastStamp = newNow;stampPrefix = getStampStr(newNow);}} else {// 时钟回拨,报错throw new IllegalStateException("Clock moved backwards. Reject to generate id");}// 将增长序列转为16进制与时间戳拼接return stampPrefix + String.format("%0" + l2 + "X", new BigInteger(String.valueOf(this.seqNum), 10));}private String hex10To16(String str) {return String.format("%X", new BigInteger(str, 10));}private long now() {return System.currentTimeMillis();}/*** 获取传入时间与开始时间的间隔毫秒数,将结果转为16进制* @param now 时间戳* @return*/private String getStampStr(Long now) {return hex10To16(String.valueOf(now - START_STAMP));}
8位16进制可使用到4201年-03-20 07:32:15,后续时间戳所占位数自动变为9位,id总长度不变,不用担心id用尽的问题。
代码中关于时间赋值的代码请谨慎改动,顺序颠倒会产生bug。