生产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;
} }
生产uuid的更多相关文章
- 很实用的js限制不让输入其他字符,只让输入数字和 js生成UUID
onkeyup="this.value=this.value.replace(/\D/g,'')" js生产UUID var createUUID = (function (uui ...
- JAVA Get UUID
UUID是通用唯一标识码(Universally Unique Identifier),通过开源软件基金会(OSF)设立的一种算法生成.它的主要作用就是保证生成的字符串在同一时空中所有机器上都是唯一的 ...
- UUID相同导致的网络连接问题
目录 场景 思路 解决过程 提升虚拟机配置 直连交换机 最终解决方案 总结 场景 有同事从公司寄了一台服务器到现场,用来安装数据库.缓存等组件供开发使用.到了之后,连接电源.网线,设置IP,用vSph ...
- java 与日期转换相关的方法(java.util.date类型和java.sql.date类型互相转换)、随机字符串生成方法、UUID生产随机字符串
package com.oop.util; import java.text.*; import java.util.UUID; import org.junit.Test; /* * 与日期相关的工 ...
- Java生成唯一GUID UUID
GUID(Global unique identifier)全局唯一标识符,它是由网卡上的标识数字(每个网卡都有唯一的标识号)以及 CPU 时钟的唯一数字生成的的一个 16 字节的二进制值. GUID ...
- 如何在高并发的分布式系统中产生UUID
一.数据库发号器 每一次都请求数据库,通过数据库的自增ID来获取全局唯一ID 对于小系统来说,这是一个简单有效的方案,不过也就不符合讨论情形中的高并发的场景. 首先,数据库自增ID需要锁表 而且,UU ...
- 基于redis的分布式锁(不适合用于生产环境)
基于redis的分布式锁 1 介绍 这篇博文讲介绍如何一步步构建一个基于Redis的分布式锁.会从最原始的版本开始,然后根据问题进行调整,最后完成一个较为合理的分布式锁. 本篇文章会将分布式锁的实现分 ...
- kafka生产消费原理笔记
一.什么是kafka Kafka是最初由Linkedin公司开发,是一个分布式.支持分区的(partition).多副本的(replica),基于zookeeper协调的分布式消息系统,它的最大的特性 ...
- 升级后重启造成fsck.ext3: Unable to resolve UUID
这篇文章帮了我的大忙了:转载自:http://wilber82.blog.51cto.com/1124820/724472 今天在做服务器补丁部署,有一台ESX4.1的服务器在升级后重启过程中挂了,通 ...
随机推荐
- nodemon配置文件简单整理
文件名称nodemon.json 内容如下: { "restartable":"rs",//重启的命令,默认是 rs "ignore":[& ...
- mfc打开程序
void CMy3MFCDlg::OnBnClickedButton1() { // TODO: 在此添加控件通知处理程序代码 HINSTANCE hRslt = ShellExecute(NULL, ...
- Beta版本冲刺——day5
No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 站立式会议 今日计划表 人员 工作 ...
- ubuntu12.10 源更新出错(sudo apt-get update)
Ubuntu12.10 刚安装完发现vi编辑器无法正常使用,后来用sudo apt-get install vim 一直出错,搜索相关资源发现需要更新源 首先,备份一下Ubuntu 12.10 原来的 ...
- 在 Web 项目中应用 Apache Shiro
Apache Shiro 是功能强大并且容易集成的开源权限框架,它能够完成认证.授权.加密.会话管理等功能.认证和授权为权限控制的核心,简单来说,"认证"就是证明你是谁? Web ...
- Tomcat启动报错 Could not reserve enough space for object heap
报错信息: Error occurred during initialization of VM Could not reserve enough space for object heap Coul ...
- C# 读取和配置IniFile
自定义IniFile操作类 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- SQL起别名
select列的时候取别名有三种方法,这三种方法并不是所有数据库都适用. 方法一.直接在字段名称后面加上别名,中间以空格隔开. 方法二.以as关键字指定字段别名,as在select的字段和别名之间. ...
- [Maven]Maven详解
转自:http://www.cnblogs.com/hongwz/p/5456578.html 一.前言 以前做过的项目中,没有真正的使用过Maven,只知道其名声很大,其作用是用来管理jar ...
- JAVA使用堆外内存导致swap飙高
https://github.com/nereuschen/blog/issues/29 堆内内存分析一般用Memory Analyzer Tool http://tivan.iteye.com/bl ...