Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dekuan/dedid
An unique id generator for primary key of distributed database
https://github.com/dekuan/dedid
composer distributed-database laravel php unique-id-generator
Last synced: 18 days ago
JSON representation
An unique id generator for primary key of distributed database
- Host: GitHub
- URL: https://github.com/dekuan/dedid
- Owner: dekuan
- License: apache-2.0
- Created: 2017-08-03T18:09:33.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-12-25T07:54:44.000Z (almost 7 years ago)
- Last Synced: 2024-10-08T23:36:08.962Z (28 days ago)
- Topics: composer, distributed-database, laravel, php, unique-id-generator
- Language: PHP
- Homepage:
- Size: 20.5 KB
- Stars: 22
- Watchers: 4
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.chs.md
- License: LICENSE
Awesome Lists containing this project
README
# dekuan/dedid
一种为分布式数据库而设计的全局唯一 ID(主键)生成器。
本算法的实现参考了 Twitter Snowflake,但是在最后的 12 位您不仅仅可以使用随机数字,也可以通过指定字符串来获取哈希值。* [Documentation in English](README.md)
# 算法
### 结构体概况
本算法使用一个 64 位的 int 值作为 ID 的载体。~~~
0 xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx x xxxxxx xxxxxx xx xxxxxxxx
~~~### 结构体说明
位置 | 长度 | 用途 | 备注
----------|----------|----------|----------
0 | 1 | 保留位 | 一直都是 0
1~41 | 41 | 以毫秒为单位的流淌时间 |0~69 年,即本 ID 算法最多可以使用 69 年
42~46 | 5 | 数据中心编号 | 编号范围 0~31,最多可容纳 32 个数据中心
47~51 | 5 | 数据中心下属数据节点编号 | 编号范围 0~31,每个数据中心最多可容纳 32 个数据节点。所以整个系统最多可容纳 1024 个数据节点
52~63 | 12 | 随机数/哈希值 | 取值范围 0~4095# 位排列
### 数据中心编号
~~~
0 00000000 00000000 00000000 00000000 00000000 0 11111 00000 0000 0000000000000000 00000000 00000000 00000000 00000000 00111110 00000000 00000000
00 00 00 00 00 3E 00 00
~~~### 数据节点编号
~~~
0 00000000 00000000 00000000 00000000 00000000 0 00000 11111 0000 0000000000000000 00000000 00000000 00000000 00000000 00000001 11110000 00000000
00 00 00 00 00 01 F0 00
~~~### 流淌时间
~~~
0 11111111 11111111 11111111 11111111 11111111 1 00000 00000 0000 0000000001111111 11111111 11111111 11111111 11111111 11000000 00000000 00000000
7F FF FF FF FF C0 00 00
~~~### 随机数/哈希值
~~~
0 00000000 00000000 00000000 00000000 00000000 0 00000 00000 1111 1111111100000000 00000000 00000000 00000000 00000000 00000000 00001111 11111111
00 00 00 00 00 00 0F FF
~~~# 使用方法
### 创建一个普通的随机数 ID
~~~
$cDId = CDId::getInstance();
$nCenter = 0;
$nNode = 1;$arrD = [];
$nNewId = $cDId->createId( $nCenter, $nNode, null, $arrD );echo "new id = " . $nNewId . "\r\n";
print_r( $arrD );~~~
##### 输出
~~~
new id = 114654484990270790
Array
(
[center] => 0
[node] => 1
[time] => 27335759399
[rand] => 3398
)
~~~### 指定字符串,创建一个带有哈希值的 ID
~~~
$cDId = CDId::getInstance();
$nCenter = 0;
$nNode = 15;$sSrc = "dekuan";
$arrD = [];
$nNewId = $cDId->createId( $nCenter, $nNode, $sSrc, $arrD );echo "new id = " . $nNewId . "\r\n";
print_r( $arrD );~~~
##### 输出
~~~
new id = 114654631304370386
Array
(
[center] => 0
[node] => 1
[time] => 27335794283
[rand] => 2258
)
~~~### 解析 ID 获取详细信息
~~~
$cDId = CDId::getInstance();
$arrId = $cDId->parseId( 114654631304370386 );
print_r( $arrId );~~~
##### 输出
~~~
Array
(
[center] => 0
[node] => 1
[time] => 27335794283
[rand] => 2258
)
~~~# 如何安装
~~~
# composer require dekuan/dedid
~~~
了解更多信息,敬请访问 [https://packagist.org/packages/dekuan/dedid](https://packagist.org/packages/dekuan/dedid)