springboot+雪花算法⽣成订单号(适⽤于分布式⾼并发环
境)
package utils;
/**
* @deprecated 类功能:使⽤雪花算法⽣成订单号
* xiaohuya
*/
public class SnowFlake {
//起始的时间戳
private final static long START_STAMP = 1480166465631L;
//每⼀部分占⽤的位数
private final static long SEQUENCE_BIT = 12; //序列号占⽤的位数
private final static long MACHINE_BIT = 5; //机器标识占⽤的位数
private final static long DATA_CENTER_BIT = 5;//数据中⼼占⽤的位数
//每⼀部分的最⼤值
private final static long MAX_DATA_CENTER_NUM = -1L ^ (-1L << DATA_CENTER_BIT);
private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
//每⼀部分向左的位移
private final static long MACHINE_LEFT = SEQUENCE_BIT;
private final static long DATA_CENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
private final static long TIMESTAMP_LEFT = DATA_CENTER_LEFT + DATA_CENTER_BIT;
private long dataCenterId; //数据中⼼
private long machineId; //机器标识
private long sequence = 0L; //序列号
private long lastStamp = -1L;//上⼀次时间戳
public SnowFlake(long dataCenterId, long machineId) {
if (dataCenterId > MAX_DATA_CENTER_NUM || dataCenterId < 0) {
throw new IllegalArgumentException("dataCenterId can't be greaterthan MAX_DATA_CENTER_NUM or less than 0");
}
if (machineId > MAX_MACHINE_NUM || machineId < 0) {
throw new IllegalArgumentException("machineId can't be greater thanMAX_MACHINE_NUM or less than 0");
}
this.dataCenterId = dataCenterId;
this.machineId = machineId;
}
/**
* ⽣成订单前缀,26个字母随机俩位
* @return
*/
private String createPrefix() {
String randomPrefix = "";
for (int i = 0; i < 2; i++) {
char c = (char) (Math.random() * 26 + 'A');
randomPrefix += c;
}
System.out.println("randomPrefix======" + randomPrefix);
return randomPrefix;
}
//产⽣订单号
public synchronized String createOrderNo() {
long currStamp = getNewStamp();
if (currStamp < lastStamp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
}
if (currStamp == lastStamp) {
//相同毫秒内,序列号⾃增
sequence = (sequence + 1) & MAX_SEQUENCE;
sequence = (sequence + 1) & MAX_SEQUENCE;
//同⼀毫秒的序列数已经达到最⼤
if (sequence == 0L) {
currStamp = getNextMill();
}
} else {
//不同毫秒内,序列号置为0
sequence = 0L;
}
lastStamp = currStamp;
return "" + ((currStamp - START_STAMP) << TIMESTAMP_LEFT //时间戳部分
| dataCenterId << DATA_CENTER_LEFT //数据中⼼部分
| machineId << MACHINE_LEFT //机器标识部分
| sequence); //序列号部分
/*return createPrefix() + ((currStamp - START_STAMP) << TIMESTAMP_LEFT //时间戳部分 | dataCenterId << DATA_CENTER_LEFT //数据中⼼部分
| machineId << MACHINE_LEFT //机器标识部分
| sequence);*/
}
private long getNextMill() {
long mill = getNewStamp();
while (mill <= lastStamp) {
mill = getNewStamp();
}
return mill;
}
/**
* ⽣成新的时间戳
springboot推荐算法* @return
*/
private long getNewStamp() {
return System.currentTimeMillis();
}
/**
* 测试,分布式下数据中⼼的id和服务器id需要准确填写,单机下随意
* @param args
*/
public static void main(String[] args) {
System.out.println(new SnowFlake(0, 0).createOrderNo());
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论