1.雪花ID概念
雪花算法(Snowflake)是一种用于生成唯一标识符(ID)的分布式算法。它可以生成趋势递增且具有一定时间顺序的64位整数,适用于分布式系统中的唯一ID生成需求。
2.雪花ID结构
0 | 41位时间戳 | 10位工作机器ID | 12位序列号
3.雪花ID的特点
传统自增id容易造成id重复冲突,而UUID是无序的会导致数据散乱,雪花ID就同时就有两者优点,唯一且有序递增
4.雪花ID的使用
4.1 雪花ID工具类
package com.woniuxy.property1002.util;
public class SnowflakeIdWorker {/*** 开始时间:2020-01-01 00:00:00*/private final long beginTs = 1577808000000L;private final long workerIdBits = 10;/*** 2^10 - 1 = 1023*/private final long maxWorkerId = -1L ^ (-1L << workerIdBits);private final long sequenceBits = 12;/*** 2^12 - 1 = 4095*/private final long maxSequence = -1L ^ (-1L << sequenceBits);/*** 时间戳左移22位*/private final long timestampLeftOffset = workerIdBits + sequenceBits;/*** 业务ID左移12位*/private final long workerIdLeftOffset = sequenceBits;/*** 合并了机器ID和数据标示ID,统称业务ID,10位*/private long workerId;/*** 毫秒内序列,12位,2^12 = 4096个数字*/private long sequence = 0L;/*** 上一次生成的ID的时间戳,同一个worker中*/private long lastTimestamp = -1L;/**** 雪花算法工具类的构造方法,需要传入实例id和集群id,在构造函数内首先判断是否超过最大的实例id并且实例id是否小于0,* 同样的去判断数据中心id,如果不符合要求,则抛出参数异常*/private SnowflakeIdWorker(long workerId) {if (workerId > maxWorkerId || workerId < 0) {throw new IllegalArgumentException(String.format("WorkerId必须大于或等于0且小于或等于%d", maxWorkerId));}this.workerId = workerId;}public synchronized long nextId() {long ts = System.currentTimeMillis();if (ts < lastTimestamp) {throw new RuntimeException(String.format("系统时钟回退了%d毫秒", (lastTimestamp - ts)));}// 同一时间内,则计算序列号if (ts == lastTimestamp) {// 序列号溢出if (++sequence > maxSequence) {ts = tilNextMillis(lastTimestamp);sequence = 0L;}} else {// 时间戳改变,重置序列号sequence = 0L;}lastTimestamp = ts;// 左移后,低位补0,进行按位或运算相当于二进制拼接// 本来高位还有个0<<63,0与任何数字按位或都是本身,所以写不写效果一样return (ts - beginTs) << timestampLeftOffset | workerId << workerIdLeftOffset | sequence;}/*** 阻塞到下一个毫秒*/private long tilNextMillis(long lastTimestamp) {long ts = System.currentTimeMillis();while (ts <= lastTimestamp) {ts = System.currentTimeMillis();}return ts;}//单例模式private static final SnowflakeIdWorker instance = new SnowflakeIdWorker(1);public static long newId(){return instance.nextId();}
}
4.2 调用它获取一个雪花ID
import com.woniuxy.property1002.util.SnowflakeIdWorker;@Override
public boolean chargeTenement(Order order) {//生成订单编号long orderno = SnowflakeIdWorker.newId();order.setOrderno(orderno+"");return moneyMapper.chargeTenement(order);
}