雪花算法案例

雪花算法

“雪花算法”(Snowflake Algorithm)是一种用于生成唯一ID的算法。它之所以被称为“雪花算法”,是因为它的ID格式和雪花的形状有些相似。

雪花算法的主要目标是生成一个全局唯一的ID,用于在分布式系统中标识不同的数据元素或对象。这些ID通常包含了时间戳、机器节点ID和序列号等信息。雪花算法中的时间戳精确到毫秒级,机器节点ID用于在多台机器上唯一标识,序列号保证了在同一毫秒内生成的ID的唯一性。

雪花算法的设计理念是简单高效,生成的ID具有趋势递增的特点,方便数据库索引和查询优化。它被广泛应用于分布式系统中,例如在大规模的数据存储、消息队列、分布式锁等方面。

public class SnowflakeIdGenerator {
    // 起始的时间戳,用于计算时间戳部分
    private final static long START_TIMESTAMP = 1632585600000L; // 这里以2021-09-26 00:00:00为起始时间

    // 分别占用位数
    private final static long SEQUENCE_BITS = 12; // 序列号占用的位数
    private final static long MACHINE_ID_BITS = 5; // 机器标识占用的位数
    private final static long DATA_CENTER_ID_BITS = 5; // 数据中心占用的位数

    // 每部分的最大值
    private final static long MAX_SEQUENCE = ~(-1L << SEQUENCE_BITS);
    private final static long MAX_MACHINE_ID = ~(-1L << MACHINE_ID_BITS);
    private final static long MAX_DATA_CENTER_ID = ~(-1L << DATA_CENTER_ID_BITS);

    // 每部分向左的位移
    private final static long MACHINE_ID_SHIFT = SEQUENCE_BITS;
    private final static long DATA_CENTER_ID_SHIFT = SEQUENCE_BITS + MACHINE_ID_BITS;
    private final static long TIMESTAMP_SHIFT = SEQUENCE_BITS + MACHINE_ID_BITS + DATA_CENTER_ID_BITS;

    private long dataCenterId; // 数据中心ID
    private long machineId; // 机器标识ID
    private long sequence = 0L; // 序列号
    private long lastTimestamp = -1L; // 上次生成ID的时间戳

    public SnowflakeIdGenerator(long dataCenterId, long machineId) {
        if (dataCenterId > MAX_DATA_CENTER_ID || dataCenterId < 0) {
            throw new IllegalArgumentException("Data center ID can't be greater than " + MAX_DATA_CENTER_ID + " or less than 0");
        }
        if (machineId > MAX_MACHINE_ID || machineId < 0) {
            throw new IllegalArgumentException("Machine ID can't be greater than " + MAX_MACHINE_ID + " or less than 0");
        }
        this.dataCenterId = dataCenterId;
        this.machineId = machineId;
    }

    public synchronized long generateId() {
        long timestamp = System.currentTimeMillis(); // 获取当前时间戳
        if (timestamp < lastTimestamp) {
            throw new RuntimeException("Clock moved backwards. Refusing to generate ID");
        }

        if (timestamp == lastTimestamp) {
            sequence = (sequence + 1) & MAX_SEQUENCE;
            if (sequence == 0) {
                timestamp = waitNextMillis(lastTimestamp);
            }
        } else {
            sequence = 0L;
        }

        lastTimestamp = timestamp;

        return ((timestamp - START_TIMESTAMP) << TIMESTAMP_SHIFT)
            | (dataCenterId << DATA_CENTER_ID_SHIFT)
            | (machineId << MACHINE_ID_SHIFT)
            | sequence;
    }

    private long waitNextMillis(long lastTimestamp) {
        long timestamp = System.currentTimeMillis();
        while (timestamp <= lastTimestamp) {
            timestamp = System.currentTimeMillis();
        }
        return timestamp;
    }
}
public class SnowflakeIdGeneratorExample {
    public static void main(String[] args) {
        SnowflakeIdGenerator generator = new SnowflakeIdGenerator(1, 1);

        // 生成10个唯一ID
        for (int i = 0; i < 10; i++) {
            long id = generator.generateId();
            System.out.println("Generated ID: " + id);
        }
    }
}