@Component
public class SerialNumberUtil {
private static volatile SerialNumberUtil instance = null;
private long workerId; //用ip地址最后几个字节标示
private long datacenterId = 0L; //可配置在properties中,启动时加载,此处默认先写成0
private long sequence = 0L;
private long workerIdBits = 8L; //节点ID长度
private long sequenceBits = 12L; //序列号12位
private long workerIdShift = sequenceBits; //机器节点左移12位
private long datacenterIdShift = sequenceBits + workerIdBits; //数据中心节点左移14位
private long sequenceMask = -1L ^ (-1L << sequenceBits); //4095
private long lastTimestamp = -1L;
public static SerialNumberUtil getInstance() {
if (instance == null) {
synchronized (SerialNumberUtil.class) {
if (instance == null) {
instance = new SerialNumberUtil();
}
}
}
return instance;
}
/**
* 生成流水号
*
* @return
*/
来源:CSDN
作者:灰灰的辉
链接:https://blog.csdn.net/qq_39543482/article/details/103237540