package com.zhiche.tools.application; import java.util.Random; public class IdSnowFlake { private final long workerId;//數據中心(5位) private final long datacenterId;//節點(5位) private final long idepoch;//毫秒級時間(41位) private long sequence;//毫秒內序列(12位) private static final long datacenterIdShift = 17L; private static final long workerIdShift = 12L; private static final long timestampLeftShift = 22L; private static final long maxWorkerId = 31L; private static final long maxDatacenterId = 31L; private static final long sequenceMask = 4095L;//序列編號共12位,每節點每毫秒生成4096個ID private long lastTimestamp = -1L;//能夠根據須要設定一個最先時間,任什麼時候間不得小於最先時間,默認不設限制。 private long defaultTimestamp = 1288834974657L;//能夠根據實情進行時間的修改。 private static final Random r = new Random(); /** * 數據中心=random,節點中心=random,毫秒內序列(12位)=0,毫秒級時間(41位)=now */ public IdSnowFlake() { this(System.currentTimeMillis()); } /** * 數據中心=random,節點中心=random,毫秒內序列(12位)=0 * @param idepoch 毫秒級時間(41位) */ public IdSnowFlake(long idepoch) { this(r.nextInt((int) maxWorkerId), r.nextInt((int) maxDatacenterId), 0, idepoch); } /** * 毫秒內序列 = 0, 毫秒級時間 = now, [經常使用的構造爲這個構造] * @param workerId 數據中心(5位) * @param datacenterId 節點(5位) */ public IdSnowFlake(long workerId, long datacenterId) { this(workerId, datacenterId, 0, System.currentTimeMillis()); } /** * 毫秒級時間 = now * @param workerId 數據中心(5位) * @param datacenterId 節點(5位) * @param sequence 毫秒內序列(12位) */ public IdSnowFlake(long workerId, long datacenterId, long sequence) { this(workerId, datacenterId, sequence, System.currentTimeMillis()); } /** * 數據中心,節點中心,毫秒內序列(12位),毫秒級時間(41位) * @param workerId 數據中心(5位) * @param datacenterId 節點(5位) * @param sequence 毫秒內序列(12位) * @param idepoch 毫秒級時間(41位) */ public IdSnowFlake(long workerId, long datacenterId, long sequence, long idepoch) { this.workerId = workerId; this.datacenterId = datacenterId; this.sequence = sequence; this.idepoch = idepoch; if (workerId < 0 || workerId > maxWorkerId) { throw new IllegalArgumentException(String.format("非法workerId 數據中心(5位)應大於0而小於%d,而當前值爲:%d", maxWorkerId,workerId)); } if (datacenterId < 0 || datacenterId > maxDatacenterId) { throw new IllegalArgumentException(String.format("非法datacenterId 節點(5位)應大於0而小於%d,而當前值爲:%d ", maxDatacenterId , datacenterId)); } } /** * @return 數據中心5位 */ public long getDatacenterId() { return datacenterId; } public long getIdepoch() { return idepoch; } public long getlastTimeStamp() { return lastTimestamp; } public long getSequence() { return sequence; } /** * @return 節點5位 */ public long getWorkerId() { return workerId; } /** * @return 當前時間 */ public long getTime() { return System.currentTimeMillis(); } /** * @return 獲得long類型id */ public long getId() { long id = nextId(); return id; } /** * 設置最先時間 * @param lastTimestamp 最先時間 */ public void setLastTimestamp(long lastTimestamp) { this.lastTimestamp = lastTimestamp; } private synchronized long nextId() { long timestamp = timeGen(); if (timestamp < lastTimestamp) { throw new IllegalStateException(String.format("時間早於最低時間:%d",lastTimestamp)); } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = tilNextMillis(lastTimestamp); } } else { sequence = 0; } lastTimestamp = timestamp; //a|b的意思就是把a和b按位或, 按位或的意思就是先把a和b都換成2進制,而後用或操做 //[也能夠改爲直接+操做,直接+操做效率高,可是會有極低機率產生重複ID] long id = ((timestamp - defaultTimestamp) << timestampLeftShift)//前41位 | (datacenterId << datacenterIdShift)//中間前5位 | (workerId << workerIdShift)//中間後5位 | sequence;//最後12位 /* long id = ((timestamp - defaultTimestamp) << timestampLeftShift)//前41位 + (datacenterId << datacenterIdShift)//中間前5位 + (workerId << workerIdShift)//中間後5位 + sequence;//最後12位 */ return id; } private long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } private long timeGen() { return System.currentTimeMillis(); } @Override public String toString() { final StringBuilder sb = new StringBuilder("IdWorker{"); sb.append("workerId=").append(workerId); sb.append(", datacenterId=").append(datacenterId); sb.append(", idepoch=").append(idepoch); sb.append(", lastTimestamp=").append(lastTimestamp); sb.append(", sequence=").append(sequence); sb.append('}'); return sb.toString(); } }
package cn.huiyunche.base.service.utils; import cn.huiyunche.tools.application.IdSnowFlake; /** * @ClassName: UniquenessFlagUnits * @Description: 惟一性標示 * @author: Aaron * @date: 2016年7月25日 上午10:50:38 */ public class UniquenessFlagUnits { /** * @param prifix 前綴 * @return * @Title: generateUniquenessFlag * @Description: 根據前綴生成惟一性標示 * @return: String */ public static String generateUniquenessFlag(String prifix) { IdSnowFlake id = new IdSnowFlake(); return String.valueOf(prifix + id.getId()); } }