diff --git a/C#.NET/README.md b/C#.NET/README.md index 4779926..33834f0 100644 --- a/C#.NET/README.md +++ b/C#.NET/README.md @@ -1,9 +1,9 @@ - + ## 运行环境 .NET Standard 2.0+ -## 引用nuget包 +## 引用 nuget 包 ``` ``` diff --git a/C#.NET/source/Yitter.IdGenerator/Contract/IdGeneratorOptions.cs b/C#.NET/source/Yitter.IdGenerator/Contract/IdGeneratorOptions.cs index 8a54b3b..c5ee938 100644 --- a/C#.NET/source/Yitter.IdGenerator/Contract/IdGeneratorOptions.cs +++ b/C#.NET/source/Yitter.IdGenerator/Contract/IdGeneratorOptions.cs @@ -29,34 +29,31 @@ namespace Yitter.IdGenerator /// /// 机器码 - /// 与 WorkerIdBitLength 有关系 - /// (ushort类型,最大值65535,如果有更高要求,请修改数据类型,或联系作者) + /// 必须由外部设定,最大值 2^WorkerIdBitLength-1 /// public virtual ushort WorkerId { get; set; } = 0; /// /// 机器码位长 - /// 范围:1-21(要求:序列数位长+机器码位长不超过22)。 - /// 建议范围:6-12。 + /// 默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22) /// public virtual byte WorkerIdBitLength { get; set; } = 6;//10; /// /// 序列数位长 - /// 范围:2-21(要求:序列数位长+机器码位长不超过22)。 - /// 建议范围:6-14。 + /// 默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22) /// public virtual byte SeqBitLength { get; set; } = 6;//10; /// /// 最大序列数(含) - /// (由SeqBitLength计算的最大值) + /// 设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]) /// public virtual int MaxSeqNumber { get; set; } = 0; /// /// 最小序列数(含) - /// 默认5,不小于5,不大于MaxSeqNumber + /// 默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位 /// public virtual ushort MinSeqNumber { get; set; } = 5; diff --git a/C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM1.cs b/C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM1.cs index 2c0ffcf..dc91a05 100644 --- a/C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM1.cs +++ b/C#.NET/source/Yitter.IdGenerator/Core/SnowWorkerM1.cs @@ -236,6 +236,8 @@ namespace Yitter.IdGenerator { EndOverCostAction(currentTimeTick); + // TODO: 在漂移终止,等待时间对齐时,如果发生时间回拨较长,则此处可能等待较长时间。可优化为:在漂移终止时增加时间回拨应对逻辑。(该情况发生概率很低) + _LastTimeTick = GetNextTimeTick(); _CurrentSeqNumber = MinSeqNumber; _IsOverCost = false; diff --git a/C#.NET/source/Yitter.IdGenerator/Yitter.IdGenerator.csproj b/C#.NET/source/Yitter.IdGenerator/Yitter.IdGenerator.csproj index 1994ba4..3e634fa 100644 --- a/C#.NET/source/Yitter.IdGenerator/Yitter.IdGenerator.csproj +++ b/C#.NET/source/Yitter.IdGenerator/Yitter.IdGenerator.csproj @@ -18,7 +18,7 @@ Yitter https://github.com/yitter/idgenerator MIT - 1.0.11 + 1.0.12 diff --git a/C/README.md b/C/README.md index 8dcff92..032b153 100644 --- a/C/README.md +++ b/C/README.md @@ -1,4 +1,4 @@ -# idgenerator +# idgenerator ## 编译说明 diff --git a/C/source/idgen/IdGenOptions.h b/C/source/idgen/IdGenOptions.h index a50aeaf..deedafc 100644 --- a/C/source/idgen/IdGenOptions.h +++ b/C/source/idgen/IdGenOptions.h @@ -10,18 +10,25 @@ typedef struct IdGenOptions { /// 雪花计算方法,(1-漂移算法|2-传统算法),默认1 uint8_t Method; + /// 基础时间(ms单位),不能超过当前系统时间 uint64_t BaseTime; - /// 机器码,与 WorkerIdBitLength 有关系 + + /// 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1 uint32_t WorkerId; - /// 机器码位长,范围:1-21(要求:序列数位长+机器码位长不超过22) + + /// 机器码位长,默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22) uint8_t WorkerIdBitLength; - /// 序列数位长,范围:2-21(要求:序列数位长+机器码位长不超过22) + + /// 序列数位长,默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22) uint8_t SeqBitLength; - /// 最大序列数(含),(由 SeqBitLength 计算的最大值) + + /// 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]) uint32_t MaxSeqNumber; - /// 最小序列数(含),默认5,不小于5,不大于 MaxSeqNumber + + /// 最小序列数(含),默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位 uint32_t MinSeqNumber; + /// 最大漂移次数(含),默认2000,推荐范围 500-20000(与计算能力有关) uint32_t TopOverCostCount; diff --git a/Go/README.md b/Go/README.md index 1bff156..44ec2fd 100644 --- a/Go/README.md +++ b/Go/README.md @@ -1,21 +1,9 @@ -# ❄ idenerator-go +# ❄ idenerator-go ## 介绍 项目更多介绍参照:https://github.com/yitter/idgenerator -## Go环境 - -1.SDK,go1.14 - -2.启用 Go-Modules -``` -go env -w GO111MODULE=on - -# Next *ONLY* for China-Users: -go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct -``` - -3. 安装方式 +## 引用全局库 ``` go get -u -v github.com/yitter/idgenerator-go ``` @@ -40,5 +28,18 @@ idgen.SetIdGenerator(options) var newId = idgen.NextId() ``` +## 关于Go环境 + +1.SDK,go1.14 + +2.启用 Go-Modules +``` +go env -w GO111MODULE=on + +# Next *ONLY* for China-Users: +go env -w GOPROXY=https://goproxy.cn,https://goproxy.io,direct +``` + ## 代码贡献者(按时间顺序) guoyahao | amuluowin | houseme + diff --git a/Go/source/idgen/IdGeneratorOptions.go b/Go/source/idgen/IdGeneratorOptions.go index 136179a..f1fe52f 100644 --- a/Go/source/idgen/IdGeneratorOptions.go +++ b/Go/source/idgen/IdGeneratorOptions.go @@ -9,11 +9,11 @@ package idgen type IdGeneratorOptions struct { Method uint16 // 雪花计算方法,(1-漂移算法|2-传统算法),默认1 BaseTime int64 // 基础时间(ms单位),不能超过当前系统时间 - WorkerId uint16 // 机器码,与 WorkerIdBitLength 有关系 - WorkerIdBitLength byte // 机器码位长,范围:1-21(要求:序列数位长+机器码位长不超过22) - SeqBitLength byte // 序列数位长,范围:2-21(要求:序列数位长+机器码位长不超过22) - MaxSeqNumber uint32 // 最大序列数(含),(由SeqBitLength计算的最大值) - MinSeqNumber uint32 // 最小序列数(含),默认5,不小于5,不大于MaxSeqNumber + WorkerId uint16 // 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1 + WorkerIdBitLength byte // 机器码位长,默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22) + SeqBitLength byte // 序列数位长,默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22) + MaxSeqNumber uint32 // 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]) + MinSeqNumber uint32 // 最小序列数(含),默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位 TopOverCostCount uint32 // 最大漂移次数(含),默认2000,推荐范围500-10000(与计算能力有关) } diff --git a/Go/source/main.go b/Go/source/main.go index 086742b..e18dd42 100644 --- a/Go/source/main.go +++ b/Go/source/main.go @@ -4,7 +4,6 @@ import ( "C" "fmt" "time" - "unsafe" "yitidgen/idgen" "yitidgen/regworkerid" ) @@ -27,10 +26,11 @@ func RegisterOne(ip *C.char, port int32, password *C.char, maxWorkerId int32) in } // 注册多个 WorkerId,会先注销所有本机已注册的记录 -//export RegisterMany -func RegisterMany(ip *C.char, port int32, password *C.char, maxWorkerId int32, totalCount int32) *C.int { - values := regworkerid.RegisterMany(C.GoString(ip), port, C.GoString(password), maxWorkerId, totalCount) - return (*C.int)(unsafe.Pointer(&values)) +///export RegisterMany +func RegisterMany(ip *C.char, port int32, password *C.char, maxWorkerId int32, totalCount int32) []int32 { + //values := regworkerid.RegisterMany(C.GoString(ip), port, C.GoString(password), maxWorkerId, totalCount) + //return (*C.int)(unsafe.Pointer(&values)) + return regworkerid.RegisterMany(C.GoString(ip), port, C.GoString(password), maxWorkerId, totalCount); } // 注销本机已注册的 WorkerId @@ -82,6 +82,8 @@ func main() { } } -// windows: go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go -//// go install -buildmode=shared -linkshared std -//linux: go build -o ./target/yitidgengo.so -buildmode=c-shared main.go +// windows: +// go build -o ./target/yitidgengo.dll -buildmode=c-shared main.go + +// linux init: go install -buildmode=shared -linkshared std +// go build -o ./target/yitidgengo.so -buildmode=c-shared main.go diff --git a/Java/README.md b/Java/README.md index e045fe5..f3efdad 100644 --- a/Java/README.md +++ b/Java/README.md @@ -1,4 +1,4 @@ - + ## 运行环境 JDK 1.8+ @@ -8,7 +8,7 @@ JDK 1.8+ com.github.yitter yitter-idgenerator - 1.0.5 + 1.0.6 ``` diff --git a/Java/source/pom.xml b/Java/source/pom.xml index a12a031..3c27bdc 100644 --- a/Java/source/pom.xml +++ b/Java/source/pom.xml @@ -6,7 +6,7 @@ com.github.yitter yitter-idgenerator - 1.0.5 + 1.0.6 jar yitter-idgenerator diff --git a/Java/source/src/main/java/com/github/yitter/contract/IdGeneratorOptions.java b/Java/source/src/main/java/com/github/yitter/contract/IdGeneratorOptions.java index 8c56dae..0bbe49e 100644 --- a/Java/source/src/main/java/com/github/yitter/contract/IdGeneratorOptions.java +++ b/Java/source/src/main/java/com/github/yitter/contract/IdGeneratorOptions.java @@ -6,7 +6,7 @@ package com.github.yitter.contract; /** * 雪花算法使用的参数 - * 此处代码不采用 get/set 那种冗长的写法 + * 参数说明,参考 README.md 的 “配置参数” 章节。 */ public class IdGeneratorOptions { @@ -23,35 +23,32 @@ public class IdGeneratorOptions { public long BaseTime = 1582136402000L; /** - * 机器码,必须由外部系统设置 - * 与 WorkerIdBitLength 有关系 - * (short类型,最大值32766,如果有更高要求,请修改数据类型,或联系作者) + * 机器码 + * 必须由外部设定,最大值 2^WorkerIdBitLength-1 */ public short WorkerId = 0; /** * 机器码位长 - * 范围:1-21(要求:序列数位长+机器码位长不超过22)。 - * 建议范围:6-12。 + * 默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22) */ public byte WorkerIdBitLength = 6; /** * 序列数位长 - * 范围:2-21(要求:序列数位长+机器码位长不超过22)。 - * 建议范围:6-14。 + * 默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22) */ public byte SeqBitLength = 6; /** * 最大序列数(含) - * (由SeqBitLength计算的最大值) + * 设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]) */ public short MaxSeqNumber = 0; /** * 最小序列数(含) - * 默认5,不小于5,不大于MaxSeqNumber + * 默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号是0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位 */ public short MinSeqNumber = 5; diff --git a/Java/source/src/main/java/com/github/yitter/core/SnowWorkerM1.java b/Java/source/src/main/java/com/github/yitter/core/SnowWorkerM1.java index 46402a9..6246b96 100644 --- a/Java/source/src/main/java/com/github/yitter/core/SnowWorkerM1.java +++ b/Java/source/src/main/java/com/github/yitter/core/SnowWorkerM1.java @@ -76,49 +76,17 @@ public class SnowWorkerM1 implements ISnowWorker { } private void BeginOverCostAction(long useTimeTick) { -// if (GenAction == null) { -// return; -// } -// -// DoGenIdAction(new OverCostActionArg( -// WorkerId, -// useTimeTick, -// 1, -// _OverCostCountInOneTerm, -// _GenCountInOneTerm, -// _TermIndex)); + } private void EndOverCostAction(long useTimeTick) { if (_TermIndex > 10000) { _TermIndex = 0; } -// -// if (GenAction == null) { -// return; -// } -// -// DoGenIdAction(new OverCostActionArg( -// WorkerId, -// useTimeTick, -// 2, -// _OverCostCountInOneTerm, -// _GenCountInOneTerm, -// _TermIndex)); } private void BeginTurnBackAction(long useTimeTick) { -// if (GenAction == null) { -// return; -// } -// -// DoGenIdAction(new OverCostActionArg( -// WorkerId, -// useTimeTick, -// 8, -// _OverCostCountInOneTerm, -// _GenCountInOneTerm, -// _TermIndex)); + } private void EndTurnBackAction(long useTimeTick) { diff --git a/README.md b/README.md index dab405b..22c5b64 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# 比雪花算法更好用的ID生成算法(单机或分布式唯一ID) +# 比雪花算法更好用的数据主键新算法 -## 💎 算法介绍 +## 💎 基本介绍 一个全新的雪花漂移算法,生成的ID更短、速度更快。 @@ -59,7 +59,7 @@ QQ群:646049993 ## 新算法特点 - 整形数字,随时间单调递增(不一定连续),长度更短,用50年都不会超过 js Number类型最大值。(默认配置 WorkerId 是6bit,自增数是6bit) + 整形数字,随时间单调递增(不一定连续),长度更短,用50年都不会超过 js Number类型最大值。(默认配置 WorkerId 是6bit,序列数是6bit) 速度更快,是传统雪花算法的2-5倍,0.1秒可生成50万个。(i7笔记本,默认算法配置6bit+6bit) @@ -102,34 +102,17 @@ QQ群:646049993 🔶 允许时间回拨至本算法预设基数(参数可调)。 -## 💎 配置参数 - - WorkerIdBitLength,决定 WorkerId 的最大值。 - - SeqBitLength,决定每毫秒生成的 ID 个数。 - - WorkerIdBitLength + SeqBitLength 不能超过 22。 - - WorkerId,最大值 2^WorkerIdBitLength-1。 - - 默认配置值: - -``` -WorkerIdBitLength = 6 -SeqBitLength = 6 -``` - ## 💎 ID组成 * 本算法生成的ID由3部分组成(沿用雪花算法定义): * +-------------------------+--------------+----------+ - * | 1.相对基础时间的时间差 | 2.WorkerId | 3.自增数 | + * | 1.相对基础时间的时间差 | 2.WorkerId | 3.序列数 | * +-------------------------+--------------+----------+ * +-------------------------+---- 6 bits ---+- 6 bits -+ * * 第1部分,时间差,是生成ID时的系统时间减去 BaseTime 的总时间差(毫秒单位)。 * 第2部分,WorkerId,是区分不同机器或不同应用的唯一ID,最大值由 WorkerIdBitLength(默认6)限定。 - * 第3部分,自增数,是每毫秒下的自增数,由参数中的 SeqBitLength(默认6)限定。 + * 第3部分,序列数,是每毫秒下的序列数,由参数中的 SeqBitLength(默认6)限定。 ## 💎 ID示例 @@ -163,6 +146,19 @@ SeqBitLength = 6 🔵 在支持 4096 个工作节点时,ID可用 1120 年不重复。 +## 💎 参数设置 + + ***WorkerIdBitLength***,机器码位长,决定 WorkerId 的最大值,默认值6,取值范围 [1, 19],实际上有些语言采用 无符号ushort(uint16) 类型接收该参数,所以最大值是16,如果是采用有符号short(int16),则最大值为15。 + + ***WorkerId***,机器码,无默认值,必须由外部设定,最大值 2^WorkerIdBitLength-1(实际上根据语言的实现不同可能会限定在 65535 或 32767,原理同 WorkerIdBitLength 的规则)。不同机器或不同应用实例不可相同,你可通过应用程序配置该值,也可通过调用外部服务获取值。针对自动注册WorkerId需求,本算法提供默认实现:通过 redis 自动注册 WorkerId 的动态库,详见“Tools\AutoRegisterWorkerId”。 + + ***SeqBitLength***,序列数位长,默认值6,取值范围 [3, 21](建议不小于4),决定每毫秒生成的 ID 个数。规则要求:WorkerIdBitLength + SeqBitLength 不超过 22。 + + ***MinSeqNumber***,最小序列数,默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位。 + + ***MaxSeqNumber***,最大序列数,设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]),不为0时,用该设置值为最大序列数,一般不用设置最大序列数,除非多机共享WorkerId分段生成ID(此时还要正确设置最小序列数)。 + + ## 💎 常规集成 1️⃣ 用单例模式调用。外部集成方使用更多的实例并行调用本算法,不会增加ID产出效能,因为本算法采用单线程模式生成ID。 @@ -195,15 +191,32 @@ SeqBitLength = 6 ## 自动注册WorkerId -🔍 唯一ID生成器,依赖WorkerId,当业务服务需要水平自动化复制时,就要求它能自动化注册全局唯一WorkerId,然后各个容器化的无差别部署的业务服务,才能根据它生产唯一ID。 +🔍 唯一ID生成器,依赖WorkerId,当业务服务需要水平无差别复制时,就要求它能自动注册全局唯一WorkerId,然后才能根据它生产唯一ID。 + +🔍 本算法提供一个开源动态库(go语言实现),能在容器 k8s(或其它容器化集群) 环境下,通过 redis 自动注册 WorkerId。 + +🔍 通过redis注册WorkerId,并不是唯一的方法。你也可以自己开发一个配置中心服务,各个应用服务启动时,通过配置中心获取唯一 WorkerId。 -🔍 本算法提供一个开源的动态库(go语言实现),能在容器 k8s(或其它容器化集群) 环境下,通过 redis 自动注册 WorkerId。动态库提供的C接口方法可参考 -源码文件 [ Tools/AutoRegisterWorkerId/lib/yitidgengo.h ] +🔍 当然,如果你的服务不需要自动扩展,你就不必自动注册WorkerId,而是为每个应用手工设定一个唯一值。 -redis作用 -🔎 在集成“自动注册WorkerId”功能时,用于注册 WorkerId ,不用于生产 ID。 +动态库下载链接:https://gitee.com/yitter/idgenerator/attach_files/662372/download/regworkerid_lib_v1.0.zip +动态库接口定义: +``` +// 注册一个 WorkerId,会先注销所有本机已注册的记录 +// ip: redis 服务器地址 +// port: redis 端口 +// password: redis 访问密码,可为空字符串“” +// maxWorkerId: 最大 WorkerId +extern __declspec(dllexport) GoInt32 RegisterOne(char* ip, GoInt32 port, char* password, GoInt32 maxWorkerId); + +// 注销本机已注册的 WorkerId +extern __declspec(dllexport) void UnRegister(); + +// 检查本地WorkerId是否有效(0-有效,其它-无效) +extern __declspec(dllexport) GoInt32 Validate(GoInt32 workerId); +``` ## 💎 已实现的语言 diff --git a/Rust/README.md b/Rust/README.md index 01ddc83..2767343 100644 --- a/Rust/README.md +++ b/Rust/README.md @@ -1,22 +1,22 @@ -# idgenerator +# idgenerator -## ʾRust +## 调用示例(Rust) -1**ȫ** ʼӦóʱִһΣ +第1步,**全局** 初始化(应用程序启动时执行一次): ``` -// IdGeneratorOptions ڹ캯 WorkerId +// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId: let mut options = IdGeneratorOptions::New(1); -// options.WorkerIdBitLength = 10; // WorkerIdBitLength Ĭֵ6ֵ֧ WorkerId ֵΪ2^6-1 WorkerId 64ø WorkerIdBitLength -// ...... òο IdGeneratorOptions 壬һ˵ֻҪ WorkerIdBitLength WorkerId ֵ +// options.WorkerIdBitLength = 10; // WorkerIdBitLength 默认值6,支持的 WorkerId 最大值为2^6-1,若 WorkerId 超过64,可设置更大的 WorkerIdBitLength +// ...... 其它参数设置参考 IdGeneratorOptions 定义,一般来说,只要再设置 WorkerIdBitLength (决定 WorkerId 的最大值)。 -// IJöЧ +// 保存参数(必须的操作,否则以上设置都不能生效): YitIdHelper::SetIdGenerator(options); -// ϳʼֻȫһΣұڵ2֮ǰá +// 以上初始化过程只需全局一次,且必须在第2步之前设置。 ``` -2ID +第2步,生成ID: ``` -// ʼԺ󣬼κҪIDĵط· +// 初始化以后,即可在任何需要生成ID的地方,调用以下方法: long newId = YitIdHelper::NextId(); ``` diff --git a/Rust/source/Cargo.lock b/Rust/source/Cargo.lock index 33c508a..3d07392 100644 --- a/Rust/source/Cargo.lock +++ b/Rust/source/Cargo.lock @@ -1,29 +1,12 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "async-trait" -version = "0.1.48" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36ea56748e10732c49404c153638a15ec3d6211ec5ff35d9bb20e13b93576adf" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] -name = "bytes" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" - -[[package]] name = "chrono" version = "0.4.19" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -37,73 +20,20 @@ dependencies = [ ] [[package]] -name = "combine" -version = "4.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc4369b5e4c0cddf64ad8981c0111e7df4f7078f4d6ba98fb31f2e17c4c57b7e" -dependencies = [ - "bytes", - "memchr", -] - -[[package]] -name = "dtoa" -version = "0.4.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" - -[[package]] -name = "form_urlencoded" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" -dependencies = [ - "matches", - "percent-encoding", -] - -[[package]] -name = "idna" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89829a5d69c23d348314a7ac337fe39173b61149a9864deabd260983aed48c21" +name = "idgen" +version = "1.0.0" dependencies = [ - "matches", - "unicode-bidi", - "unicode-normalization", + "chrono", + "libc", ] [[package]] -name = "itoa" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" - -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - -[[package]] name = "libc" version = "0.2.90" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba4aede83fc3617411dc6993bc8c70919750c1c257c6ca6a502aed6e0e2394ae" [[package]] -name = "matches" -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" - -[[package]] -name = "memchr" -version = "2.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" - -[[package]] name = "num-integer" version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -123,62 +53,6 @@ dependencies = [ ] [[package]] -name = "percent-encoding" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" - -[[package]] -name = "proc-macro2" -version = "1.0.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" -dependencies = [ - "unicode-xid", -] - -[[package]] -name = "quote" -version = "1.0.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "redis" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eeb8f8d059ead7805e171fc22de8348a3d611c0f985aaa4f5cf6c0dfc7645407" -dependencies = [ - "async-trait", - "combine", - "dtoa", - "itoa", - "percent-encoding", - "sha1", - "url", -] - -[[package]] -name = "sha1" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" - -[[package]] -name = "syn" -version = "1.0.67" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6498a9efc342871f91cc2d0d694c674368b4ceb40f62b65a7a08c3792935e702" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - -[[package]] name = "time" version = "0.1.44" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -190,57 +64,6 @@ dependencies = [ ] [[package]] -name = "tinyvec" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" - -[[package]] -name = "unicode-bidi" -version = "0.3.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" -dependencies = [ - "matches", -] - -[[package]] -name = "unicode-normalization" -version = "0.1.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07fbfce1c8a97d547e8b5334978438d9d6ec8c20e38f56d4a4374d181493eaef" -dependencies = [ - "tinyvec", -] - -[[package]] -name = "unicode-xid" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" - -[[package]] -name = "url" -version = "2.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ccd964113622c8e9322cfac19eb1004a07e636c545f325da085d5cdde6f1f8b" -dependencies = [ - "form_urlencoded", - "idna", - "matches", - "percent-encoding", -] - -[[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -267,13 +90,3 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "yitidgen" -version = "1.0.0" -dependencies = [ - "chrono", - "lazy_static", - "libc", - "redis", -] diff --git a/Rust/source/Cargo.toml b/Rust/source/Cargo.toml index 64350e1..f68b5f8 100644 --- a/Rust/source/Cargo.toml +++ b/Rust/source/Cargo.toml @@ -1,25 +1,22 @@ [package] -name = "yitidgen" +name = "idgen" version = "1.0.0" authors = ["yitter "] edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +license = "MIT" +description="Shorter ID and faster generation with a new snowflake drift algorithm. The core is to shorten the ID length, but also can have a very high instantaneous concurrent processing capacity (50W/0.1s), and powerful configuration capacity." +readme = "README.md" +homepage ="https://github.com/yitter/IdGenerator" +repository = "https://github.com/yitter/IdGenerator" +keywords = ["snowflake", "idgenerator"] +#categories = ["command-line-utilities"] [dependencies] -chrono = "0.4.10" -lazy_static = "1.4.0" -#simple_redis = "*" -redis = "0.20.0" -libc="*" - -#actix = "0.9.0" -#actix-web = "2.0" -#actix-rt = "1.0" -#actix-redis = "0.8.0" -#redis-async = "0.6.1" +libc = "*" +chrono = "0.4.19" +#lazy_static = "1.4.0" [lib] -name = "yitidgen" +name = "idgen" path = "./src/lib.rs" crate-type = ["cdylib"] diff --git a/Rust/source/README.md b/Rust/source/README.md new file mode 100644 index 0000000..2767343 --- /dev/null +++ b/Rust/source/README.md @@ -0,0 +1,23 @@ +# idgenerator + +## 调用示例(Rust) + +第1步,**全局** 初始化(应用程序启动时执行一次): +``` +// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId: +let mut options = IdGeneratorOptions::New(1); +// options.WorkerIdBitLength = 10; // WorkerIdBitLength 默认值6,支持的 WorkerId 最大值为2^6-1,若 WorkerId 超过64,可设置更大的 WorkerIdBitLength +// ...... 其它参数设置参考 IdGeneratorOptions 定义,一般来说,只要再设置 WorkerIdBitLength (决定 WorkerId 的最大值)。 + +// 保存参数(必须的操作,否则以上设置都不能生效): +YitIdHelper::SetIdGenerator(options); +// 以上初始化过程只需全局一次,且必须在第2步之前设置。 +``` + +第2步,生成ID: +``` +// 初始化以后,即可在任何需要生成ID的地方,调用以下方法: +long newId = YitIdHelper::NextId(); +``` + + diff --git a/Rust/source/src/yitgen/gen/default_id_generator.rs b/Rust/source/src/idgen/default_id_generator.rs similarity index 87% rename from Rust/source/src/yitgen/gen/default_id_generator.rs rename to Rust/source/src/idgen/default_id_generator.rs index 50fa71b..925cc25 100644 --- a/Rust/source/src/yitgen/gen/default_id_generator.rs +++ b/Rust/source/src/idgen/default_id_generator.rs @@ -5,12 +5,10 @@ use std::{thread, time}; use std::net::UdpSocket; use chrono::Utc; -use super::super::contract::*; -use super::super::core::*; -use super::*; use std::sync::Mutex; use std::sync::Arc; use std::borrow::BorrowMut; +use crate::idgen::*; // static mut instance2: Option>> = None; diff --git a/Rust/source/src/yitgen/contract/i_snow_worker.rs b/Rust/source/src/idgen/i_snow_worker.rs similarity index 100% rename from Rust/source/src/yitgen/contract/i_snow_worker.rs rename to Rust/source/src/idgen/i_snow_worker.rs diff --git a/Rust/source/src/yitgen/contract/id_generator_options.rs b/Rust/source/src/idgen/id_generator_options.rs similarity index 62% rename from Rust/source/src/yitgen/contract/id_generator_options.rs rename to Rust/source/src/idgen/id_generator_options.rs index f190505..d649b03 100644 --- a/Rust/source/src/yitgen/contract/id_generator_options.rs +++ b/Rust/source/src/idgen/id_generator_options.rs @@ -6,18 +6,25 @@ pub struct IdGeneratorOptions { /// 雪花计算方法,(1-漂移算法|2-传统算法),默认1 pub Method: u8, + /// 基础时间(ms单位),不能超过当前系统时间 pub BaseTime: i64, - /// 机器码,与 WorkerIdBitLength 有关系 + + /// 必须由外部设定,最大值 2^WorkerIdBitLength-1 pub WorkerId: u32, - /// 机器码位长,范围:1-21(要求:序列数位长+机器码位长不超过22) + + /// 默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22) pub WorkerIdBitLength: u8, - /// 序列数位长,范围:2-21(要求:序列数位长+机器码位长不超过22) + + /// 默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22) pub SeqBitLength: u8, - /// 最大序列数(含),(由 SeqBitLength 计算的最大值) + + /// 设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]) pub MaxSeqNumber: u32, - /// 最小序列数(含),默认5,不小于5,不大于 MaxSeqNumber + + /// 默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位 pub MinSeqNumber: u32, + /// 最大漂移次数(含),默认2000,推荐范围 500-20000(与计算能力有关) pub TopOverCostCount: u32, } diff --git a/Rust/source/src/idgen/mod.rs b/Rust/source/src/idgen/mod.rs new file mode 100644 index 0000000..4de1ec3 --- /dev/null +++ b/Rust/source/src/idgen/mod.rs @@ -0,0 +1,18 @@ +mod default_id_generator; +mod yit_id_helper; +mod snow_worker_m1; +mod snow_worker_m2; +mod id_generator_options; +mod i_snow_worker; +mod over_cost_action_arg; + +use snow_worker_m1::SnowWorkerM1; +use snow_worker_m2::SnowWorkerM2; + +pub use over_cost_action_arg::OverCostActionArg; +pub use yit_id_helper::YitIdHelper; +pub use default_id_generator::DefaultIdGenerator; +pub use id_generator_options::IdGeneratorOptions; +pub use i_snow_worker::ISnowWorker; + + diff --git a/Rust/source/src/yitgen/contract/over_cost_action_arg.rs b/Rust/source/src/idgen/over_cost_action_arg.rs similarity index 100% rename from Rust/source/src/yitgen/contract/over_cost_action_arg.rs rename to Rust/source/src/idgen/over_cost_action_arg.rs diff --git a/Rust/source/src/yitgen/core/snow_worker_m1.rs b/Rust/source/src/idgen/snow_worker_m1.rs similarity index 96% rename from Rust/source/src/yitgen/core/snow_worker_m1.rs rename to Rust/source/src/idgen/snow_worker_m1.rs index 716c778..750eaba 100644 --- a/Rust/source/src/yitgen/core/snow_worker_m1.rs +++ b/Rust/source/src/idgen/snow_worker_m1.rs @@ -2,10 +2,10 @@ * 版权属于:yitter(yitter@126.com) * 开源地址:https://gitee.com/yitter/idgenerator */ -use super::super::contract::*; use std::{thread}; use chrono::Utc; use std::thread::sleep; +use crate::idgen::*; // use lazy_static::lazy_static; pub struct SnowWorkerM1 { diff --git a/Rust/source/src/yitgen/core/snow_worker_m2.rs b/Rust/source/src/idgen/snow_worker_m2.rs similarity index 72% rename from Rust/source/src/yitgen/core/snow_worker_m2.rs rename to Rust/source/src/idgen/snow_worker_m2.rs index a9093c1..e645f35 100644 --- a/Rust/source/src/yitgen/core/snow_worker_m2.rs +++ b/Rust/source/src/idgen/snow_worker_m2.rs @@ -2,7 +2,7 @@ * 版权属于:yitter(yitter@126.com) * 开源地址:https://gitee.com/yitter/idgenerator */ -use super::super::contract::ISnowWorker; +use crate::idgen::*; pub struct SnowWorkerM2 { diff --git a/Rust/source/src/yitgen/gen/yit_id_helper.rs b/Rust/source/src/idgen/yit_id_helper.rs similarity index 90% rename from Rust/source/src/yitgen/gen/yit_id_helper.rs rename to Rust/source/src/idgen/yit_id_helper.rs index ae1e5e7..18c3327 100644 --- a/Rust/source/src/yitgen/gen/yit_id_helper.rs +++ b/Rust/source/src/idgen/yit_id_helper.rs @@ -2,11 +2,9 @@ * 版权属于:yitter(yitter@126.com) * 开源地址:https://gitee.com/yitter/idgenerator */ -use super::super::contract::*; -use super::super::core::*; -use super::*; use std::sync::Mutex; use std::sync::Arc; +use crate::idgen::*; pub struct YitIdHelper; diff --git a/Rust/source/src/lib.rs b/Rust/source/src/lib.rs index 63a664e..f3b770d 100644 --- a/Rust/source/src/lib.rs +++ b/Rust/source/src/lib.rs @@ -1,30 +1,19 @@ -mod yitgen; - -use yitgen::gen::YitIdHelper; -use yitgen::contract::*; +mod idgen; #[macro_use] -extern crate lazy_static; -extern crate redis; +// extern crate lazy_static; extern crate libc; -use redis::Commands; - use libc::{c_char, uint32_t}; use std::ffi::{CStr, CString}; use std::str; +pub use idgen::*; -lazy_static! { - //static ref TestValue: Vec = vec!(0); - // static ref MAP: HashMap = HashMap::new(); - } - - -// // #[export_name = "SetIdGenerator"] -// #[no_mangle] -// pub extern "C" fn SetIdGenerator(options: IdGeneratorOptions) { -// YitIdHelper::SetIdGenerator(options); -// } +// #[export_name = "SetIdGenerator"] +#[no_mangle] +pub extern "C" fn SetIdGenerator(options: IdGeneratorOptions) { + YitIdHelper::SetIdGenerator(options); +} #[no_mangle] pub extern "C" fn SetOptions(workerId: u32, workerIdBitLength: u8, seqBitLength: u8) { @@ -44,67 +33,5 @@ pub extern "C" fn NextId() -> i64 { YitIdHelper::NextId() } -// static mut TestValue: i32 = 0; -// #[no_mangle] -// pub extern "C" fn Test() -> i32 { -// unsafe { -// TestValue += 1; -// return TestValue; -// } -// } - -// #[no_mangle] -// pub extern "C" -// fn GetWorkerId(ip: *const c_char, port: i32) -> redis::RedisResult { -// // let c_str = unsafe { -// // assert!(!ip.is_null()); -// // CStr::from_ptr(ip) -// // }; -// // -// // let r_str = c_str.to_str(); -// -// // connect to redis -// // let client = redis::Client::open(format!("redis://{}:{}/", String::from(r_str).to_string(), port))?; -// let client = redis::Client::open(format!("redis://localhost:{}/", port))?; -// -// let mut con = client.get_connection()?; -// // throw away the result, just make sure it does not fail -// unsafe { -// let _: () = con.set("my_key111", TestValue.clone())?; -// } -// con.get("my_key") -// // read back the key and return it. Because the return value -// // from the function is a result for integer this will automatically -// // convert into one. -// // -// -// // match simple_redis::create(&format!("redis://{}:{}/", ip, port)) { -// // Ok(mut client) => { -// // println!("Created Redis Client"); -// // -// // let valueString = TestValue.to_string(); -// // let valueString2 = (*TestValue).to_string(); -// // -// // match client.set("my_key", valueString) { -// // Err(error) => println!("Unable to set value in Redis: {}", error), -// // _ => println!("Value set in Redis") -// // }; -// // -// // match client.set("my_key2", valueString2) { -// // Err(error) => println!("Unable to set value in Redis: {}", error), -// // _ => println!("Value set in Redis") -// // }; -// // -// // match client.quit() { -// // Err(error) => println!("Error: {}", error), -// // _ => println!("Connection Closed.") -// // } -// // } -// // Err(error) => println!("Unable to create Redis client: {}", error) -// // } -// -// //return 1; -// } - // build-win-x64: cargo build --release // build-linux-x64: cargo build --target x86_64-unknown-linux-musl --release diff --git a/Rust/source/src/main.rs b/Rust/source/src/main.rs index 9870a12..70b4e41 100644 --- a/Rust/source/src/main.rs +++ b/Rust/source/src/main.rs @@ -1,7 +1,6 @@ -mod yitgen; +mod idgen; -use yitgen::contract::*; -use yitgen::gen::*; +use idgen::*; use std::thread; use chrono::Utc; use std::time::Duration; diff --git a/Rust/source/src/yitgen/contract/mod.rs b/Rust/source/src/yitgen/contract/mod.rs deleted file mode 100644 index 769f846..0000000 --- a/Rust/source/src/yitgen/contract/mod.rs +++ /dev/null @@ -1,13 +0,0 @@ -/* - * 版权属于:yitter(yitter@126.com) - * 开源地址:https://gitee.com/yitter/idgenerator - */ -mod id_generator_options; -mod i_snow_worker; -mod over_cost_action_arg; - -pub use id_generator_options::IdGeneratorOptions; -pub use i_snow_worker::ISnowWorker; -pub use over_cost_action_arg::OverCostActionArg; - - diff --git a/Rust/source/src/yitgen/core/mod.rs b/Rust/source/src/yitgen/core/mod.rs deleted file mode 100644 index 0dec16d..0000000 --- a/Rust/source/src/yitgen/core/mod.rs +++ /dev/null @@ -1,10 +0,0 @@ -/* - * 版权属于:yitter(yitter@126.com) - * 开源地址:https://gitee.com/yitter/idgenerator - */ -mod snow_worker_m1; -mod snow_worker_m2; - -pub use snow_worker_m1::SnowWorkerM1; -pub use snow_worker_m2::SnowWorkerM2; - diff --git a/Rust/source/src/yitgen/gen/mod.rs b/Rust/source/src/yitgen/gen/mod.rs deleted file mode 100644 index c0d329c..0000000 --- a/Rust/source/src/yitgen/gen/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -/* - * 版权属于:yitter(yitter@126.com) - * 开源地址:https://gitee.com/yitter/idgenerator - */ -mod default_id_generator; -mod yit_id_helper; - -pub use yit_id_helper::YitIdHelper; -pub use default_id_generator::DefaultIdGenerator; diff --git a/Rust/source/src/yitgen/mod.rs b/Rust/source/src/yitgen/mod.rs deleted file mode 100644 index b12d01c..0000000 --- a/Rust/source/src/yitgen/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod contract; -pub mod core; -pub mod gen; diff --git a/Tools/AutoRegisterWorkerId/README.md b/Tools/AutoRegisterWorkerId/README.md index 8e57b14..cc995e9 100644 --- a/Tools/AutoRegisterWorkerId/README.md +++ b/Tools/AutoRegisterWorkerId/README.md @@ -1,16 +1,8 @@ -# idgenerator +# idgenerator -## AutoRegisterWorkerId +## 关于AutoRegisterWorkerId -Զע WorkerId Ĺߡ¼ʹá - - -## 輯 - -ȫִһΣ - -1.Redisӡ - -2.ȡ WorkerIdֵ IdGenerator +自动注册 WorkerId 参考: +https://gitee.com/yitter/idgenerator#%E8%87%AA%E5%8A%A8%E6%B3%A8%E5%86%8Cworkerid