1
0
mirror of synced 2025-12-17 02:28:12 +08:00

26 Commits

Author SHA1 Message Date
yitter
f8cb6b9ac5 auto commit 2021-04-06 21:52:24 +08:00
yitter
71e6657b52 auto commit 2021-04-06 21:45:54 +08:00
yitter
41827bf643 auto commit 2021-04-06 21:42:49 +08:00
yitter
f1c9920413 auto commit 2021-04-06 13:37:35 +08:00
yitter
7515e3e6d5 auto commit 2021-04-06 11:28:08 +08:00
yitter
ae98c7493b auto commit 2021-04-06 11:21:47 +08:00
yitter
d390b17397 auto commit 2021-04-06 11:20:38 +08:00
yitter
506656c1df auto commit 2021-04-06 11:13:35 +08:00
yitter
c1ad6b776a auto commit 2021-04-06 11:12:15 +08:00
yitter
74264f1e58 auto commit 2021-04-06 11:11:32 +08:00
yitter
4535c4ad06 auto commit 2021-04-06 11:11:06 +08:00
yitter
89c6bceea4 auto commit 2021-04-06 11:10:37 +08:00
yitter
5826b6aa1c 修改参数注释 2021-04-06 11:09:49 +08:00
yitter
56af98fcfc auto commit 2021-04-06 11:06:59 +08:00
yitter
2ee5c97b88 auto commit 2021-04-06 11:04:11 +08:00
yitter
bdbfc4eedb auto commit 2021-04-06 11:03:37 +08:00
yitter
b0de7049e3 auto commit 2021-04-06 01:27:27 +08:00
yitter
21963938dc autocommit 2021-04-06 00:27:05 +08:00
yitter
8ef0a9f16a autocommit 2021-04-06 00:19:58 +08:00
yitter
b87af2e8de autocommit 2021-04-06 00:18:12 +08:00
yitter
a15315fb58 autocommit 2021-04-05 23:47:50 +08:00
yitter
52b68c62b1 autocommit 2021-04-05 23:40:50 +08:00
yitter
eb4edad7c6 auto commit 2021-04-05 23:33:06 +08:00
yitter
46328d9eae auto commit 2021-04-05 23:26:12 +08:00
yitter
012d99838b auto commit 2021-04-05 22:46:16 +08:00
yitter
a5958f4453 auto commit 2021-04-05 22:45:43 +08:00
31 changed files with 188 additions and 464 deletions

View File

@@ -1,9 +1,9 @@

## 运行环境
.NET Standard 2.0+
## 引用nuget包
## 引用 nuget
```
<PackageReference Include="Yitter.IdGenerator" Version="1.0.*" />
```

View File

@@ -29,34 +29,31 @@ namespace Yitter.IdGenerator
/// <summary>
/// 机器码
/// WorkerIdBitLength 有关系
/// ushort类型最大值65535如果有更高要求请修改数据类型或联系作者)
/// 必须由外部设定,最大值 2^WorkerIdBitLength-1
/// </summary>
public virtual ushort WorkerId { get; set; } = 0;
/// <summary>
/// 机器码位长
/// 范围1-21(要求:序列数位长+机器码位长不超过22
/// 建议范围6-12。
/// 默认值6取值范围 [1, 15](要求:序列数位长+机器码位长不超过22
/// </summary>
public virtual byte WorkerIdBitLength { get; set; } = 6;//10;
/// <summary>
/// 序列数位长
/// 范围2-21要求序列数位长+机器码位长不超过22
/// 建议范围6-14。
/// 默认值6取值范围 [3, 21](要求:序列数位长+机器码位长不超过22
/// </summary>
public virtual byte SeqBitLength { get; set; } = 6;//10;
/// <summary>
/// 最大序列数(含)
/// 由SeqBitLength计算的最大值
/// 设置范围 [MinSeqNumber, 2^SeqBitLength-1]默认值0表示最大序列数取最大值2^SeqBitLength-1]
/// </summary>
public virtual int MaxSeqNumber { get; set; } = 0;
/// <summary>
/// 最小序列数(含)
/// 默认5不小于5不大于MaxSeqNumber
/// 默认5取值范围 [5, MaxSeqNumber]每毫秒的前5个序列数对应编号0-4是保留位其中1-4是时间回拨相应预留位0是手工新值预留位
/// </summary>
public virtual ushort MinSeqNumber { get; set; } = 5;

View File

@@ -18,7 +18,7 @@
<Copyright>Yitter</Copyright>
<PackageProjectUrl>https://github.com/yitter/idgenerator</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>1.0.11</Version>
<Version>1.0.12</Version>
<PackageReleaseNotes></PackageReleaseNotes>
</PropertyGroup>

View File

@@ -1,4 +1,4 @@
# idgenerator
# idgenerator
## 编译说明

View File

@@ -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;

View File

@@ -1,21 +1,9 @@
# ❄ idenerator-go
# ❄ idenerator-go
## 介绍
项目更多介绍参照https://github.com/yitter/idgenerator
## Go环境
1.SDKgo1.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.SDKgo1.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

View File

@@ -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与计算能力有关
}

View File

@@ -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

View File

@@ -1,4 +1,4 @@

## 运行环境
JDK 1.8+
@@ -8,7 +8,7 @@ JDK 1.8+
<dependency>
<groupId>com.github.yitter</groupId>
<artifactId>yitter-idgenerator</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>
</dependency>
```

View File

@@ -6,7 +6,7 @@
<groupId>com.github.yitter</groupId>
<artifactId>yitter-idgenerator</artifactId>
<version>1.0.5</version>
<version>1.0.6</version>
<packaging>jar</packaging>
<name>yitter-idgenerator</name>

View File

@@ -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;

View File

@@ -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) {

View File

@@ -1,6 +1,6 @@
# 比雪花算法更好用的ID生成算法单机或分布式唯一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
@@ -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由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 年不重复。
## 💎 参数设置
<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 的规则)。不同机器或不同应用不能相同,本算法提供一个通过 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。
@@ -195,10 +191,21 @@ SeqBitLength = 6
## 自动注册WorkerId
🔍 唯一ID生成器依赖WorkerId当业务服务需要水平自动化复制时,就要求它能自动注册全局唯一WorkerId然后各个容器化的无差别部署的业务服务,才能根据它生产唯一ID。
🔍 唯一ID生成器依赖WorkerId当业务服务需要水平无差别复制时就要求它能自动注册全局唯一WorkerId然后才能根据它生产唯一ID。
🔍 本算法提供一个开源的动态库go语言实现能在容器 k8s或其它容器化集群 环境下,通过 redis 自动注册 WorkerId。动态库提供的C接口方法可参考
源码文件 [ Tools/AutoRegisterWorkerId/lib/yitidgengo.h ]
🔍 本算法提供一个开源的动态库go语言实现能在容器 k8s或其它容器化集群 环境下,通过 redis 自动注册 WorkerId。由于动态库文件较大,目前源码中不含编译后的 so linux和 dllwindows文件。
动态库接口定义:
```
// 注册一个 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);
```
redis作用

View File

@@ -1,22 +1,22 @@
# idgenerator
# idgenerator
## <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Rust<EFBFBD><EFBFBD>
## 调用示例(Rust
<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>**ȫ<><C8AB>** <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>Ӧ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱִ<CAB1><D6B4>һ<EFBFBD>Σ<EFBFBD><CEA3><EFBFBD>
第1步**全局** 初始化(应用程序启动时执行一次):
```
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> IdGeneratorOptions <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> WorkerId<EFBFBD><EFBFBD>
// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId
let mut options = IdGeneratorOptions::New(1);
// options.WorkerIdBitLength = 10; // WorkerIdBitLength Ĭ<EFBFBD><EFBFBD>ֵ6<EFBFBD><EFBFBD>֧<EFBFBD>ֵ<EFBFBD> WorkerId <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ2^6-1<EFBFBD><EFBFBD><EFBFBD><EFBFBD> WorkerId <EFBFBD><EFBFBD><EFBFBD><EFBFBD>64<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ø<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> WorkerIdBitLength
// ...... <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>òο<EFBFBD> IdGeneratorOptions <EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD>ֻҪ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> WorkerIdBitLength <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> WorkerId <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// options.WorkerIdBitLength = 10; // WorkerIdBitLength 默认值6支持的 WorkerId 最大值为2^6-1,若 WorkerId 超过64可设置更大的 WorkerIdBitLength
// ...... 其它参数设置参考 IdGeneratorOptions 定义,一般来说,只要再设置 WorkerIdBitLength (决定 WorkerId 的最大值)。
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// 保存参数(必须的操作,否则以上设置都不能生效):
YitIdHelper::SetIdGenerator(options);
// <EFBFBD><EFBFBD><EFBFBD>ϳ<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD>һ<EFBFBD>Σ<EFBFBD><EFBFBD>ұ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>2<EFBFBD><EFBFBD>֮ǰ<EFBFBD><EFBFBD><EFBFBD>á<EFBFBD>
// 以上初始化过程只需全局一次且必须在第2步之前设置。
```
<EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD><EFBFBD>
第2步生成ID
```
// <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>Ժ󣬼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ID<EFBFBD>ĵط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// 初始化以后即可在任何需要生成ID的地方调用以下方法
long newId = YitIdHelper::NextId();
```

195
Rust/source/Cargo.lock generated
View File

@@ -1,28 +1,11 @@
# 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"
@@ -37,72 +20,19 @@ dependencies = [
]
[[package]]
name = "combine"
version = "4.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc4369b5e4c0cddf64ad8981c0111e7df4f7078f4d6ba98fb31f2e17c4c57b7e"
name = "idgen"
version = "1.0.0"
dependencies = [
"bytes",
"memchr",
"chrono",
"libc",
]
[[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"
dependencies = [
"matches",
"unicode-bidi",
"unicode-normalization",
]
[[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"
@@ -122,62 +52,6 @@ dependencies = [
"autocfg",
]
[[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"
@@ -189,57 +63,6 @@ dependencies = [
"winapi",
]
[[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"
@@ -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",
]

View File

@@ -1,25 +1,22 @@
[package]
name = "yitidgen"
name = "idgen"
version = "1.0.0"
authors = ["yitter <yitter@126.com>"]
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"]

23
Rust/source/README.md Normal file
View File

@@ -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();
```

View File

@@ -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<Arc<Mutex<SnowWorkerM1>>> = None;

View File

@@ -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,
}

View File

@@ -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;

View File

@@ -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 {

View File

@@ -2,7 +2,7 @@
* yitter(yitter@126.com)
* https://gitee.com/yitter/idgenerator
*/
use super::super::contract::ISnowWorker;
use crate::idgen::*;
pub struct SnowWorkerM2 {

View File

@@ -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;

View File

@@ -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<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]
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<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-linux-x64: cargo build --target x86_64-unknown-linux-musl --release

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -1,3 +0,0 @@
pub mod contract;
pub mod core;
pub mod gen;