生成唯一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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM