JAVA UUID 生成
GUID是一個(gè)128位長(zhǎng)的數(shù)字,一般用16進(jìn)制表示。算法的核心思想是結(jié)合機(jī)器的網(wǎng)卡、當(dāng)?shù)貢r(shí)間、一個(gè)隨即數(shù)來生成GUID。從理論上講,如果一臺(tái)機(jī)器每秒產(chǎn)生10000000個(gè)GUID,則可以保證(概率意義上)3240年不重復(fù)。
UUID是1.5中新增的一個(gè)類,在java.util下,用它可以產(chǎn)生一個(gè)號(hào)稱全球唯一的ID
package com.mytest;
import java.util.UUID;
public class UTest {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
System.out.println(uuid);
}
}
- UUID(Universally Unique Identifier)全局唯一標(biāo)識(shí)符,是指在一臺(tái)機(jī)器上生成的數(shù)字,它保證對(duì)在同一時(shí)空中的所有機(jī)器都是唯一的。按照開放軟件基金會(huì)(OSF)制定的標(biāo)準(zhǔn)計(jì)算,用到了以太網(wǎng)卡地址、納秒級(jí)時(shí)間、芯片ID碼和許多可能的數(shù)字。由以下幾部分的組合:當(dāng)前日期和時(shí)間(UUID的第一個(gè)部分與時(shí)間有關(guān),如果你在生成一個(gè)UUID之后,過幾秒又生成一個(gè)UUID,則第一個(gè)部分不同,其余相同),時(shí)鐘序列,全局唯一的IEEE機(jī)器識(shí)別號(hào)(如果有網(wǎng)卡,從網(wǎng)卡獲得,沒有網(wǎng)卡以其他方式獲得),UUID的唯一缺陷在于生成的結(jié)果串會(huì)比較長(zhǎng)。
- 在Java中生成UUID主要有以下幾種方式:
JDK1.5
如果使用的JDK1.5的話,那么生成UUID變成了一件簡(jiǎn)單的事,以為JDK實(shí)現(xiàn)了UUID:
java.util.UUID,直接調(diào)用即可.
UUID uuid = UUID.randomUUID();
String s = UUID.randomUUID().toString();
//用來生成數(shù)據(jù)庫(kù)的主鍵id非常不錯(cuò)。。
UUID是由一個(gè)十六位的數(shù)字組成,表現(xiàn)出來的形式例如
550E8400-E29B-11D4-A716-446655440000
//下面就是實(shí)現(xiàn)為數(shù)據(jù)庫(kù)獲取一個(gè)唯一的主鍵id的代碼
public class UUIDGenerator {
public UUIDGenerator() {
}
/**
* 獲得一個(gè)UUID
* @return String UUID
*/
public static String getUUID(){
String s = UUID.randomUUID().toString();
//去掉“-”符號(hào)
return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
}
/**
* 獲得指定數(shù)目的UUID
* @param number int 需要獲得的UUID數(shù)量
* @return String[] UUID數(shù)組
*/
public static String[] getUUID(int number){
if(number < 1){
return null;
}
String[] ss = new String[number];
for(int i=0;i<number;i++){
ss[i] = getUUID();
}
return ss;
}
public static void main(String[] args){
String[] ss = getUUID(10);
for(int i=0;i<ss.length;i++){
System.out.println(ss[i]);
}
}
}
兩種方式生成guid 與uuid
需要comm log 庫(kù)
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
public class RandomGUID extends Object {
protected final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
.getLog(getClass());
public String valueBeforeMD5 = "";
public String valueAfterMD5 = "";
private static Random myRand;
private static SecureRandom mySecureRand;
private static String s_id;
private static final int PAD_BELOW = 0x10;
private static final int TWO_BYTES = 0xFF;
/*
* Static block to take care of one time secureRandom seed.
* It takes a few seconds to initialize SecureRandom. You might
* want to consider removing this static block or replacing
* it with a "time since first loaded" seed to reduce this time.
* This block will run only once per JVM instance.
*/
static {
mySecureRand = new SecureRandom();
long secureInitializer = mySecureRand.nextLong();
myRand = new Random(secureInitializer);
try {
s_id = InetAddress.getLocalHost().toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/*
* Default constructor. With no specification of security option,
* this constructor defaults to lower security, high performance.
*/
public RandomGUID() {
getRandomGUID(false);
}
/*
* Constructor with security option. Setting secure true
* enables each random number generated to be cryptographically
* strong. Secure false defaults to the standard Random function seeded
* with a single cryptographically strong random number.
*/
public RandomGUID(boolean secure) {
getRandomGUID(secure);
}
/*
* Method to generate the random GUID
*/
private void getRandomGUID(boolean secure) {
MessageDigest md5 = null;
StringBuffer sbValueBeforeMD5 = new StringBuffer(128);
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
logger.error("Error: " + e);
}
try {
long time = System.currentTimeMillis();
long rand = 0;
if (secure) {
rand = mySecureRand.nextLong();
} else {
rand = myRand.nextLong();
}
sbValueBeforeMD5.append(s_id);
sbValueBeforeMD5.append(":");
sbValueBeforeMD5.append(Long.toString(time));
sbValueBeforeMD5.append(":");
sbValueBeforeMD5.append(Long.toString(rand));
valueBeforeMD5 = sbValueBeforeMD5.toString();
md5.update(valueBeforeMD5.getBytes());
byte[] array = md5.digest();
StringBuffer sb = new StringBuffer(32);
for (int j = 0; j < array.length; ++j) {
int b = array[j] & TWO_BYTES;
if (b < PAD_BELOW)
sb.append('0');
sb.append(Integer.toHexString(b));
}
valueAfterMD5 = sb.toString();
} catch (Exception e) {
logger.error("Error:" + e);
}
}
/*
* Convert to the standard format for GUID
* (Useful for SQL Server UniqueIdentifiers, etc.)
* Example: C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
*/
public String toString() {
String raw = valueAfterMD5.toUpperCase();
StringBuffer sb = new StringBuffer(64);
sb.append(raw.substring(0, 8));
sb.append("-");
sb.append(raw.substring(8, 12));
sb.append("-");
sb.append(raw.substring(12, 16));
sb.append("-");
sb.append(raw.substring(16, 20));
sb.append("-");
sb.append(raw.substring(20));
return sb.toString();
}
// Demonstraton and self test of class
public static void main(String args[]) {
for (int i=0; i< 100; i++) {
RandomGUID myGUID = new RandomGUID();
System.out.println("Seeding String=" + myGUID.valueBeforeMD5);
System.out.println("rawGUID=" + myGUID.valueAfterMD5);
System.out.println("RandomGUID=" + myGUID.toString());
}
}
}
同樣
UUID uuid = UUID.randomUUID();
System.out.println("{"+uuid.toString()+"}");
UUID是指在一臺(tái)機(jī)器上生成的數(shù)字,它保證對(duì)在同一時(shí)空中的所有機(jī)器都是唯一的。通常平臺(tái)會(huì)提供生成UUID的API。UUID按照開放軟件基金會(huì)(OSF)制定的標(biāo)準(zhǔn)計(jì)算,用到了以太網(wǎng)卡地址、納秒級(jí)時(shí)間、芯片ID碼和許多可能的數(shù)字。由以下幾部分的組合:當(dāng)前日期和時(shí)間(UUID的第一個(gè)部分與時(shí)間有關(guān),如果你在生成一個(gè)UUID之后,過幾秒又生成一個(gè)UUID,則第一個(gè)部分不同,其余相同),時(shí)鐘序列,全局唯一的IEEE機(jī)器識(shí)別號(hào)(如果有網(wǎng)卡,從網(wǎng)卡獲得,沒有網(wǎng)卡以其他方式獲得),UUID的唯一缺陷在于生成的結(jié)果串會(huì)比較長(zhǎng)。關(guān)于UUID這個(gè)標(biāo)準(zhǔn)使用最普遍的是微軟的GUID(Globals Unique Identifiers)。
UUID含義是通用唯一識(shí)別碼 (Universally Unique Identifier),這 是一個(gè)軟件建構(gòu)的標(biāo)準(zhǔn),也是被開源軟件基金會(huì) (Open Software Foundation, OSF) 的組織在分布式計(jì)算環(huán)境 (Distributed Computing Environment, DCE) 領(lǐng)域的一部份。UUID 的目的,是讓分布式系統(tǒng)中的所有元素,都能有唯一的辨識(shí)資訊,而不需要透過中央控制端來做辨識(shí)資訊的指定。如此一來,每個(gè)人都可以建立不與其它人沖突的 UUID。在這樣的情況下,就不需考慮數(shù)據(jù)庫(kù)建立時(shí)的名稱重復(fù)問題。目前最廣泛應(yīng)用的 UUID,即是微軟的 Microsoft's Globally Unique Identifiers (GUIDs),而其他重要的應(yīng)用,則有 Linux ext2/ext3 檔案系統(tǒng)、LUKS 加密分割區(qū)、GNOME、KDE、Mac OS X 等等。
以下是具體生成UUID的例子:
view plaincopy to clipboardprint?
package test;
import java.util.UUID;
public class UUIDGenerator {
public UUIDGenerator() {
}
public static String getUUID() {
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
// 去掉"-"符號(hào)
String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
return str+","+temp;
}
//獲得指定數(shù)量的UUID
public static String[] getUUID(int number) {
if (number < 1) {
return null;
}
String[] ss = new String[number];
for (int i = 0; i < number; i++) {
ss[i] = getUUID();
}
return ss;
}
public static void main(String[] args) {
String[] ss = getUUID(10);
for (int i = 0; i < ss.length; i++) {
System.out.println("ss["+i+"]====="+ss[i]);
}
}
}
package test;
import java.util.UUID;
public class UUIDGenerator {
public UUIDGenerator() {
}
public static String getUUID() {
UUID uuid = UUID.randomUUID();
String str = uuid.toString();
// 去掉"-"符號(hào)
String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
return str+","+temp;
}
//獲得指定數(shù)量的UUID
public static String[] getUUID(int number) {
if (number < 1) {
return null;
}
String[] ss = new String[number];
for (int i = 0; i < number; i++) {
ss[i] = getUUID();
}
return ss;
}
public static void main(String[] args) {
String[] ss = getUUID(10);
for (int i = 0; i < ss.length; i++) {
System.out.println("ss["+i+"]====="+ss[i]);
}
}
}
結(jié)果:
view plaincopy to clipboardprint?
ss[0]=====4cdbc040-657a-4847-b266-7e31d9e2c3d9,4cdbc040657a4847b2667e31d9e2c3d9
ss[1]=====72297c88-4260-4c05-9b05-d28bfb11d10b,72297c8842604c059b05d28bfb11d10b
ss[2]=====6d513b6a-69bd-4f79-b94c-d65fc841ea95,6d513b6a69bd4f79b94cd65fc841ea95
ss[3]=====d897a7d3-87a3-4e38-9e0b-71013a6dbe4c,d897a7d387a34e389e0b71013a6dbe4c
ss[4]=====5709f0ba-31e3-42bd-a28d-03485b257c94,5709f0ba31e342bda28d03485b257c94
ss[5]=====530fbb8c-eec9-48d1-ae1b-5f792daf09f3,530fbb8ceec948d1ae1b5f792daf09f3
ss[6]=====4bf07297-65b2-45ca-b905-6fc6f2f39158,4bf0729765b245cab9056fc6f2f39158
ss[7]=====6e5a0e85-b4a0-485f-be54-a758115317e1,6e5a0e85b4a0485fbe54a758115317e1
ss[8]=====245accec-3c12-4642-967f-e476cef558c4,245accec3c124642967fe476cef558c4
ss[9]=====ddd4b5a9-fecd-446c-bd78-63b70bb500a1,ddd4b5a9fecd446cbd7863b70bb500a1
ss[0]=====4cdbc040-657a-4847-b266-7e31d9e2c3d9,4cdbc040657a4847b2667e31d9e2c3d9
ss[1]=====72297c88-4260-4c05-9b05-d28bfb11d10b,72297c8842604c059b05d28bfb11d10b
ss[2]=====6d513b6a-69bd-4f79-b94c-d65fc841ea95,6d513b6a69bd4f79b94cd65fc841ea95
ss[3]=====d897a7d3-87a3-4e38-9e0b-71013a6dbe4c,d897a7d387a34e389e0b71013a6dbe4c
ss[4]=====5709f0ba-31e3-42bd-a28d-03485b257c94,5709f0ba31e342bda28d03485b257c94
ss[5]=====530fbb8c-eec9-48d1-ae1b-5f792daf09f3,530fbb8ceec948d1ae1b5f792daf09f3
ss[6]=====4bf07297-65b2-45ca-b905-6fc6f2f39158,4bf0729765b245cab9056fc6f2f39158
ss[7]=====6e5a0e85-b4a0-485f-be54-a758115317e1,6e5a0e85b4a0485fbe54a758115317e1
ss[8]=====245accec-3c12-4642-967f-e476cef558c4,245accec3c124642967fe476cef558c4
ss[9]=====ddd4b5a9-fecd-446c-bd78-63b70bb500a1,ddd4b5a9fecd446cbd7863b70bb500a1
可以看出,UUID 是指在一臺(tái)機(jī)器上生成的數(shù)字,它保證對(duì)在同一時(shí)空中的所有機(jī)器都是唯一的。通常平臺(tái)會(huì)提供生成的API。按照開放軟件基金會(huì)(OSF)制定的標(biāo)準(zhǔn)計(jì)算,用到了以太網(wǎng)卡地址、納秒級(jí)時(shí)間、芯片ID碼和許多可能的數(shù)字
UUID由以下幾部分的組合:
(1)當(dāng)前日期和時(shí)間,UUID的第一個(gè)部分與時(shí)間有關(guān),如果你在生成一個(gè)UUID之后,過幾秒又生成一個(gè)UUID,則第一個(gè)部分不同,其余相同。
(2)時(shí)鐘序列
(3)全局唯一的IEEE機(jī)器識(shí)別號(hào),如果有網(wǎng)卡,從網(wǎng)卡MAC地址獲得,沒有網(wǎng)卡以其他方式獲得。
UUID的唯一缺陷在于生成的結(jié)果串會(huì)比較長(zhǎng)。關(guān)于UUID這個(gè)標(biāo)準(zhǔn)使用最普遍的是微軟的GUID(Globals Unique Identifiers)。在ColdFusion中可以用CreateUUID()函數(shù)很簡(jiǎn)單的生成UUID,其格式為:xxxxxxxx-xxxx- xxxx-xxxxxxxxxxxxxxxx(8-4-4-16),其中每個(gè) x 是 0-9 或 a-f 范圍內(nèi)的一個(gè)十六進(jìn)制的數(shù)字。而標(biāo)準(zhǔn)的UUID格式為:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12),可以從cflib 下載CreateGUID() UDF進(jìn)行轉(zhuǎn)換。
使用UUID的好處在分布式的軟件系統(tǒng)中(比如:DCE/RPC, COM+,CORBA)就能體現(xiàn)出來,它能保證每個(gè)節(jié)點(diǎn)所生成的標(biāo)識(shí)都不會(huì)重復(fù),并且隨著WEB服務(wù)等整合技術(shù)的發(fā)展,UUID的優(yōu)勢(shì)將更加明顯。根據(jù)使用的特定機(jī)制,UUID不僅需要保證是彼此不相同的,或者最少也是與公元3400年之前其他任何生成的通用惟一標(biāo)識(shí)符有非常大的區(qū)別。
通用惟一標(biāo)識(shí)符還可以用來指向大多數(shù)的可能的物體。微軟和其他一些軟件公司都傾向使用全球惟一標(biāo)識(shí)符(GUID),這也是通用惟一標(biāo)識(shí)符的一種類型,可用來指向組建對(duì)象模塊對(duì)象和其他的軟件組件。第一個(gè)通用惟一標(biāo)識(shí)符是在網(wǎng)羅計(jì)算機(jī)系統(tǒng)(NCS)中創(chuàng)建,并且隨后成為開放軟件基金會(huì)(OSF)的分布式計(jì)算環(huán)境(DCE)的組件。