生产uuid

uuid生产功能

优点:分布式,高性能,不重复

近端时间要做一个获取唯一流水号的功能,于是有了:ip+starttime+pid+flow的方式。

 import java.lang.management.ManagementFactory;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* 获取流水号:ip+starttime+pid+flow
*
* //将ip和starttime和pid转化为32进制+流水号
*
* @author zhanghw
*/
public class FlowNum {
private static final Logger LOGGER = LoggerFactory.getLogger(FlowNum.class);
private static final String FLOW_NUM_PERFIX;// 流水号前缀
private static final AtomicLong count = new AtomicLong();
private static final int radix = 32;// 转换的进制
private static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss-SSS"); static {
String ip = getLocalAddress();
long startTime = System.currentTimeMillis();
String pid = getPid();
System.out.printf("ip=%s,pid= %s,startTime= %s", ip, pid, dateFormat.format(new Date(startTime)));
LOGGER.info("this program ip ={},pid={},starttime={}", ip, pid, dateFormat.format(new Date(startTime))); StringBuilder sb = new StringBuilder();
sb.append(ipToLong(ip)).append(startTime).append(pid);
// ip(9),starttime(),pid
System.out.println(sb.toString()); BigInteger big = new BigInteger(sb.toString());
String str_32 = big.toString(radix);
FLOW_NUM_PERFIX = str_32 + "_";
} /**
* 获取一个流水号
* @return
*/
public static String get() {
return FLOW_NUM_PERFIX + count.incrementAndGet();
} /**
* 47igkn0n9v03282b9dr8_7
*
* @throws Exception
*/
public static void printInfo(String FLOW_NUM_PERFIX) throws Exception {
if (FLOW_NUM_PERFIX == null || FLOW_NUM_PERFIX.trim().length() == 0) {
throw new Exception("the sttring is empty!");
}
String perfix = FLOW_NUM_PERFIX.split("_")[0];
BigInteger perfix_32 = new BigInteger(perfix, radix);// 32进制
String perfix_10 = perfix_32.toString(10);// 10进制 String ip_long = perfix_10.substring(0, 9);
String startTime = perfix_10.substring(9, 22);
System.out.println(startTime);
String pid = perfix_10.substring(22, perfix_10.length()); String ipStr = longToIp(Long.valueOf(ip_long));
String startTimeStr = dateFormat.format(new Date(Long.valueOf(startTime))); System.out.printf("ip=%s,time=%s,pid=%s", ipStr, startTimeStr, pid);
LOGGER.info("ip={},time={},pid={}", ipStr, startTimeStr, pid); } /**
* 获取当前进程id
*
* @return
*/
private static final String getPid() {
// get name representing the running Java virtual machine.
String name = ManagementFactory.getRuntimeMXBean().getName();
String pid = name.split("@")[0];
return pid;
} /**
* 获取当前服务器ip
*
* @return
*/
private static final String getLocalAddress() {
InetAddress addr = null;
try {
addr = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
} if (addr == null) {
return null;
} return addr.getHostAddress();
} /**
* long转为ip
*
* @param ip
* @return
*/
private static String longToIp(long ip) {
return ((ip >> 24) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + (ip & 0xFF);
} /**
* ip转为long
*
* @param ipAddress
* @return
*/
private static long ipToLong(String ipAddress) {
long result = 0;
String[] ipAddressInArray = ipAddress.split("\\.");
for (int i = 3; i >= 0; i--) {
long ip = Long.parseLong(ipAddressInArray[3 - i]);
result |= ip << (i * 8);
}
return result;
} }
上一篇:Java集合类学习-LinkedList, ArrayList, Stack, Queue, Vector


下一篇:Ajax跨域问题的出现和解决