在分布式系統中,會有生成全局唯一ID的需求,已有的解決方案中,UUID不便於索引,全局ID生成器自身會有性能瓶頸,雪花算法則很好的解決了這兩個問題
雪花算法生成的是一個64位的LONG值,主要由以下四個部分組成
1、1位的保留位
2、41位的時間戳(可以保證69年不重復)
3、10位的機器ID(可供1024台機器使用)
4、12位的自增序列號(從1-4096中循環使用)
基於雪花算法的設計原則(時間戳+機器號+自增號)可以很方便的設計出符合自己需要的ID生成器,需要注意的是,雪花算法依賴於機器自身的時間,如果機器自身的時間存在問題,則可能出現重復ID,這個問題可以在生成器初始化時,連接時間服務器來解決。
GITHUB上的雪花算法基於scala實現:
https://github.com/twitter/snowflake
基於Java的實現可參考,這個實現與twitter的實現略有不同,使用了保留位,可以生成139年不重復的ID:
https://www.callicoder.com/distributed-unique-id-sequence-number-generator/
import java.net.NetworkInterface;
import java.security.SecureRandom;
import java.time.Instant;
import java.util.Enumeration;
/**
* Distributed Sequence Generator.
* Inspired by Twitter snowflake: https://github.com/twitter/snowflake/tree/snowflake-2010
*
* This class should be used as a Singleton.
* Make sure that you create and reuse a Single instance of SequenceGenerator per node in your distributed system cluster.
*/
public class SequenceGenerator {
private static final int TOTAL_BITS = 64;
private static final int EPOCH_BITS = 42;
private static final int NODE_ID_BITS = 10;
private static final int SEQUENCE_BITS = 12;
private static final int maxNodeId = (int)(Math.pow(2, NODE_ID_BITS) - 1);
private static final int maxSequence = (int)(Math.pow(2, SEQUENCE_BITS) - 1);
// Custom Epoch (January 1, 2015 Midnight UTC = 2015-01-01T00:00:00Z)
private static final long CUSTOM_EPOCH = 1420070400000L;
private final int nodeId;
private volatile long lastTimestamp = -1L;
private volatile long sequence = 0L;
// Create SequenceGenerator with a nodeId
public SequenceGenerator(int nodeId) {
if(nodeId < 0 || nodeId > maxNodeId) {
throw new IllegalArgumentException(String.format("NodeId must be between %d and %d", 0, maxNodeId));
}
this.nodeId = nodeId;
}
// Let SequenceGenerator generate a nodeId
public SequenceGenerator() {
this.nodeId = createNodeId();
}
public synchronized long nextId() {
long currentTimestamp = timestamp();
if(currentTimestamp < lastTimestamp) {
throw new IllegalStateException("Invalid System Clock!");
}
if (currentTimestamp == lastTimestamp) {
sequence = (sequence + 1) & maxSequence;
if(sequence == 0) {
// Sequence Exhausted, wait till next millisecond.
currentTimestamp = waitNextMillis(currentTimestamp);
}
} else {
// reset sequence to start with zero for the next millisecond
sequence = 0;
}
lastTimestamp = currentTimestamp;
long id = currentTimestamp << (TOTAL_BITS - EPOCH_BITS);
id |= (nodeId << (TOTAL_BITS - EPOCH_BITS - NODE_ID_BITS));
id |= sequence;
return id;
}
// Get current timestamp in milliseconds, adjust for the custom epoch.
private static long timestamp() {
return Instant.now().toEpochMilli() - CUSTOM_EPOCH;
}
// Block and wait till next millisecond
private long waitNextMillis(long currentTimestamp) {
while (currentTimestamp == lastTimestamp) {
currentTimestamp = timestamp();
}
return currentTimestamp;
}
private int createNodeId() {
int nodeId;
try {
StringBuilder sb = new StringBuilder();
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
byte[] mac = networkInterface.getHardwareAddress();
if (mac != null) {
for(int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X", mac[i]));
}
}
}
nodeId = sb.toString().hashCode();
} catch (Exception ex) {
nodeId = (new SecureRandom().nextInt());
}
nodeId = nodeId & maxNodeId;
return nodeId;
}
}