天天看点

JAVA UUID 生成唯一标识需求UUID代码实现获取八位UUID标识码

    项目在设计表的时候,要处理并发多的一些数据,类似订单号不能重复,要保持唯一。原本以为来个时间戳,精确到毫秒应该不错了。后来觉得是错了,测试环境下很多一样的id,不能达到唯一标识。

    jdk api 是这么说的:

“表示通用唯一标识符 (uuid) 的类。 uuid 表示一个 128 位的值。”

    详细的说就是:

“uuid

含义是通用唯一识别码 (universally unique identifier),这 是一个软件建构的标准,也是被开源软件基金会 (open

software foundation, osf) 的组织在分布式计算环境 (distributed computing

environment, dce) 领域的一部份。uuid

的目的,是让分布式系统中的所有元素,都能有唯一的辨识资讯,而不需要透过中央控制端来做辨识资讯的指定。如此一来,每个人都可以建立不与其它人冲突的

uuid。在这样的情况下,就不需考虑数据库建立时的名称重复问题。目前最广泛应用的 uuid,即是微软的 microsoft’s globally

unique identifiers (guids),而其他重要的应用,则有 linux ext2/ext3 档案系统、luks

加密分割区、gnome、kde、mac os x 等等。”

uuid由以下几部分的组合:   

(1)当前日期和时间,uuid的第一个部分与时间有关,如果你在生成一个uuid之后,过几秒又生成一个uuid,则第一个部分不同,其余相同。   

(2)时钟序列   

(3)全局唯一的ieee机器识别号,如果有网卡,从网卡mac地址获得,没有网卡以其他方式获得。

    很方便的,直接调用uuid的randomuuid方法,即可获得uuid对象,然后就获取了这个唯一标识码。

1

2

3

4

5

<code>public static void main(string[] args)</code>

<code>{</code>

<code>    </code><code>uuid uuid = uuid.randomuuid();</code>

<code>    </code><code>system.out.println(uuid);</code>

<code>}</code>

    run一下,可以从控制台发现:

<code>&lt;</code><code>font</code> <code>size</code><code>=</code><code>"4"</code><code>&gt;     65752c66-bd3f-4564-b8d6-92d66796e007&lt;/</code><code>font</code><code>&gt;</code>

    这就是唯一标志码。但显得冗长,不够友好。如果在url后面做参数,更加不够友好。还有存储一个uuid要花费更多的空间。获取的时间倒不必考虑太多。

仿着网上大牛代码,直接上代码:

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<code>&lt;</code><code>font</code> <code>size</code><code>=</code><code>"4"</code><code>&gt;public static string[] chars = new string[]</code>

<code>        </code><code>{</code>

<code>            </code><code>"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",</code>

<code>            </code><code>"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",</code>

<code>            </code><code>"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v","w", "x", "y", "z"</code>

<code>        </code><code>}; </code>

<code>    </code><code>public static string getshortuuid()</code>

<code>    </code><code>{ </code>

<code>        </code><code>stringbuffer stringbuffer = new stringbuffer(); </code>

<code>        </code><code>string uuid = uuid.randomuuid().tostring().replace("-", ""); </code>

<code>        </code><code>for (int i = 0; i &lt; </code><code>8</code><code>; i++)</code>

<code>        </code><code>{ </code>

<code>            </code><code>string </code><code>str</code>      <code>= </code><code>uuid</code><code>.substring(i * 4, i * 4 + 4); </code>

<code>            </code><code>int </code><code>strinteger</code>  <code>= integer.parseint(str, 16); </code>

<code>            </code><code>stringbuffer.append(chars[strinteger % 0x3e]); </code>

<code>        </code><code>} </code>

<code>        </code> 

<code>        </code><code>return stringbuffer.tostring(); </code>

<code>    </code><code>}  &lt;/font&gt;</code>

用300个测试下,没问题。足够用了,能适应环境场景即可。