生成唯一id写法,雪花算法

Posted wangquanyi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了生成唯一id写法,雪花算法相关的知识,希望对你有一定的参考价值。

这个工具直接调用就可以了,用法和写法如下:

代码:

这个是雪花算法的写法:

 1 public class SnowFlakeUtil 
 2 
 3     /**
 4      * 起始的时间戳
 5      */
 6     private final static long START_STMP = 1480166465631L;
 7 
 8     /**
 9      * 每一部分占用的位数
10      */
11     private final static long SEQUENCE_BIT = 12; //序列号占用的位数
12     private final static long MACHINE_BIT = 5;  //机器标识占用的位数
13     private final static long DATACENTER_BIT = 5;//数据中心占用的位数
14 
15     /**
16      * 每一部分的最大值
17      */
18     private final static long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT);
19     private final static long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
20     private final static long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
21 
22     /**
23      * 每一部分向左的位移
24      */
25     private final static long MACHINE_LEFT = SEQUENCE_BIT;
26     private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
27     private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;
28 
29     private long datacenterId;  //数据中心
30     private long machineId;    //机器标识
31     private long sequence = 0L; //序列号
32     private long lastStmp = -1L;//上一次时间戳
33 
34     public SnowFlakeUtil(long datacenterId, long machineId) 
35         if (datacenterId > MAX_DATACENTER_NUM || datacenterId < 0) 
36             throw new IllegalArgumentException("datacenterId can‘t be greater than MAX_DATACENTER_NUM or less than 0");
37         
38         if (machineId > MAX_MACHINE_NUM || machineId < 0) 
39             throw new IllegalArgumentException("machineId can‘t be greater than MAX_MACHINE_NUM or less than 0");
40         
41         this.datacenterId = datacenterId;
42         this.machineId = machineId;
43     
44 
45     /**
46      * 产生下一个ID
47      *
48      * @return
49      */
50     public synchronized long nextId() 
51         long currStmp = getNewstmp();
52         if (currStmp < lastStmp) 
53             throw new RuntimeException("Clock moved backwards.  Refusing to generate id");
54         
55 
56         if (currStmp == lastStmp) 
57             //相同毫秒内,序列号自增
58             sequence = (sequence + 1) & MAX_SEQUENCE;
59             //同一毫秒的序列数已经达到最大
60             if (sequence == 0L) 
61                 currStmp = getNextMill();
62             
63          else 
64             //不同毫秒内,序列号置为0
65             sequence = 0L;
66         
67 
68         lastStmp = currStmp;
69 
70         return (currStmp - START_STMP) << TIMESTMP_LEFT //时间戳部分
71                 | datacenterId << DATACENTER_LEFT      //数据中心部分
72                 | machineId << MACHINE_LEFT            //机器标识部分
73                 | sequence;                            //序列号部分
74     
75 
76     private long getNextMill() 
77         long mill = getNewstmp();
78         while (mill <= lastStmp) 
79             mill = getNewstmp();
80         
81         return mill;
82     
83 
84     private long getNewstmp() 
85         return System.currentTimeMillis();
86     
87 //小测试代码:
88 
89 //    public static void main(String[] args) 
90 //        SnowFlakeUtil snowFlake = new SnowFlakeUtil(2, 3);
91 //
92 //            System.out.println(snowFlake.nextId());
93 //
94 //    
95 

具体的调用:

 1 import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3 import java.util.Random;
 4 
 5 public class CreateAUniqueIDUtil 
 6     public String ImageID(String subtype) 
 7         SnowFlakeUtil snowFlake = new SnowFlakeUtil(2, 3);//调用雪花算法生成18位唯一id
 8         long randomNumber18 = snowFlake.nextId();//18位唯一id
 9         Random rand = new Random();//生成随机数
10         String cardNnumer = "";
11         for (int a = 0; a < 2; a++) 
12             cardNnumer += rand.nextInt(10);//生成2位随机数
13         
14 //        String subtype = "01";//01代表的是对应应用中解析时的参数,当前01代表 人员
15         String randomNumberString = "";
16         for (int a = 0; a < 5; a++) 
17             randomNumberString += rand.nextInt(10);//生成5位数字
18         
19         Date date = new Date();
20         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
21         String format = df.format(date);
22         String ImageID = randomNumber18 + cardNnumer + subtype + format + randomNumberString;
23         return ImageID;
24     
25 
26     public String PersonID(String subtype) 
27         SnowFlakeUtil snowFlake = new SnowFlakeUtil(2, 3);//调用雪花算法生成18位唯一id
28         long randomNumber18 = snowFlake.nextId();//18位唯一id
29         Random rand = new Random();//生成随机数
30         String cardNnumer = "";
31         for (int i = 0; i < 23; i++) 
32             cardNnumer += rand.nextInt(10);//生成23位随机数
33         
34 //        String subtype = "01";//01-人员
35         String randomNumberString = "";
36         for (int a = 0; a < 5; a++) 
37             randomNumberString += rand.nextInt(10);//生成5位数字
38         
39         String PersonID = randomNumber18 + cardNnumer + subtype + randomNumberString;
40         return PersonID;
41     
42 
43     public String SourceID(String subtype) 
44         SnowFlakeUtil snowFlake = new SnowFlakeUtil(2, 3);//调用雪花算法生成18位唯一id
45         long randomNumber18 = snowFlake.nextId();//18位唯一id
46         Random rand = new Random();//生成随机数
47         String cardNnumer = "";
48         for (int i = 0; i < 2; i++) 
49             cardNnumer += rand.nextInt(10);//生成2位随机数
50         
51 //        String subtype = "02";//01-人员02-机动车03-非机动车04-物品05-场景06-人脸等
52         Date date = new Date();
53         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
54         String format = df.format(date);
55         String randomNumberString = "";
56         for (int a = 0; a < 5; a++) 
57             randomNumberString += rand.nextInt(10);//生成5位数字
58         
59         String SourceID = randomNumber18 + cardNnumer + subtype + format + randomNumberString;
60         return SourceID;
61     
62 

 

以上是关于生成唯一id写法,雪花算法的主要内容,如果未能解决你的问题,请参考以下文章

分布式唯一ID生成算法-雪花算法

php雪花算法SnowFlake生成唯一ID

雪花算法:分布式唯一ID生成利器

spring boot中使用雪花算法生成雪花ID

分布式场景全局唯一ID生成工具类(非雪花算法)

分布式场景全局唯一ID生成工具类(非雪花算法)