简体   繁体   English

基于HttpServletRequest生成全局(应用程序作用域)唯一请求ID的算法?

[英]Algorithm to generate global (Application scoped) unique request id based on HttpServletRequest?

I have issue with generating global unique request id based on HttpServletRequest. 我有基于HttpServletRequest生成全局唯一请求ID的问题。 I need to make sure the generated id is unique in Application scope. 我需要确保生成的id在Application范围内是唯一的。 Java UUID Or Random does not guarantee what I want although the chance for duplicate is very low. 尽管重复的可能性非常低,但Java UUID或Random并不能保证我想要的东西。

Anyone has any ideas? 有人有什么想法吗? Thanks, 谢谢,

如果你必须绝对确定你没有复制,你可以使用自动增量数据库ID,但鉴于使用UUID重复的机率极低,我不认为它证明了成本。

Random definitely doesn't guarantee uniqueness, but UUID definitely does that. Random肯定不保证唯一性,但UUID绝对做到了。 So your concern makes no sense. 所以你的担忧毫无意义。 Even if you've hardware capable of serving a billion HTTP requests per second, you'd only after 100 years reach 50% chance on a duplicate. 即使您的硬件能够每秒服务数十亿个HTTP请求,您也只能在100年后才能复制50%的机会。

If you have really really a hard head in this, just check in synchronized block if the application scope contains already such a key and if so, then just generate another one. 如果你真的有一个硬头,只需检查同步块,如果应用程序范围包含这样的密钥,如果是,那么只需生成另一个。 And so on. 等等。 This way you can even use Random . 这样你甚至可以使用Random

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM