生成30的全局ID

2019-10-15  本文已影响0人  小诸葛686

public class TraceIdGenerator {

private static String        IP_16 = "ffffffff";
private static AtomicInteger count = new AtomicInteger(1000);

static {
    try {
        String ipAddress = getInetAddress();
        if (ipAddress != null) {
            IP_16 = getIP_16(ipAddress);
        }
    } catch (Throwable e) {
        /*
         * empty catch block
         */
    }
}

private static String getTraceId(String ip, long timestamp, int nextId) {
    StringBuilder appender = new StringBuilder(30);
    appender.append(ip).append(timestamp).append(nextId).append(TracerUtils.getPID());
    return appender.toString();
}

public static String generate() {
    return getTraceId(IP_16, System.currentTimeMillis(), getNextId());
}

private static String getIP_16(String ip) {
    String[] ips = ip.split("\\.");
    StringBuilder sb = new StringBuilder();
    for (String column : ips) {
        String hex = Integer.toHexString(Integer.parseInt(column));
        if (hex.length() == 1) {
            sb.append('0').append(hex);
        } else {
            sb.append(hex);
        }

    }
    return sb.toString();
}

private static int getNextId() {
    for (;;) {
        int current = count.get();
        int next = (current > 9000) ? 1000 : current + 1;
        if (count.compareAndSet(current, next)) {
            return next;
        }
    }
}

public static String getInetAddress() {
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        InetAddress address = null;
        while (interfaces.hasMoreElements()) {
            NetworkInterface ni = interfaces.nextElement();
            Enumeration<InetAddress> addresses = ni.getInetAddresses();
            while (addresses.hasMoreElements()) {
                address = addresses.nextElement();
                if (!address.isLoopbackAddress() && address.getHostAddress().indexOf(":") == -1) {
                    return address.getHostAddress();
                }
            }
        }
        return null;
    } catch (Throwable t) {
        return null;
    }
}

}

注:来源于开源组件

上一篇下一篇

猜你喜欢

热点阅读