@@ -1,9 +1,9 @@ | |||||
| |||||
## 运行环境 | ## 运行环境 | ||||
.NET Standard 2.0+ | .NET Standard 2.0+ | ||||
## 引用nuget包 | |||||
## 引用 nuget 包 | |||||
``` | ``` | ||||
<PackageReference Include="Yitter.IdGenerator" Version="1.0.*" /> | <PackageReference Include="Yitter.IdGenerator" Version="1.0.*" /> | ||||
``` | ``` | ||||
@@ -29,34 +29,31 @@ namespace Yitter.IdGenerator | |||||
/// <summary> | /// <summary> | ||||
/// 机器码 | /// 机器码 | ||||
/// 与 WorkerIdBitLength 有关系 | |||||
/// (ushort类型,最大值65535,如果有更高要求,请修改数据类型,或联系作者) | |||||
/// 必须由外部设定,最大值 2^WorkerIdBitLength-1 | |||||
/// </summary> | /// </summary> | ||||
public virtual ushort WorkerId { get; set; } = 0; | public virtual ushort WorkerId { get; set; } = 0; | ||||
/// <summary> | /// <summary> | ||||
/// 机器码位长 | /// 机器码位长 | ||||
/// 范围:1-21(要求:序列数位长+机器码位长不超过22)。 | |||||
/// 建议范围:6-12。 | |||||
/// 默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22) | |||||
/// </summary> | /// </summary> | ||||
public virtual byte WorkerIdBitLength { get; set; } = 6;//10; | public virtual byte WorkerIdBitLength { get; set; } = 6;//10; | ||||
/// <summary> | /// <summary> | ||||
/// 序列数位长 | /// 序列数位长 | ||||
/// 范围:2-21(要求:序列数位长+机器码位长不超过22)。 | |||||
/// 建议范围:6-14。 | |||||
/// 默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22) | |||||
/// </summary> | /// </summary> | ||||
public virtual byte SeqBitLength { get; set; } = 6;//10; | public virtual byte SeqBitLength { get; set; } = 6;//10; | ||||
/// <summary> | /// <summary> | ||||
/// 最大序列数(含) | /// 最大序列数(含) | ||||
/// (由SeqBitLength计算的最大值) | |||||
/// 设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]) | |||||
/// </summary> | /// </summary> | ||||
public virtual int MaxSeqNumber { get; set; } = 0; | public virtual int MaxSeqNumber { get; set; } = 0; | ||||
/// <summary> | /// <summary> | ||||
/// 最小序列数(含) | /// 最小序列数(含) | ||||
/// 默认5,不小于5,不大于MaxSeqNumber | |||||
/// 默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位 | |||||
/// </summary> | /// </summary> | ||||
public virtual ushort MinSeqNumber { get; set; } = 5; | public virtual ushort MinSeqNumber { get; set; } = 5; | ||||
@@ -236,6 +236,8 @@ namespace Yitter.IdGenerator | |||||
{ | { | ||||
EndOverCostAction(currentTimeTick); | EndOverCostAction(currentTimeTick); | ||||
// TODO: 在漂移终止,等待时间对齐时,如果发生时间回拨较长,则此处可能等待较长时间。可优化为:在漂移终止时增加时间回拨应对逻辑。(该情况发生概率很低) | |||||
_LastTimeTick = GetNextTimeTick(); | _LastTimeTick = GetNextTimeTick(); | ||||
_CurrentSeqNumber = MinSeqNumber; | _CurrentSeqNumber = MinSeqNumber; | ||||
_IsOverCost = false; | _IsOverCost = false; | ||||
@@ -18,7 +18,7 @@ | |||||
<Copyright>Yitter</Copyright> | <Copyright>Yitter</Copyright> | ||||
<PackageProjectUrl>https://github.com/yitter/idgenerator</PackageProjectUrl> | <PackageProjectUrl>https://github.com/yitter/idgenerator</PackageProjectUrl> | ||||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | <PackageLicenseExpression>MIT</PackageLicenseExpression> | ||||
<Version>1.0.11</Version> | |||||
<Version>1.0.12</Version> | |||||
<PackageReleaseNotes></PackageReleaseNotes> | <PackageReleaseNotes></PackageReleaseNotes> | ||||
</PropertyGroup> | </PropertyGroup> | ||||
@@ -1,4 +1,4 @@ | |||||
# idgenerator | |||||
# idgenerator | |||||
## 编译说明 | ## 编译说明 | ||||
@@ -10,18 +10,25 @@ | |||||
typedef struct IdGenOptions { | typedef struct IdGenOptions { | ||||
/// 雪花计算方法,(1-漂移算法|2-传统算法),默认1 | /// 雪花计算方法,(1-漂移算法|2-传统算法),默认1 | ||||
uint8_t Method; | uint8_t Method; | ||||
/// 基础时间(ms单位),不能超过当前系统时间 | /// 基础时间(ms单位),不能超过当前系统时间 | ||||
uint64_t BaseTime; | uint64_t BaseTime; | ||||
/// 机器码,与 WorkerIdBitLength 有关系 | |||||
/// 机器码,必须由外部设定,最大值 2^WorkerIdBitLength-1 | |||||
uint32_t WorkerId; | uint32_t WorkerId; | ||||
/// 机器码位长,范围:1-21(要求:序列数位长+机器码位长不超过22) | |||||
/// 机器码位长,默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22) | |||||
uint8_t WorkerIdBitLength; | uint8_t WorkerIdBitLength; | ||||
/// 序列数位长,范围:2-21(要求:序列数位长+机器码位长不超过22) | |||||
/// 序列数位长,默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22) | |||||
uint8_t SeqBitLength; | uint8_t SeqBitLength; | ||||
/// 最大序列数(含),(由 SeqBitLength 计算的最大值) | |||||
/// 最大序列数(含),设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]) | |||||
uint32_t MaxSeqNumber; | uint32_t MaxSeqNumber; | ||||
/// 最小序列数(含),默认5,不小于5,不大于 MaxSeqNumber | |||||
/// 最小序列数(含),默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位 | |||||
uint32_t MinSeqNumber; | uint32_t MinSeqNumber; | ||||
/// 最大漂移次数(含),默认2000,推荐范围 500-20000(与计算能力有关) | /// 最大漂移次数(含),默认2000,推荐范围 500-20000(与计算能力有关) | ||||
uint32_t TopOverCostCount; | uint32_t TopOverCostCount; | ||||
@@ -1,21 +1,9 @@ | |||||
# ❄ idenerator-go | |||||
# ❄ idenerator-go | |||||
## 介绍 | ## 介绍 | ||||
项目更多介绍参照:https://github.com/yitter/idgenerator | 项目更多介绍参照: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 | go get -u -v github.com/yitter/idgenerator-go | ||||
``` | ``` | ||||
@@ -40,5 +28,18 @@ idgen.SetIdGenerator(options) | |||||
var newId = idgen.NextId() | 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 | guoyahao | amuluowin | houseme | ||||
@@ -9,11 +9,11 @@ package idgen | |||||
type IdGeneratorOptions struct { | type IdGeneratorOptions struct { | ||||
Method uint16 // 雪花计算方法,(1-漂移算法|2-传统算法),默认1 | Method uint16 // 雪花计算方法,(1-漂移算法|2-传统算法),默认1 | ||||
BaseTime int64 // 基础时间(ms单位),不能超过当前系统时间 | 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(与计算能力有关) | TopOverCostCount uint32 // 最大漂移次数(含),默认2000,推荐范围500-10000(与计算能力有关) | ||||
} | } | ||||
@@ -4,7 +4,6 @@ import ( | |||||
"C" | "C" | ||||
"fmt" | "fmt" | ||||
"time" | "time" | ||||
"unsafe" | |||||
"yitidgen/idgen" | "yitidgen/idgen" | ||||
"yitidgen/regworkerid" | "yitidgen/regworkerid" | ||||
) | ) | ||||
@@ -27,10 +26,11 @@ func RegisterOne(ip *C.char, port int32, password *C.char, maxWorkerId int32) in | |||||
} | } | ||||
// 注册多个 WorkerId,会先注销所有本机已注册的记录 | // 注册多个 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 | // 注销本机已注册的 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 |
@@ -1,4 +1,4 @@ | |||||
| |||||
## 运行环境 | ## 运行环境 | ||||
JDK 1.8+ | JDK 1.8+ | ||||
@@ -8,7 +8,7 @@ JDK 1.8+ | |||||
<dependency> | <dependency> | ||||
<groupId>com.github.yitter</groupId> | <groupId>com.github.yitter</groupId> | ||||
<artifactId>yitter-idgenerator</artifactId> | <artifactId>yitter-idgenerator</artifactId> | ||||
<version>1.0.5</version> | |||||
<version>1.0.6</version> | |||||
</dependency> | </dependency> | ||||
``` | ``` | ||||
@@ -6,7 +6,7 @@ | |||||
<groupId>com.github.yitter</groupId> | <groupId>com.github.yitter</groupId> | ||||
<artifactId>yitter-idgenerator</artifactId> | <artifactId>yitter-idgenerator</artifactId> | ||||
<version>1.0.5</version> | |||||
<version>1.0.6</version> | |||||
<packaging>jar</packaging> | <packaging>jar</packaging> | ||||
<name>yitter-idgenerator</name> | <name>yitter-idgenerator</name> | ||||
@@ -6,7 +6,7 @@ package com.github.yitter.contract; | |||||
/** | /** | ||||
* 雪花算法使用的参数 | * 雪花算法使用的参数 | ||||
* 此处代码不采用 get/set 那种冗长的写法 | |||||
* 参数说明,参考 README.md 的 “配置参数” 章节。 | |||||
*/ | */ | ||||
public class IdGeneratorOptions { | public class IdGeneratorOptions { | ||||
@@ -23,35 +23,32 @@ public class IdGeneratorOptions { | |||||
public long BaseTime = 1582136402000L; | public long BaseTime = 1582136402000L; | ||||
/** | /** | ||||
* 机器码,必须由外部系统设置 | |||||
* 与 WorkerIdBitLength 有关系 | |||||
* (short类型,最大值32766,如果有更高要求,请修改数据类型,或联系作者) | |||||
* 机器码 | |||||
* 必须由外部设定,最大值 2^WorkerIdBitLength-1 | |||||
*/ | */ | ||||
public short WorkerId = 0; | public short WorkerId = 0; | ||||
/** | /** | ||||
* 机器码位长 | * 机器码位长 | ||||
* 范围:1-21(要求:序列数位长+机器码位长不超过22)。 | |||||
* 建议范围:6-12。 | |||||
* 默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22) | |||||
*/ | */ | ||||
public byte WorkerIdBitLength = 6; | public byte WorkerIdBitLength = 6; | ||||
/** | /** | ||||
* 序列数位长 | * 序列数位长 | ||||
* 范围:2-21(要求:序列数位长+机器码位长不超过22)。 | |||||
* 建议范围:6-14。 | |||||
* 默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22) | |||||
*/ | */ | ||||
public byte SeqBitLength = 6; | public byte SeqBitLength = 6; | ||||
/** | /** | ||||
* 最大序列数(含) | * 最大序列数(含) | ||||
* (由SeqBitLength计算的最大值) | |||||
* 设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]) | |||||
*/ | */ | ||||
public short MaxSeqNumber = 0; | public short MaxSeqNumber = 0; | ||||
/** | /** | ||||
* 最小序列数(含) | * 最小序列数(含) | ||||
* 默认5,不小于5,不大于MaxSeqNumber | |||||
* 默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号是0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位 | |||||
*/ | */ | ||||
public short MinSeqNumber = 5; | public short MinSeqNumber = 5; | ||||
@@ -76,49 +76,17 @@ public class SnowWorkerM1 implements ISnowWorker { | |||||
} | } | ||||
private void BeginOverCostAction(long useTimeTick) { | private void BeginOverCostAction(long useTimeTick) { | ||||
// if (GenAction == null) { | |||||
// return; | |||||
// } | |||||
// | |||||
// DoGenIdAction(new OverCostActionArg( | |||||
// WorkerId, | |||||
// useTimeTick, | |||||
// 1, | |||||
// _OverCostCountInOneTerm, | |||||
// _GenCountInOneTerm, | |||||
// _TermIndex)); | |||||
} | } | ||||
private void EndOverCostAction(long useTimeTick) { | private void EndOverCostAction(long useTimeTick) { | ||||
if (_TermIndex > 10000) { | if (_TermIndex > 10000) { | ||||
_TermIndex = 0; | _TermIndex = 0; | ||||
} | } | ||||
// | |||||
// if (GenAction == null) { | |||||
// return; | |||||
// } | |||||
// | |||||
// DoGenIdAction(new OverCostActionArg( | |||||
// WorkerId, | |||||
// useTimeTick, | |||||
// 2, | |||||
// _OverCostCountInOneTerm, | |||||
// _GenCountInOneTerm, | |||||
// _TermIndex)); | |||||
} | } | ||||
private void BeginTurnBackAction(long useTimeTick) { | private void BeginTurnBackAction(long useTimeTick) { | ||||
// if (GenAction == null) { | |||||
// return; | |||||
// } | |||||
// | |||||
// DoGenIdAction(new OverCostActionArg( | |||||
// WorkerId, | |||||
// useTimeTick, | |||||
// 8, | |||||
// _OverCostCountInOneTerm, | |||||
// _GenCountInOneTerm, | |||||
// _TermIndex)); | |||||
} | } | ||||
private void EndTurnBackAction(long useTimeTick) { | private void EndTurnBackAction(long useTimeTick) { | ||||
@@ -1,6 +1,6 @@ | |||||
# 比雪花算法更好用的ID生成算法(单机或分布式唯一ID) | |||||
# 比雪花算法更好用的数据主键新算法 | |||||
## 💎 算法介绍 | |||||
## 💎 基本介绍 | |||||
<font color="#11aaff" size="5">❄</font> 一个全新的雪花漂移算法,生成的ID更短、速度更快。 | <font color="#11aaff" size="5">❄</font> 一个全新的雪花漂移算法,生成的ID更短、速度更快。 | ||||
@@ -59,7 +59,7 @@ QQ群:646049993 | |||||
## 新算法特点 | ## 新算法特点 | ||||
<font color="green" size="5">✔</font> 整形数字,随时间单调递增(不一定连续),长度更短,用50年都不会超过 js Number类型最大值。(默认配置 WorkerId 是6bit,自增数是6bit) | |||||
<font color="green" size="5">✔</font> 整形数字,随时间单调递增(不一定连续),长度更短,用50年都不会超过 js Number类型最大值。(默认配置 WorkerId 是6bit,序列数是6bit) | |||||
<font color="green" size="5">✔</font> 速度更快,是传统雪花算法的2-5倍,0.1秒可生成50万个。(i7笔记本,默认算法配置6bit+6bit) | <font color="green" size="5">✔</font> 速度更快,是传统雪花算法的2-5倍,0.1秒可生成50万个。(i7笔记本,默认算法配置6bit+6bit) | ||||
@@ -102,34 +102,17 @@ QQ群:646049993 | |||||
🔶 允许时间回拨至本算法预设基数(参数可调)。 | 🔶 允许时间回拨至本算法预设基数(参数可调)。 | ||||
## 💎 配置参数 | |||||
<font color="#11aaff" size="5">❄</font> WorkerIdBitLength,决定 WorkerId 的最大值。 | |||||
<font color="#11aaff" size="5">❄</font> SeqBitLength,决定每毫秒生成的 ID 个数。 | |||||
<font color="#11aaff" size="5">❄</font> WorkerIdBitLength + SeqBitLength 不能超过 22。 | |||||
<font color="#11aaff" size="5">❄</font> WorkerId,最大值 2^WorkerIdBitLength-1。 | |||||
<font color="#11aaff" size="5">❄</font> 默认配置值: | |||||
``` | |||||
WorkerIdBitLength = 6 | |||||
SeqBitLength = 6 | |||||
``` | |||||
## 💎 ID组成 | ## 💎 ID组成 | ||||
* 本算法生成的ID由3部分组成(沿用雪花算法定义): | * 本算法生成的ID由3部分组成(沿用雪花算法定义): | ||||
* +-------------------------+--------------+----------+ | * +-------------------------+--------------+----------+ | ||||
* | 1.相对基础时间的时间差 | 2.WorkerId | 3.自增数 | | |||||
* | 1.相对基础时间的时间差 | 2.WorkerId | 3.序列数 | | |||||
* +-------------------------+--------------+----------+ | * +-------------------------+--------------+----------+ | ||||
* +-------------------------+---- 6 bits ---+- 6 bits -+ | * +-------------------------+---- 6 bits ---+- 6 bits -+ | ||||
* | * | ||||
* 第1部分,时间差,是生成ID时的系统时间减去 BaseTime 的总时间差(毫秒单位)。 | * 第1部分,时间差,是生成ID时的系统时间减去 BaseTime 的总时间差(毫秒单位)。 | ||||
* 第2部分,WorkerId,是区分不同机器或不同应用的唯一ID,最大值由 WorkerIdBitLength(默认6)限定。 | * 第2部分,WorkerId,是区分不同机器或不同应用的唯一ID,最大值由 WorkerIdBitLength(默认6)限定。 | ||||
* 第3部分,自增数,是每毫秒下的自增数,由参数中的 SeqBitLength(默认6)限定。 | |||||
* 第3部分,序列数,是每毫秒下的序列数,由参数中的 SeqBitLength(默认6)限定。 | |||||
## 💎 ID示例 | ## 💎 ID示例 | ||||
@@ -163,6 +146,19 @@ SeqBitLength = 6 | |||||
🔵 在支持 4096 个工作节点时,ID可用 1120 年不重复。 | 🔵 在支持 4096 个工作节点时,ID可用 1120 年不重复。 | ||||
## 💎 参数设置 | |||||
<font color="#11aaff" size="5">❄</font> <font color=blue>***WorkerIdBitLength***</font>,机器码位长,决定 WorkerId 的最大值,默认值6,取值范围 [1, 19],实际上有些语言采用 无符号ushort(uint16) 类型接收该参数,所以最大值是16,如果是采用有符号short(int16),则最大值为15。 | |||||
<font color="#11aaff" size="5">❄</font> <font color=blue>***WorkerId***</font>,机器码,无默认值,必须由外部设定,最大值 2^WorkerIdBitLength-1(实际上根据语言的实现不同可能会限定在 65535 或 32767,原理同 WorkerIdBitLength 的规则)。不同机器或不同应用实例不可相同,你可通过应用程序配置该值,也可通过调用外部服务获取值。针对自动注册WorkerId需求,本算法提供默认实现:通过 redis 自动注册 WorkerId 的动态库,详见“Tools\AutoRegisterWorkerId”。 | |||||
<font color="#11aaff" size="5">❄</font> <font color=blue>***SeqBitLength***</font>,序列数位长,默认值6,取值范围 [3, 21](建议不小于4),决定每毫秒生成的 ID 个数。规则要求:WorkerIdBitLength + SeqBitLength 不超过 22。 | |||||
<font color="#11aaff" size="5">❄</font> <font color=blue>***MinSeqNumber***</font>,最小序列数,默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位。 | |||||
<font color="#11aaff" size="5">❄</font> <font color=blue>***MaxSeqNumber***</font>,最大序列数,设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]),不为0时,用该设置值为最大序列数,一般不用设置最大序列数,除非多机共享WorkerId分段生成ID(此时还要正确设置最小序列数)。 | |||||
## 💎 常规集成 | ## 💎 常规集成 | ||||
1️⃣ 用单例模式调用。外部集成方使用更多的实例并行调用本算法,不会增加ID产出效能,因为本算法采用单线程模式生成ID。 | 1️⃣ 用单例模式调用。外部集成方使用更多的实例并行调用本算法,不会增加ID产出效能,因为本算法采用单线程模式生成ID。 | ||||
@@ -195,15 +191,32 @@ SeqBitLength = 6 | |||||
## 自动注册WorkerId | ## 自动注册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); | |||||
``` | |||||
## 💎 已实现的语言 | ## 💎 已实现的语言 | ||||
@@ -1,22 +1,22 @@ | |||||
# idgenerator | |||||
锘�# idgenerator | |||||
## 调用示例(Rust) | |||||
## 璋冪敤绀轰緥锛圧ust锛� | |||||
第1步,**全局** 初始化(应用程序启动时执行一次): | |||||
绗�1姝ワ紝**鍏ㄥ眬** 鍒濆�鍖栵紙搴旂敤绋嬪簭鍚�姩鏃舵墽琛屼竴娆★級锛� | |||||
``` | ``` | ||||
// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId: | |||||
// 鍒涘缓 IdGeneratorOptions 瀵硅薄锛岃�鍦ㄦ瀯閫犲嚱鏁颁腑杈撳叆 WorkerId锛� | |||||
let mut options = IdGeneratorOptions::New(1); | 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 鐨勬渶澶у€硷級銆� | |||||
// 保存参数(必须的操作,否则以上设置都不能生效): | |||||
// 淇濆瓨鍙傛暟锛堝繀椤荤殑鎿嶄綔锛屽惁鍒欎互涓婅�缃�兘涓嶈兘鐢熸晥锛夛細 | |||||
YitIdHelper::SetIdGenerator(options); | YitIdHelper::SetIdGenerator(options); | ||||
// 以上初始化过程只需全局一次,且必须在第2步之前设置。 | |||||
// 浠ヤ笂鍒濆�鍖栬繃绋嬪彧闇€鍏ㄥ眬涓€娆★紝涓斿繀椤诲湪绗�2姝ヤ箣鍓嶈�缃�€� | |||||
``` | ``` | ||||
第2步,生成ID: | |||||
绗�2姝ワ紝鐢熸垚ID锛� | |||||
``` | ``` | ||||
// 初始化以后,即可在任何需要生成ID的地方,调用以下方法: | |||||
// 鍒濆�鍖栦互鍚庯紝鍗冲彲鍦ㄤ换浣曢渶瑕佺敓鎴怚D鐨勫湴鏂癸紝璋冪敤浠ヤ笅鏂规硶锛� | |||||
long newId = YitIdHelper::NextId(); | long newId = YitIdHelper::NextId(); | ||||
``` | ``` | ||||
@@ -1,29 +1,12 @@ | |||||
# This file is automatically @generated by Cargo. | # This file is automatically @generated by Cargo. | ||||
# It is not intended for manual editing. | # It is not intended for manual editing. | ||||
[[package]] | [[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" | name = "autocfg" | ||||
version = "1.0.1" | version = "1.0.1" | ||||
source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" | ||||
[[package]] | [[package]] | ||||
name = "bytes" | |||||
version = "1.0.1" | |||||
source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" | |||||
[[package]] | |||||
name = "chrono" | name = "chrono" | ||||
version = "0.4.19" | version = "0.4.19" | ||||
source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||
@@ -37,73 +20,20 @@ dependencies = [ | |||||
] | ] | ||||
[[package]] | [[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 = [ | dependencies = [ | ||||
"matches", | |||||
"unicode-bidi", | |||||
"unicode-normalization", | |||||
"chrono", | |||||
"libc", | |||||
] | ] | ||||
[[package]] | [[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" | name = "libc" | ||||
version = "0.2.90" | version = "0.2.90" | ||||
source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||
checksum = "ba4aede83fc3617411dc6993bc8c70919750c1c257c6ca6a502aed6e0e2394ae" | checksum = "ba4aede83fc3617411dc6993bc8c70919750c1c257c6ca6a502aed6e0e2394ae" | ||||
[[package]] | [[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" | name = "num-integer" | ||||
version = "0.1.44" | version = "0.1.44" | ||||
source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||
@@ -123,62 +53,6 @@ dependencies = [ | |||||
] | ] | ||||
[[package]] | [[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" | name = "time" | ||||
version = "0.1.44" | version = "0.1.44" | ||||
source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||
@@ -190,57 +64,6 @@ dependencies = [ | |||||
] | ] | ||||
[[package]] | [[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" | name = "wasi" | ||||
version = "0.10.0+wasi-snapshot-preview1" | version = "0.10.0+wasi-snapshot-preview1" | ||||
source = "registry+https://github.com/rust-lang/crates.io-index" | 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" | version = "0.4.0" | ||||
source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" | ||||
[[package]] | |||||
name = "yitidgen" | |||||
version = "1.0.0" | |||||
dependencies = [ | |||||
"chrono", | |||||
"lazy_static", | |||||
"libc", | |||||
"redis", | |||||
] |
@@ -1,25 +1,22 @@ | |||||
[package] | [package] | ||||
name = "yitidgen" | |||||
name = "idgen" | |||||
version = "1.0.0" | version = "1.0.0" | ||||
authors = ["yitter <yitter@126.com>"] | authors = ["yitter <yitter@126.com>"] | ||||
edition = "2018" | 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] | [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] | [lib] | ||||
name = "yitidgen" | |||||
name = "idgen" | |||||
path = "./src/lib.rs" | path = "./src/lib.rs" | ||||
crate-type = ["cdylib"] | crate-type = ["cdylib"] |
@@ -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(); | |||||
``` | |||||
@@ -5,12 +5,10 @@ | |||||
use std::{thread, time}; | use std::{thread, time}; | ||||
use std::net::UdpSocket; | use std::net::UdpSocket; | ||||
use chrono::Utc; | use chrono::Utc; | ||||
use super::super::contract::*; | |||||
use super::super::core::*; | |||||
use super::*; | |||||
use std::sync::Mutex; | use std::sync::Mutex; | ||||
use std::sync::Arc; | use std::sync::Arc; | ||||
use std::borrow::BorrowMut; | use std::borrow::BorrowMut; | ||||
use crate::idgen::*; | |||||
// static mut instance2: Option<Arc<Mutex<SnowWorkerM1>>> = None; | // static mut instance2: Option<Arc<Mutex<SnowWorkerM1>>> = None; | ||||
@@ -6,18 +6,25 @@ | |||||
pub struct IdGeneratorOptions { | pub struct IdGeneratorOptions { | ||||
/// 雪花计算方法,(1-漂移算法|2-传统算法),默认1 | /// 雪花计算方法,(1-漂移算法|2-传统算法),默认1 | ||||
pub Method: u8, | pub Method: u8, | ||||
/// 基础时间(ms单位),不能超过当前系统时间 | /// 基础时间(ms单位),不能超过当前系统时间 | ||||
pub BaseTime: i64, | pub BaseTime: i64, | ||||
/// 机器码,与 WorkerIdBitLength 有关系 | |||||
/// 必须由外部设定,最大值 2^WorkerIdBitLength-1 | |||||
pub WorkerId: u32, | pub WorkerId: u32, | ||||
/// 机器码位长,范围:1-21(要求:序列数位长+机器码位长不超过22) | |||||
/// 默认值6,取值范围 [1, 15](要求:序列数位长+机器码位长不超过22) | |||||
pub WorkerIdBitLength: u8, | pub WorkerIdBitLength: u8, | ||||
/// 序列数位长,范围:2-21(要求:序列数位长+机器码位长不超过22) | |||||
/// 默认值6,取值范围 [3, 21](要求:序列数位长+机器码位长不超过22) | |||||
pub SeqBitLength: u8, | pub SeqBitLength: u8, | ||||
/// 最大序列数(含),(由 SeqBitLength 计算的最大值) | |||||
/// 设置范围 [MinSeqNumber, 2^SeqBitLength-1],默认值0,表示最大序列数取最大值(2^SeqBitLength-1]) | |||||
pub MaxSeqNumber: u32, | pub MaxSeqNumber: u32, | ||||
/// 最小序列数(含),默认5,不小于5,不大于 MaxSeqNumber | |||||
/// 默认值5,取值范围 [5, MaxSeqNumber],每毫秒的前5个序列数对应编号0-4是保留位,其中1-4是时间回拨相应预留位,0是手工新值预留位 | |||||
pub MinSeqNumber: u32, | pub MinSeqNumber: u32, | ||||
/// 最大漂移次数(含),默认2000,推荐范围 500-20000(与计算能力有关) | /// 最大漂移次数(含),默认2000,推荐范围 500-20000(与计算能力有关) | ||||
pub TopOverCostCount: u32, | pub TopOverCostCount: u32, | ||||
} | } |
@@ -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; | |||||
@@ -2,10 +2,10 @@ | |||||
* 版权属于:yitter(yitter@126.com) | * 版权属于:yitter(yitter@126.com) | ||||
* 开源地址:https://gitee.com/yitter/idgenerator | * 开源地址:https://gitee.com/yitter/idgenerator | ||||
*/ | */ | ||||
use super::super::contract::*; | |||||
use std::{thread}; | use std::{thread}; | ||||
use chrono::Utc; | use chrono::Utc; | ||||
use std::thread::sleep; | use std::thread::sleep; | ||||
use crate::idgen::*; | |||||
// use lazy_static::lazy_static; | // use lazy_static::lazy_static; | ||||
pub struct SnowWorkerM1 { | pub struct SnowWorkerM1 { |
@@ -2,7 +2,7 @@ | |||||
* 版权属于:yitter(yitter@126.com) | * 版权属于:yitter(yitter@126.com) | ||||
* 开源地址:https://gitee.com/yitter/idgenerator | * 开源地址:https://gitee.com/yitter/idgenerator | ||||
*/ | */ | ||||
use super::super::contract::ISnowWorker; | |||||
use crate::idgen::*; | |||||
pub struct SnowWorkerM2 { | pub struct SnowWorkerM2 { | ||||
@@ -2,11 +2,9 @@ | |||||
* 版权属于:yitter(yitter@126.com) | * 版权属于:yitter(yitter@126.com) | ||||
* 开源地址:https://gitee.com/yitter/idgenerator | * 开源地址:https://gitee.com/yitter/idgenerator | ||||
*/ | */ | ||||
use super::super::contract::*; | |||||
use super::super::core::*; | |||||
use super::*; | |||||
use std::sync::Mutex; | use std::sync::Mutex; | ||||
use std::sync::Arc; | use std::sync::Arc; | ||||
use crate::idgen::*; | |||||
pub struct YitIdHelper; | pub struct YitIdHelper; | ||||
@@ -1,30 +1,19 @@ | |||||
mod yitgen; | |||||
use yitgen::gen::YitIdHelper; | |||||
use yitgen::contract::*; | |||||
mod idgen; | |||||
#[macro_use] | #[macro_use] | ||||
extern crate lazy_static; | |||||
extern crate redis; | |||||
// extern crate lazy_static; | |||||
extern crate libc; | extern crate libc; | ||||
use redis::Commands; | |||||
use libc::{c_char, uint32_t}; | use libc::{c_char, uint32_t}; | ||||
use std::ffi::{CStr, CString}; | use std::ffi::{CStr, CString}; | ||||
use std::str; | use std::str; | ||||
pub use idgen::*; | |||||
lazy_static! { | |||||
//static ref TestValue: Vec<i32> = vec!(0); | |||||
// static ref MAP: HashMap<u32, String> = 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] | #[no_mangle] | ||||
pub extern "C" fn SetOptions(workerId: u32, workerIdBitLength: u8, seqBitLength: u8) { | pub extern "C" fn SetOptions(workerId: u32, workerIdBitLength: u8, seqBitLength: u8) { | ||||
@@ -44,67 +33,5 @@ pub extern "C" fn NextId() -> i64 { | |||||
YitIdHelper::NextId() | 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<isize> { | |||||
// // 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-win-x64: cargo build --release | ||||
// build-linux-x64: cargo build --target x86_64-unknown-linux-musl --release | // build-linux-x64: cargo build --target x86_64-unknown-linux-musl --release |
@@ -1,7 +1,6 @@ | |||||
mod yitgen; | |||||
mod idgen; | |||||
use yitgen::contract::*; | |||||
use yitgen::gen::*; | |||||
use idgen::*; | |||||
use std::thread; | use std::thread; | ||||
use chrono::Utc; | use chrono::Utc; | ||||
use std::time::Duration; | use std::time::Duration; | ||||
@@ -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; | |||||
@@ -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; | |||||
@@ -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; |
@@ -1,3 +0,0 @@ | |||||
pub mod contract; | |||||
pub mod core; | |||||
pub mod gen; |
@@ -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 | |||||