全局分布式id生成工具
IdGeneratorUtils
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.tencent.iov.parent.utils;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class IdGeneratorUtils {
private static final Logger log = LoggerFactory.getLogger(IdGeneratorUtils.class);
private static byte machineId = getMachineId();
private static int sequence = 0;
private static final int timeBits = 39;
private static final int workerBits = 8;
private static final int seqBits = 24;
private static final int timeShift = 32;
public IdGeneratorUtils() {
}
public static synchronized long generate() {
long id = System.currentTimeMillis() / 1000L << 32 | ((long)machineId & 255L) << 24 | (long)(sequence++ % 1048575);
return id;
}
public static synchronized long getIdStartByTime(Long time) {
long id = time << 32 | 0L | 0L;
return id;
}
public static Long getTimeById(Long id) {
long dateSecond = id >>> 32;
return dateSecond;
}
private static byte getMachineId() {
try {
Enumeration nifs = NetworkInterface.getNetworkInterfaces();
label44:
while(nifs.hasMoreElements()) {
NetworkInterface networkInterface = (NetworkInterface)nifs.nextElement();
Enumeration addresses = networkInterface.getInetAddresses();
InetAddress addr;
do {
do {
if (!addresses.hasMoreElements()) {
continue label44;
}
addr = (InetAddress)addresses.nextElement();
} while(!networkInterface.getName().equalsIgnoreCase("eth0") && !networkInterface.getName().equalsIgnoreCase("en0"));
} while(!(addr instanceof Inet4Address));
log.info("网卡接口名称:" + networkInterface.getName());
log.info("网卡接口地址:" + addr.getHostAddress());
byte[] addrBytes = addr.getAddress();
for(int i = 0; i < addrBytes.length; ++i) {
log.info("address{} = {}", i, addrBytes[i]);
}
return addrBytes[addrBytes.length - 1];
}
log.error("!!!!!!!!!!!!!!getMachineId error!!!!!!!!!!!!");
return 0;
} catch (Exception var6) {
log.error("!!!!!!!!!!!!!!getMachineId error!!!!!!!!!!!!");
return 0;
}
}
}