Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate distributed globally unique ID in JAVA

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to generate a distributed global unique ID in JAVA, many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

SnowFlake algorithm is Twitter's open source distributed id generation algorithm.

1. The SnowFlake generation ID of twitter can be generated sequentially in time.

2. The result of id generated by SnowFlake algorithm is an integer of 64bit size.

3. There is no duplicate id in the distributed system (there is a distinction between datacenterId and machineId)

/ * * @ Description snowflake algorithm is a classic algorithm for generating distributed global unique ID * Twitter_Snowflake.

* the structure of SnowFlake is as follows (use-separate each part):

* 0-0000000000 0000000000 0000000000 00000000000-00000000-000000000000

* 1-bit identification. Because the basic type of long is signed in Java, the highest bit is the sign bit, the positive number is 0 and the negative number is 1, so the id is generally positive and the highest bit is 0

* 41-bit time cut (millisecond). Note that 41-bit time cut is not the time cut for storing the current time, but the difference between the stored time cut (current time cut-start time cut) *). The start time cut here is usually the time when our id generator starts to use, which is specified by our program (such as the startTime attribute of the IdWorker class below). 41-bit time limit, can be used for 69 years, annual T = (1L)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report