• 雪花算法生成分布式主键ID


    直接上代码,复制即可使用

    public class SnowflakeIdGenerator {
    
        private static final long START_TIMESTAMP = 1624000000000L; // 设置起始时间戳,2021-06-18 00:00:00
        private static final long DATA_CENTER_ID_BITS = 5L;
        private static final long WORKER_ID_BITS = 5L;
        private static final long SEQUENCE_BITS = 12L;
    
        private static final long MAX_DATA_CENTER_ID = ~(-1L << DATA_CENTER_ID_BITS);
        private static final long MAX_WORKER_ID = ~(-1L << WORKER_ID_BITS);
        private static final long MAX_SEQUENCE = ~(-1L << SEQUENCE_BITS);
    
        private static final long DATA_CENTER_ID_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS;
        private static final long WORKER_ID_SHIFT = SEQUENCE_BITS;
    
        private static final long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS + DATA_CENTER_ID_BITS;
    
        private static long dataCenterId;
        private static long workerId;
        private static long sequence = 0L;
        private static long lastTimestamp = -1L;
    
        public static void init(long dataCenterId, long workerId) {
            if (dataCenterId > MAX_DATA_CENTER_ID || dataCenterId < 0) {
                throw new IllegalArgumentException(String.format("Data center ID can't be greater than %d or less than 0", MAX_DATA_CENTER_ID));
            }
            if (workerId > MAX_WORKER_ID || workerId < 0) {
                throw new IllegalArgumentException(String.format("Worker ID can't be greater than %d or less than 0", MAX_WORKER_ID));
            }
            SnowflakeIdGenerator.dataCenterId = dataCenterId;
            SnowflakeIdGenerator.workerId = workerId;
        }
    
        public static synchronized String generateId() {
            long timestamp = System.currentTimeMillis();
    
            if (timestamp < lastTimestamp) {
                throw new RuntimeException("Clock moved backwards. Refusing to generate id for " + (lastTimestamp - timestamp) + " milliseconds.");
            }
    
            if (timestamp == lastTimestamp) {
                sequence = (sequence + 1) & MAX_SEQUENCE;
                if (sequence == 0) {
                    timestamp = waitNextMillis(timestamp);
                }
            } else {
                sequence = 0L;
            }
    
            lastTimestamp = timestamp;
    
            return String.valueOf(((timestamp - START_TIMESTAMP) << TIMESTAMP_LEFT_SHIFT)
                    | (dataCenterId << DATA_CENTER_ID_SHIFT)
                    | (workerId << WORKER_ID_SHIFT)
                    | sequence);
        }
    
        private static long waitNextMillis(long timestamp) {
            long currentTimestamp = System.currentTimeMillis();
            while (currentTimestamp <= timestamp) {
                currentTimestamp = System.currentTimeMillis();
            }
            return currentTimestamp;
        }
    
        public static void main(String[] args) {
            SnowflakeIdGenerator.init(1, 1);
    
            for (int i = 0; i < 10; i++) {
                System.out.println("Snowflake ID: " + SnowflakeIdGenerator.generateId());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    在这个示例中,你可以通过 SnowflakeIdGenerator.init(dataCenterId, workerId); 初始化数据中心 ID 和工作 ID,然后通过 SnowflakeIdGenerator.generateId(); 静态方法生成 Snowflake ID 的字符串形式。

  • 相关阅读:
    C++ 创建文件并写入内容
    2024年csdn最新最全的Postman接口测试: postman实现参数化
    C语言小bug汇总
    使用ApiFox衔接前后端开发人员,提升沟通效率实践
    OSI和TCP的握手/挥手
    设计模式-11装饰器模式(装饰器设计模式)详解
    面试:Android中的一些小问题随笔
    linux压缩解压命令
    Java:File类
    记录一次手动读取BigTiff文件(Python)
  • 原文地址:https://blog.csdn.net/qq_30776829/article/details/136223084