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;
} }

生产uuid的更多相关文章

  1. 很实用的js限制不让输入其他字符,只让输入数字和 js生成UUID

    onkeyup="this.value=this.value.replace(/\D/g,'')" js生产UUID var createUUID = (function (uui ...

  2. JAVA Get UUID

    UUID是通用唯一标识码(Universally Unique Identifier),通过开源软件基金会(OSF)设立的一种算法生成.它的主要作用就是保证生成的字符串在同一时空中所有机器上都是唯一的 ...

  3. UUID相同导致的网络连接问题

    目录 场景 思路 解决过程 提升虚拟机配置 直连交换机 最终解决方案 总结 场景 有同事从公司寄了一台服务器到现场,用来安装数据库.缓存等组件供开发使用.到了之后,连接电源.网线,设置IP,用vSph ...

  4. java 与日期转换相关的方法(java.util.date类型和java.sql.date类型互相转换)、随机字符串生成方法、UUID生产随机字符串

    package com.oop.util; import java.text.*; import java.util.UUID; import org.junit.Test; /* * 与日期相关的工 ...

  5. Java生成唯一GUID UUID

    GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...

  6. 如何在高并发的分布式系统中产生UUID

    一.数据库发号器 每一次都请求数据库,通过数据库的自增ID来获取全局唯一ID 对于小系统来说,这是一个简单有效的方案,不过也就不符合讨论情形中的高并发的场景. 首先,数据库自增ID需要锁表 而且,UU ...

  7. 基于redis的分布式锁(不适合用于生产环境)

    基于redis的分布式锁 1 介绍 这篇博文讲介绍如何一步步构建一个基于Redis的分布式锁.会从最原始的版本开始,然后根据问题进行调整,最后完成一个较为合理的分布式锁. 本篇文章会将分布式锁的实现分 ...

  8. kafka生产消费原理笔记

    一.什么是kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性 ...

  9. 升级后重启造成fsck.ext3: Unable to resolve UUID

    这篇文章帮了我的大忙了:转载自:http://wilber82.blog.51cto.com/1124820/724472 今天在做服务器补丁部署,有一台ESX4.1的服务器在升级后重启过程中挂了,通 ...

随机推荐

  1. PHP MVC简单介绍,对PHP当前主流的MVC做了一个总结

    东抄西抄,对PHP当前主流的MVC做了一个总结PPT. 希望对初学者有点帮助! PHP MVC初步.ppt

  2. Netty系列之Netty百万级推送服务设计要点

    1. 背景 1.1. 话题来源 最近很多从事移动互联网和物联网开发的同学给我发邮件或者微博私信我,咨询推送服务相关的问题.问题五花八门,在帮助大家答疑解惑的过程中,我也对问题进行了总结,大概可以归纳为 ...

  3. json在项目中的应用大总结

    一.摘要 刚开始接触json的时候,那时候还不太清楚json到底是个什么东西,然后就在项目中使用了它.因为没有搞明白json的本质,所以刚开始使用json的时候走了不少弯路.这次总结一些json的知识 ...

  4. ESXi查询网卡的驱动和固件版本

    ~ # esxcfg-nics -lName PCI Driver Link Speed Duplex MAC Address MTU Description vmnic0 0000:01:00.00 ...

  5. DOJO DOM 功能

    In this tutorial, you'll learn about how to use Dojo to manipulate the DOM in a simple, cross-browse ...

  6. Install NukeX v7.0v6 in CentOS 7

    - download THE_FOUNDRY_NUKEX_V7.0V6_LNX64-XFORCE - unzip and untar to /home/user0/tools/foundry/nuke ...

  7. CodeSmith模板

    重复性的工作交给代码生成器就好,这里分享几套模板, 1.从数据库抓取字段,生成Table元素,这个在web开发中很有用 <%-- Name: Author: Description: --%&g ...

  8. python 获取当前目录下文件(转)

    今天继续整理原来写的 python 代码,下面是获取文件信息的 python 处理代码. 获取指定目录下文件的文件名以及文件的数量,然后列出其中还存在的目录名称: #!/usr/bin/env pyt ...

  9. 使用canvas编写环形图.

    原理使用canvas画图: 第一步:画一个大圆 第二部:画一个扇形 第三部:画一个小圆 相互叠加. 最终效果: 现在上代码: (function($){ $.fn.drawPic=function(o ...

  10. [转]细说SQL Server中的加密

    简介 加密是指通过使用密钥或密码对数据进行模糊处理的过程.在SQL Server中,加密并不能替代其他的安全设置,比如防止未被授权的人访问数据库或是数据库实例所在的Windows系统,甚至是数据库所在 ...