In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Java implementation of snowflake algorithm code how to write the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this Java implementation of snowflake algorithm code how to write the article will have a harvest, let's take a look.
I. introduction
SnowFlow algorithm is a distributed id generation algorithm proposed by Twitter. The main core idea is to use the number of long type of 64bit as the global id. It is often used in distributed systems, and the concept of timestamp is added to id, which is basically non-repetitive and continues to increase upward.
In this 64bit, the first bit is not used, and then 41 bit are used as milliseconds, 10bit as the working machine id, and 12bit` as the serial number. It is shown in the following figure:
The first part: 0, this is a symbol bit, because if the first bit in the binary is 1, then it is all negative, but the id we generate are all positive, so the first bit is basically 0.
The second part: 41 bit, representing a timestamp, 41bit can represent up to $2 ^ {41} $- 1, can also represent 2 ^ {41}-1 millisecond value, basically almost 69 years.
The third part: 5 bit represents the id of the computer room.
The fourth part: five bit represents the machine id.
The fifth part: the 12 bit represents the id of the computer room, and the serial number is the serial number of the id generated simultaneously on a machine in a computer room in this millisecond, 0000 00000000. If it is the same millisecond, then the snowflake value will increase step by step.
To put it simply, if a service assumes that you want to generate a globally unique id, you can send a request to the system that deploys the SnowFlake algorithm, which will generate the unique id.
This algorithm ensures that a unique id is generated on a machine in a computer room in the same millisecond. Multiple id may be generated in a millisecond, but there are the sequence numbers of the last 12 bit to distinguish them.
Let's take a brief look at the code implementation part of this algorithm.
In short, each bit position in a 64bit number is used to set different flag bits.
2. Code implementation package com.lhh.utils;/** * @ author liuhuanhuan * @ version 1.0 * @ date the distributed unique id algorithm * / public class SnowFlow {/ / launched by 22:33 * @ describe Twitter on 2022-2-21, because the first bit in the binary is 1, then it is all negative, but the id we generate is positive, so the first bit is all 0. / / Machine ID 2-bit 5-bit 32-bit minus 1-bit 31 private long workerId; / / computer room ID 2-bit 5-bit 32-bit minus 1-bit 31 private long datacenterId; / / represents the latest sequence number of multiple id generated in one millisecond 12-bit 4096-1 = 4095 private long sequence; / / set a time initial value 2 ^ 41-1 can almost use 69 years private long twepoch = 15856442688L / / 5-bit machine id private long workerIdBits = 5L; / / 5-bit computer room id .' Private long datacenterIdBits = 5L; / / the number of id generated per millisecond to the power of 12 private long sequenceBits = 12L; / / this is a binary operation, that is, 5 bit can only have a maximum of 31 digits, that is, a machine id can only be a maximum of 32 private long maxWorkerId =-1L ^ (- 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.