zoukankan      html  css  js  c++  java
  • 生产uuid

    uuid生产功能

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

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

      1 import java.lang.management.ManagementFactory;
      2 import java.math.BigInteger;
      3 import java.net.InetAddress;
      4 import java.net.UnknownHostException;
      5 import java.text.DateFormat;
      6 import java.text.SimpleDateFormat;
      7 import java.util.Date;
      8 import java.util.concurrent.atomic.AtomicLong;
      9 
     10 import org.slf4j.Logger;
     11 import org.slf4j.LoggerFactory;
     12 
     13 /**
     14  * 获取流水号:ip+starttime+pid+flow
     15  * 
     16  * //将ip和starttime和pid转化为32进制+流水号
     17  * 
     18  * @author zhanghw
     19  */
     20 public class FlowNum {
     21     private static final Logger LOGGER = LoggerFactory.getLogger(FlowNum.class);
     22     private static final String FLOW_NUM_PERFIX;// 流水号前缀
     23     private static final AtomicLong count = new AtomicLong();
     24     private static final int radix = 32;// 转换的进制
     25     private static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HH:mm:ss-SSS");
     26 
     27     static {
     28         String ip = getLocalAddress();
     29         long startTime = System.currentTimeMillis();
     30         String pid = getPid();
     31         System.out.printf("ip=%s,pid= %s,startTime= %s", ip, pid, dateFormat.format(new Date(startTime)));
     32         LOGGER.info("this program ip ={},pid={},starttime={}", ip, pid, dateFormat.format(new Date(startTime)));
     33 
     34         StringBuilder sb = new StringBuilder();
     35         sb.append(ipToLong(ip)).append(startTime).append(pid);
     36         // ip(9),starttime(),pid
     37         System.out.println(sb.toString());
     38 
     39         BigInteger big = new BigInteger(sb.toString());
     40         String str_32 = big.toString(radix);
     41         FLOW_NUM_PERFIX = str_32 + "_";
     42     }
     43 
     44     /**
     45      * 获取一个流水号
     46      * @return
     47      */
     48     public static String get() {
     49         return FLOW_NUM_PERFIX + count.incrementAndGet();
     50     }
     51 
     52     /**
     53      * 47igkn0n9v03282b9dr8_7
     54      * 
     55      * @throws Exception
     56      */
     57     public static void printInfo(String FLOW_NUM_PERFIX) throws Exception {
     58         if (FLOW_NUM_PERFIX == null || FLOW_NUM_PERFIX.trim().length() == 0) {
     59             throw new Exception("the sttring is empty!");
     60         }
     61         String perfix = FLOW_NUM_PERFIX.split("_")[0];
     62         BigInteger perfix_32 = new BigInteger(perfix, radix);// 32进制
     63         String perfix_10 = perfix_32.toString(10);// 10进制
     64 
     65         String ip_long = perfix_10.substring(0, 9);
     66         String startTime = perfix_10.substring(9, 22);
     67         System.out.println(startTime);
     68         String pid = perfix_10.substring(22, perfix_10.length());
     69 
     70         String ipStr = longToIp(Long.valueOf(ip_long));
     71         String startTimeStr = dateFormat.format(new Date(Long.valueOf(startTime)));
     72 
     73         System.out.printf("ip=%s,time=%s,pid=%s", ipStr, startTimeStr, pid);
     74         LOGGER.info("ip={},time={},pid={}", ipStr, startTimeStr, pid);
     75 
     76     }
     77 
     78     /**
     79      * 获取当前进程id
     80      * 
     81      * @return
     82      */
     83     private static final String getPid() {
     84         // get name representing the running Java virtual machine.
     85         String name = ManagementFactory.getRuntimeMXBean().getName();
     86         String pid = name.split("@")[0];
     87         return pid;
     88     }
     89 
     90     /**
     91      * 获取当前服务器ip
     92      * 
     93      * @return
     94      */
     95     private static final String getLocalAddress() {
     96         InetAddress addr = null;
     97         try {
     98             addr = InetAddress.getLocalHost();
     99         } catch (UnknownHostException e) {
    100             e.printStackTrace();
    101         }
    102 
    103         if (addr == null) {
    104             return null;
    105         }
    106 
    107         return addr.getHostAddress();
    108     }
    109 
    110     /**
    111      * long转为ip
    112      * 
    113      * @param ip
    114      * @return
    115      */
    116     private static String longToIp(long ip) {
    117         return ((ip >> 24) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + (ip & 0xFF);
    118     }
    119 
    120     /**
    121      * ip转为long
    122      * 
    123      * @param ipAddress
    124      * @return
    125      */
    126     private static long ipToLong(String ipAddress) {
    127         long result = 0;
    128         String[] ipAddressInArray = ipAddress.split("\.");
    129         for (int i = 3; i >= 0; i--) {
    130             long ip = Long.parseLong(ipAddressInArray[3 - i]);
    131             result |= ip << (i * 8);
    132         }
    133         return result;
    134     }
    135 
    136 }
  • 相关阅读:
    程灵素:我走过山的时候山不说话
    编译原理自学计划
    由一个虚构的例子谈谈中小型研发型项目的技术管理及成本控制(全文)
    用3种IDE导入Linux 2.26 内核源码
    Web风行者的设计方案与计划
    使用pyste自动生成c++类的python wrapper
    让log4cpp日志文件超过2G(Linux下)的方法
    python绑定c++程序
    网络风行者(KSpider)的规则体系结构
    检测您的浏览器是否支持 HTML5 视频方法
  • 原文地址:https://www.cnblogs.com/zhangshiwen/p/6046089.html
Copyright © 2011-2022 走看看