背景
使用雪花算法生成的主键,二进制表示形式包含4部分,从高位到低位分表为:1bit符号位、41bit时间戳位、10bit工作进程位(也可以区分5bit数据中心、5bit机器ID)以及12bit序列号位。
符号位(1bit)
预留的符号位,恒为零。时间戳位(41bit)
41位的时间戳可以容纳的毫秒数是2的41次方减1,一年所使用的毫秒数是:365 * 24 * 60 * 60 * 1000。通过计算可知:
(Math.pow(2, 41) -1) / (365 * 24 * 60 * 60 * 1000L);
结果约等于69年。Sharding-Sphere的雪花算法的时间纪元从2016年11月1日零点开始,可以使用到2085年,相信能满足绝大部分系统的要求。工作进程位(10bit)
该标志在Java进程内是唯一的,如果是分布式应用部署应保证每个工作进程的id是不同的。该值默认为0,可通过调用静态方法DefaultKeyGenerator.setWorkerId(“xxxx”)设置。序列号位(12bit)
该序列是用来在同一个毫秒内生成不同的ID。如果在这个毫秒内生成的数量超过4096(2的12次方),那么生成器会等待到下个毫秒继续生成。
代码样例
public final class DefaultKeyGenerator {
public static final long EPOCH;
private static final long SEQUENCE_BITS = 12L;
private static final long WORKER_ID_BITS = 10L;
private static final long SEQUENCE_MASK = (1 << SEQUENCE_BITS) - 1;
private static final long WORKER_ID_LEFT_SHIFT_BITS = SEQUENCE_BITS;
private static final long TIMESTAMP_LEFT_SHIFT_BITS = WORKER_ID_LEFT_SHIFT_BITS + WORKER_ID_BITS;
private static final long WORKER_ID_MAX_VALUE = 1L << WORKER_ID_BITS;
private static long workerId;
private static int maxTolerateTimeDifferenceMilliseconds = 10;
static {
//也可指定开始年份
Calendar calendar = Calendar.getInstance();
calendar.set(2016, Calendar.NOVEMBER, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
EPOCH = calendar.getTimeInMillis();
}
private byte sequenceOffset;
private long sequence;
private long lastMilliseconds;
/**
* Set work process id.
*
* @param workerId work process id
*/
public static void setWorkerId(final long workerId) {
Preconditions.checkArgument(workerId >= 0L && workerId < WORKER_ID_MAX_VALUE);
DefaultKeyGenerator.workerId = workerId;
}
/**
* Set max tolerate time difference milliseconds.
*
* @param maxTolerateTimeDifferenceMilliseconds max tolerate time difference milliseconds
*/
public static void setMaxTolerateTimeDifferenceMilliseconds(final int maxTolerateTimeDifferenceMilliseconds) {
DefaultKeyGenerator.maxTolerateTimeDifferenceMilliseconds = maxTolerateTimeDifferenceMilliseconds;
}
/**
* Generate key.
*
* @return key type is @{@link Long}.
*/
public synchronized Number generateKey() {
long currentMilliseconds = System.currentTimeMillis();
if (waitTolerateTimeDifferenceIfNeed(currentMilliseconds)) {
currentMilliseconds = System.currentTimeMillis();
}
if (lastMilliseconds == currentMilliseconds) {
//如果在这个毫秒内生成的数量超过4096(2的12次方),那么生成器会等待到下个毫秒继续生成
if (0L == (sequence = (sequence + 1) & SEQUENCE_MASK)) {
currentMilliseconds = waitUntilNextTime(currentMilliseconds);
}
} else {
//下面方法的sequence只能为0或1,也可以随机一个值如sequence = RandomUtils.nextLong(0L, 60L) * 60L;
vibrateSequenceOffset();
sequence = sequenceOffset;
}
lastMilliseconds = currentMilliseconds;
return ((currentMilliseconds - EPOCH) << TIMESTAMP_LEFT_SHIFT_BITS) | (workerId << WORKER_ID_LEFT_SHIFT_BITS) | sequence;
}
@SneakyThrows
private boolean waitTolerateTimeDifferenceIfNeed(final long currentMilliseconds) {
if (lastMilliseconds <= currentMilliseconds) {
return false;
}
long timeDifferenceMilliseconds = lastMilliseconds - currentMilliseconds;
Preconditions.checkState(timeDifferenceMilliseconds < maxTolerateTimeDifferenceMilliseconds,
"Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds", lastMilliseconds, currentMilliseconds);
Thread.sleep(timeDifferenceMilliseconds);
return true;
}
private long waitUntilNextTime(final long lastTime) {
long result = System.currentTimeMillis();
while (result <= lastTime) {
result = System.currentTimeMillis();
}
return result;
}
private void vibrateSequenceOffset() {
sequenceOffset = (byte) (~sequenceOffset & 1);
}
}
单元测试
public class KeyGeneratorTest {
@Test
public void test() {
//工作进程位10位 取值1-1024 默认0
DefaultKeyGenerator.setWorkerId(1000);
//时钟回拨,最大允许容忍差异毫秒数,超过这个时间将返回异常,默认10ms
DefaultKeyGenerator.setMaxTolerateTimeDifferenceMilliseconds(10);
DefaultKeyGenerator keyGenerator = new DefaultKeyGenerator();
for(int i=0;i<1000;i++){
System.out.println(keyGenerator.generateKey());
}
}
}
优化
为避免需要手动设置workerId,可通过使用IP地址计算得出workId
public class IPSectionKeyGenerator {
private final SnowflakeKeyGenerator snowflakeKeyGenerator = new SnowflakeKeyGenerator();
static {
InetAddress address;
try {
address = InetAddress.getLocalHost();
} catch (UnknownHostException var8) {
throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!");
}
long workerId = 0L;
byte[] ipAddressByteArray = address.getAddress();
int ipLength = ipAddressByteArray.length;
int count = 0;
flag:
switch(ipLength) {
case 4:
while(true) {
if (count >= ipLength) {
break flag;
}
byte byteNum = ipAddressByteArray[count];
workerId += byteNum & 255;
++count;
}
case 16:
while(true) {
if (count >= ipLength) {
break flag;
}
byte byteNum = ipAddressByteArray[count];
workerId += byteNum & 63;
++count;
}
default:
throw new IllegalStateException("Bad LocalHost InetAddress, please check your network!");
}
SnowflakeKeyGenerator.setWorkerId(workerId);
}
public long generateKey() {
return this.snowflakeKeyGenerator.generateKey();
}
}
总结
雪花算法是由Twitter公布的分布式主键生成算法,它能够保证不同进程主键的不重复性,以及相同进程主键的有序性。
在同一个进程中,它首先是通过时间位保证不重复,如果时间相同则是通过序列位保证。
同时由于时间位是单调递增的,且各个服务器如果大体做了时间同步,那么生成的主键在分布式环境可以认为是总体有序的。同时代码对于时钟回拨问题也做了相应的处理。