分布式序列号ID算法---UUID
===========================================通过UUID获取分布式序列号ID
import java.util.UUID; public class UuidWorker { /** * UUID方式获取序列号(36位) * @return * reg bdd52c7a-8079-4b15-8dc6-61322247b594 */ public static String toUuid() { UUID randomUUID = UUID.randomUUID(); return randomUUID.toString(); } /** * UUID方式获取序列号(32位),字母小写 * @return * reg bdd52c7a80794b158dc661322247b594 */ public static String toUuid2() { UUID randomUUID = UUID.randomUUID(); return randomUUID.toString().replace("-", ""); } /** * UUID方式获取序列号(32位),字母大写 * @return * reg 9D3D155E155F4357B0829D66848A18AA */ public static String toUuid3() { UUID randomUUID = UUID.randomUUID(); return randomUUID.toString().replace("-", "").toUpperCase(); } }
===========================================通过UUID获取分布式序列号ID测试类
/** * UUID实现序列号获取 */ @Test public void test_uuid() { //bdd52c7a-8079-4b15-8dc6-61322247b594 System.out.println(UuidWorker.toUuid()); System.out.println(UuidWorker.toUuid2()); System.out.println(UuidWorker.toUuid3()); }