① 怎樣用java生成GUID與UUID
兩種方式生成guid與uuid
需要commlog庫
/**
*@authorAdministrator
*
*
*Window-Preferences-Java-CodeStyle-CodeTemplates
*/
importjava.net.InetAddress;
importjava.net.UnknownHostException;
importjava.security.MessageDigest;
importjava.security.NoSuchAlgorithmException;
importjava.security.SecureRandom;
importjava.util.Random;
{
protectedfinalorg.apache.commons.logging.Loglogger=org.apache.commons.logging.LogFactory
.getLog(getClass());
publicStringvalueBeforeMD5="";
publicStringvalueAfterMD5="";
privatestaticRandommyRand;
;
privatestaticStrings_id;
privatestaticfinalintPAD_BELOW=0x10;
privatestaticfinalintTWO_BYTES=0xFF;
/*
*.
*.Youmight
*
*itwitha"timesincefirstloaded"seedtorecethistime.
*.
*/
static{
mySecureRand=newSecureRandom();
longsecureInitializer=mySecureRand.nextLong();
myRand=newRandom(secureInitializer);
try{
s_id=InetAddress.getLocalHost().toString();
}catch(UnknownHostExceptione){
e.printStackTrace();
}
}
/*
*Defaultconstructor.,
*,highperformance.
*/
publicRandomGUID(){
getRandomGUID(false);
}
/*
*Constructorwithsecurityoption.Settingsecuretrue
*
*strong.
*.
*/
publicRandomGUID(booleansecure){
getRandomGUID(secure);
}
/*
*MethodtogeneratetherandomGUID
*/
privatevoidgetRandomGUID(booleansecure){
MessageDigestmd5=null;
StringBuffersbValueBeforeMD5=newStringBuffer(128);
try{
md5=MessageDigest.getInstance("MD5");
}catch(NoSuchAlgorithmExceptione){
logger.error("Error:"+e);
}
try{
longtime=System.currentTimeMillis();
longrand=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();
StringBuffersb=newStringBuffer(32);
for(intj=0;j<array.length;++j){
intb=array[j]&TWO_BYTES;
if(b<PAD_BELOW)
sb.append('0');
sb.append(Integer.toHexString(b));
}
valueAfterMD5=sb.toString();
}catch(Exceptione){
logger.error("Error:"+e);
}
}
/*
*
*(,etc.)
*Example:C2FEEEAC-CFCD-11D1-8B05-00600806D9B6
*/
publicStringtoString(){
Stringraw=valueAfterMD5.toUpperCase();
StringBuffersb=newStringBuffer(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));
returnsb.toString();
}
//
publicstaticvoidmain(Stringargs[]){
for(inti=0;i<100;i++){
RandomGUIDmyGUID=newRandomGUID();
System.out.println("SeedingString="+myGUID.valueBeforeMD5);
System.out.println("rawGUID="+myGUID.valueAfterMD5);
System.out.println("RandomGUID="+myGUID.toString());
}
}
}
同樣
UUIDuuid=UUID.randomUUID();
System.out.println("{"+uuid.toString()+"}");
UUID是指在一台機器上生成的數字,它保證對在同一時空中的所有機器都是唯一的。通常平台會提供生成UUID的API。UUID按照開放軟體基金會(OSF)制定的標准計算,用到了乙太網卡地址、納秒級時間、晶元ID碼和許多可能的數字。由以下幾部分的組合:當前日期和時間(UUID的第一個部分與時間有關,如果你在生成一個UUID之後,過幾秒又生成一個UUID,則第一個部分不同,其餘相同),時鍾序列,全局唯一的IEEE機器識別號(如果有網卡,從網卡獲得,沒有網卡以其他方式獲得),UUID的唯一缺陷在於生成的結果串會比較長。關於UUID這個標准使用最普遍的是微軟的GUID(GlobalsUniqueIdentifiers)。
② java uuid 和guid 的區別
全局唯一標識符(GUID)是一種演算法生成的二進制長度為128位的數字標識符。它常用於擁有多個節點、多台計算機的網路或系統中,以確保在理想情況下,任何計算機和計算機集群都不會生成兩個相同的GUID。GUID的總數達到了2^128(3.4×10^38)個,因此隨機生成兩個相同GUID的可能性非常小,但並不為0。為了減少這種重復的風險,用於生成GUID的演算法通常都會加入非隨機的參數,比如時間戳。
GUID這個詞有時也專指微軟對UUID標準的實現。UUID是由一組32位數的16進制數字組成的,理論上總數為16^32=2^128,約等於3.4 x 10^38。這意味著,即使每納秒產生1兆個UUID,也需要大約100億年才能用完所有的UUID。因此,GUID的唯一性非常高。
Java中的UUID與GUID相似,也是一種演算法生成的128位數字標識符。Java的UUID類提供了多種方法來生成UUID,比如使用隨機數生成器、基於時間戳等。與GUID一樣,UUID的生成也依賴於非隨機參數,以確保生成的UUID具有高度的唯一性。
在Java中,UUID可以用於各種場景,比如分布式系統中為對象分配全局唯一標識,或者在網路通信中作為消息標識符。通過使用非隨機參數,Java UUID能夠避免重復生成的問題,從而確保了其唯一性。
總結而言,Java UUID和GUID雖然都用於生成全局唯一標識符,但在實現細節上存在一些差異。GUID通常指微軟實現的UUID標准,而Java UUID則是在Java平台上實現的UUID,兩者都依賴於演算法和非隨機參數來確保唯一性。