-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
60 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,62 @@ | ||
# esnowflake | ||
## 背景 | ||
雪花算法是64位的,需要有个workId,而这个workId,在k8s下处理起来会非常麻烦。而我们不需要非常小的数字存储,所以我们可以考虑使用128位的雪花算法。这样我们就可以不需要workId,而直接使用pod的ip地址来生成一个128位的id。 | ||
|
||
experimental | ||
有两种128位的雪花算法。 | ||
|
||
### 全随机的 | ||
``` | ||
/* | ||
Random | ||
* 1 41 65 128 | ||
* +---------------------------------------------+----------------------------+--------------------------------------------------------------------------+ | ||
* | timestamp(ms) | worker info | random number | | ||
* +---------------------------------------------+----------------------------+--------------------------------------------------------------------------+ | ||
* | 00000000 00000000 00000000 00000000 00000000 | 00000000 00000000 00000000 | 00000000 00000000 00000000 00000000 00000000 00000000 | 00000000 00000000 | | ||
* +---------------------------------------------+----------------------------+--------------------------------------------------------------------------+ | ||
* | ||
* 1. 40 位时间截(毫秒级),注意这是时间截的差值(当前时间截 - 开始时间截)。可以使用约 34 年: (1L << 40) / (1000L * 60 * 60 * 24 * 365) = 34。(2020-2054) | ||
* 2. 24 位 worker info 数据,适应 k8s 环境。 | ||
* 3. 64 随机数 | ||
*/ | ||
``` | ||
```go | ||
// New create a new snowflake node with a unique worker id. | ||
// ip 你的机器 ip | ||
// mask1, mask2, mask3 你自己填的随机数,用于混淆 ip | ||
obj := New("192.168.1.1", 1, 2, 3) | ||
id := obj.GenerateByRandom() | ||
fmt.Println(id) | ||
fmt.Println(obj.GetTime(id)) | ||
fmt.Println(obj.GetIp(id)) | ||
``` | ||
|
||
|
||
|
||
### 带序列号的 | ||
``` | ||
/* | ||
Sequence | ||
* 1 41 65 113 128 | ||
* +---------------------------------------------+----------------------------+------------------------------------------------------+-------------------+ | ||
* | timestamp(ms) | worker info | random number | sequence | | ||
* +---------------------------------------------+----------------------------+------------------------------------------------------+-------------------+ | ||
* | 00000000 00000000 00000000 00000000 00000000 | 00000000 00000000 00000000 | 00000000 00000000 00000000 00000000 00000000 00000000 | 00000000 00000000 | | ||
* +---------------------------------------------+----------------------------+------------------------------------------------------+-------------------+ | ||
* | ||
* 1. 40 位时间截(毫秒级),注意这是时间截的差值(当前时间截 - 开始时间截)。可以使用约 34 年: (1L << 40) / (1000L * 60 * 60 * 24 * 365) = 34。(2020-2054) | ||
* 2. 24 位 worker info 数据,适应 k8s 环境。 | ||
* 3. 48 随机数 | ||
* 4. 16 位 sequence | ||
*/ | ||
``` | ||
```go | ||
// New create a new snowflake node with a unique worker id. | ||
// ip 你的机器 ip | ||
// mask1, mask2, mask3 你自己填的随机数,用于混淆 ip | ||
obj := New("192.168.1.1", 1, 2, 3) | ||
id := obj.GenerateBySequence() | ||
fmt.Println(id) | ||
fmt.Println(obj.GetTime(id)) | ||
fmt.Println(obj.GetIp(id)) | ||
``` |