在Java中创建GUID的一些最佳方法是什么?
当前回答
其他答案都是正确的,尤其是斯蒂芬·C的这个答案。
走出Java
出于安全考虑,在Java中生成UUID值仅限于版本4(随机)。
如果你想要其他版本的uuid,一种方法是让你的Java应用程序通过调用JVM之外的方法来生成uuid:
Command-line utilityBundled with nearly every operating system. For example, uuidgen found in Mac OS X, BSD, and Linux. Database serverUse JDBC to retrieve a UUID generated on the database server.For example, the uuid-ossp extension often bundled with Postgres. That extension can generates Versions 1, 3, and 4 values and additionally a couple variations: uuid_generate_v1mc() – generates a version 1 UUID but uses a random multicast MAC address instead of the real MAC address of the computer. uuid_generate_v5(namespace uuid, name text) – generates a version 5 UUID, which works like a version 3 UUID except that SHA-1 is used as a hashing method. Web ServiceFor example, UUID Generator creates Versions 1 & 3 as well as nil values and GUID.
其他回答
在许多情况下,我们需要对象的全局UUID,特别是在事件驱动架构或事件源中,我们必须根据日期对事件进行排序,但我们不需要关于时间戳的完整信息。
在那里,我们可以使用ULID的一个实现,它是按字典顺序排序的。
格式与标准UUID不同,但仍然很简单:
example value: 01AN4Z07BY79KA1307SR9X4MV3
01AN4Z07BY 79KA1307SR9X4MV3
|----------| |----------------|
Timestamp Randomness
48bits 80bits
在许多语言中都有实现。
例如,在Java中,有一个简单的库。
代码示例:
import de.huxhorn.sulky.ulid.ULID;
ULID ulid = new ULID();
// with current timestamp
String newId = ulid.nextULID();
// with selected timestamp
String newId2 = ulid.nextULID(Instant
.parse("2021-12-01T00:00:00.00Z")
.toEpochMilli()
);
使用Spring,您还可以为ULID生成器创建Bean。
@Configuration
public class UUIDGeneratorConfig {
@Bean
public ULID ulidGenerator() {
return new ULID();
}
}
@Component
public class ULIDGenerator {
private final ULID ulid;
public ULIDGenerator(ULID ulid) {
this.ulid = ulid;
}
public String generateUUID() {
return ulid.nextULID();
}
public String generateUUID(Instant timestamp) {
return ulid.nextULID(timestamp.toEpochMilli());
}
}
java.util.UUID.randomUUID();
其他答案都是正确的,尤其是斯蒂芬·C的这个答案。
走出Java
出于安全考虑,在Java中生成UUID值仅限于版本4(随机)。
如果你想要其他版本的uuid,一种方法是让你的Java应用程序通过调用JVM之外的方法来生成uuid:
Command-line utilityBundled with nearly every operating system. For example, uuidgen found in Mac OS X, BSD, and Linux. Database serverUse JDBC to retrieve a UUID generated on the database server.For example, the uuid-ossp extension often bundled with Postgres. That extension can generates Versions 1, 3, and 4 values and additionally a couple variations: uuid_generate_v1mc() – generates a version 1 UUID but uses a random multicast MAC address instead of the real MAC address of the computer. uuid_generate_v5(namespace uuid, name text) – generates a version 5 UUID, which works like a version 3 UUID except that SHA-1 is used as a hashing method. Web ServiceFor example, UUID Generator creates Versions 1 & 3 as well as nil values and GUID.
这取决于你想要什么样的UUID。
The standard Java UUID class generates Version 4 (random) UUIDs. (UPDATE - Version 3 (name) UUIDs can also be generated.) It can also handle other variants, though it cannot generate them. (In this case, "handle" means construct UUID instances from long, byte[] or String representations, and provide some appropriate accessors.) The Java UUID Generator (JUG) implementation purports to support "all 3 'official' types of UUID as defined by RFC-4122" ... though the RFC actually defines 4 types and mentions a 5th type.
关于UUID类型和变体的更多信息,维基百科中有一个很好的总结,RFC 4122和其他规范中有详细的信息。
看看与Java 5及更高版本捆绑在一起的UUID类。
例如:
如果你想要一个随机的UUID,你可以使用randomUUID方法。 如果您希望将UUID初始化为特定的值,可以使用UUID构造函数或fromString方法。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap