static SnowflakeIdWorker instance = new SnowflakeIdWorker(0, 0);
//==============================Constructors===================================== /** * 构造函数 * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ publicSnowflakeIdWorker(long workerId, long datacenterId){ if (workerId > maxWorkerId || workerId < 0) { thrownew IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { thrownew IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; }
//如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { thrownew RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); }
publicSequence(long workerId, long datacenterId){ // Assert.isFalse(workerId > 31L || workerId < 0L, String.format("worker Id can't be greater than %d or less than 0", 31L), new Object[0]); // Assert.isFalse(datacenterId > 31L || datacenterId < 0L, String.format("datacenter Id can't be greater than %d or less than 0", 31L), new Object[0]); if (workerId > maxWorkerId || workerId < 0) { thrownew IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { thrownew IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; }
protectedstaticlonggetMaxWorkerId(long datacenterId, long maxWorkerId){ StringBuilder mpid = new StringBuilder(); mpid.append(datacenterId); String name = ManagementFactory.getRuntimeMXBean().getName(); if (StringUtils.isNotBlank(name)) { mpid.append(name.split("@")[0]); }
protectedstaticlonggetDatacenterId(long maxDatacenterId){ long id = 0L;
try { InetAddress ip = InetAddress.getLocalHost(); NetworkInterface network = NetworkInterface.getByInetAddress(ip); if (network == null) { id = 1L; } else { byte[] mac = network.getHardwareAddress(); if (null != mac) { id = (255L & (long)mac[mac.length - 1] | 65280L & (long)mac[mac.length - 2] << 8) >> 6; id %= maxDatacenterId + 1L; } } } catch (Exception var7) { logger.warn(" getDatacenterId: " + var7.getMessage()); }
return id; }
publicsynchronizedlongnextId(){ long timestamp = this.timeGen(); if (timestamp < this.lastTimestamp) { long offset = this.lastTimestamp - timestamp; if (offset > 5L) { thrownew RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset)); }
try { this.wait(offset << 1); timestamp = this.timeGen(); if (timestamp < this.lastTimestamp) { thrownew RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", offset)); } } catch (Exception var6) { thrownew RuntimeException(var6); } }