需要的pom文件:
<!-- 順序UUID --> <dependency> <groupId>com.fasterxml.uuid</groupId> <artifactId>java-uuid-generator</artifactId> <version>3.1.4</version> </dependency>
有時間順序:
import com.fasterxml.uuid.EthernetAddress; import com.fasterxml.uuid.Generators; import com.fasterxml.uuid.impl.TimeBasedGenerator; public class KeyUtil { public static String generatorUUID(){ TimeBasedGenerator timeBasedGenerator = Generators.timeBasedGenerator(EthernetAddress.fromInterface()); return timeBasedGenerator.generate().toString(); } public static void main(String[] args) { System.err.println(KeyUtil.generatorUUID()); System.err.println(KeyUtil.generatorUUID()); } }
以下兩種方式都不合適:
分布式生成ID架構圖: