Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
yitter
雪花ID全家桶(SnowFlake IdGenerator)
提交
db63286b
雪
雪花ID全家桶(SnowFlake IdGenerator)
项目概览
yitter
/
雪花ID全家桶(SnowFlake IdGenerator)
11 个月 前同步成功
通知
201
Star
3
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
雪
雪花ID全家桶(SnowFlake IdGenerator)
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
db63286b
编写于
11月 01, 2021
作者:
当代码农
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'yitter-master' into dev
上级
29c69b4c
ab4ebd19
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
197 addition
and
9 deletion
+197
-9
Java/source/pom.xml
Java/source/pom.xml
+14
-1
Java/source/src/test/java/com/github/yitter/test/GenTest.java
.../source/src/test/java/com/github/yitter/test/GenTest.java
+0
-2
Java/source/src/test/java/com/github/yitter/test/README.md
Java/source/src/test/java/com/github/yitter/test/README.md
+7
-0
Java/source/src/test/java/com/github/yitter/test/StartUp.java
.../source/src/test/java/com/github/yitter/test/StartUp.java
+0
-2
Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java
...urce/src/test/java/com/github/yitter/test/StartUpJmh.java
+62
-0
Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java
...rce/src/test/java/com/github/yitter/test/StartUpJmh2.java
+63
-0
README.md
README.md
+4
-4
TypeScript/README.md
TypeScript/README.md
+47
-0
未找到文件。
Java/source/pom.xml
浏览文件 @
db63286b
...
...
@@ -45,7 +45,20 @@
<maven.compiler.target>
1.8
</maven.compiler.target>
<java.version>
1.8
</java.version>
</properties>
<dependencies>
<dependency>
<groupId>
org.openjdk.jmh
</groupId>
<artifactId>
jmh-core
</artifactId>
<version>
1.21
</version>
<scope>
provided
</scope>
</dependency>
<dependency>
<groupId>
org.openjdk.jmh
</groupId>
<artifactId>
jmh-generator-annprocess
</artifactId>
<version>
1.21
</version>
<scope>
provided
</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
...
...
Java/source/src/
main
/java/com/github/yitter/test/GenTest.java
→
Java/source/src/
test
/java/com/github/yitter/test/GenTest.java
浏览文件 @
db63286b
package
com.github.yitter.test
;
import
com.github.yitter.contract.IIdGenerator
;
import
com.github.yitter.idgen.YitIdHelper
;
import
java.util.HashSet
;
import
java.util.Set
;
...
...
Java/source/src/test/java/com/github/yitter/test/README.md
0 → 100644
浏览文件 @
db63286b
JMH 基准测试 漂移算法 macbook pro 13(2020) i5 jdk1.8.0_301
```
Benchmark Mode Cnt Score Error Units
StartUpJmh.testGetPojo thrpt 40 58835.536 ± 535.605 ops/s
StartUpJmh.testGetPojo avgt 40 ≈ 10⁻⁵ s/op
```
Java/source/src/
main
/java/com/github/yitter/test/StartUp.java
→
Java/source/src/
test
/java/com/github/yitter/test/StartUp.java
浏览文件 @
db63286b
package
com.github.yitter.test
;
import
com.github.yitter.contract.IIdGenerator
;
import
com.github.yitter.contract.IdGeneratorOptions
;
import
com.github.yitter.idgen.DefaultIdGenerator
;
import
com.github.yitter.idgen.YitIdHelper
;
public
class
StartUp
{
...
...
Java/source/src/test/java/com/github/yitter/test/StartUpJmh.java
0 → 100644
浏览文件 @
db63286b
package
com.github.yitter.test
;
import
com.github.yitter.contract.IdGeneratorOptions
;
import
com.github.yitter.idgen.YitIdHelper
;
import
java.util.concurrent.TimeUnit
;
import
org.openjdk.jmh.annotations.Benchmark
;
import
org.openjdk.jmh.annotations.BenchmarkMode
;
import
org.openjdk.jmh.annotations.Level
;
import
org.openjdk.jmh.annotations.Mode
;
import
org.openjdk.jmh.annotations.OutputTimeUnit
;
import
org.openjdk.jmh.annotations.Scope
;
import
org.openjdk.jmh.annotations.Setup
;
import
org.openjdk.jmh.annotations.State
;
import
org.openjdk.jmh.annotations.Threads
;
import
org.openjdk.jmh.runner.Runner
;
import
org.openjdk.jmh.runner.RunnerException
;
import
org.openjdk.jmh.runner.options.Options
;
import
org.openjdk.jmh.runner.options.OptionsBuilder
;
/**
* @author suzhenyu
* @date 2021/9/27
*/
// 测试方法平均执行时间
@BenchmarkMode
({
Mode
.
All
})
// 输出结果的时间粒度为微秒
@OutputTimeUnit
(
TimeUnit
.
MILLISECONDS
)
@State
(
Scope
.
Thread
)
//@Threads(2)
public
class
StartUpJmh
{
//1-漂移算法,2-传统算法
final
static
short
method
=
1
;
public
static
void
main
(
String
[]
args
)
throws
RunnerException
{
Options
options
=
new
OptionsBuilder
().
include
(
StartUpJmh
.
class
.
getSimpleName
())
.
warmupIterations
(
1
).
measurementIterations
(
5
).
forks
(
1
).
build
();
new
Runner
(
options
).
run
();
}
/**
* setup初始化容器的时候只执行一次
*/
@Setup
(
Level
.
Trial
)
public
void
init
()
{
IdGeneratorOptions
options
=
new
IdGeneratorOptions
();
options
.
WorkerIdBitLength
=
6
;
options
.
SeqBitLength
=
10
;
options
.
BaseTime
=
System
.
currentTimeMillis
();
options
.
Method
=
method
;
options
.
WorkerId
=
1
;
// 首先测试一下 IdHelper 方法,获取单个Id
YitIdHelper
.
setIdGenerator
(
options
);
YitIdHelper
.
nextId
();
}
@Benchmark
public
void
testNextId
()
{
YitIdHelper
.
nextId
();
}
}
Java/source/src/test/java/com/github/yitter/test/StartUpJmh2.java
0 → 100644
浏览文件 @
db63286b
package
com.github.yitter.test
;
import
com.github.yitter.contract.IdGeneratorOptions
;
import
com.github.yitter.idgen.YitIdHelper
;
import
java.util.concurrent.TimeUnit
;
import
org.openjdk.jmh.annotations.Benchmark
;
import
org.openjdk.jmh.annotations.BenchmarkMode
;
import
org.openjdk.jmh.annotations.Level
;
import
org.openjdk.jmh.annotations.Mode
;
import
org.openjdk.jmh.annotations.OutputTimeUnit
;
import
org.openjdk.jmh.annotations.Scope
;
import
org.openjdk.jmh.annotations.Setup
;
import
org.openjdk.jmh.annotations.State
;
import
org.openjdk.jmh.annotations.Threads
;
import
org.openjdk.jmh.runner.Runner
;
import
org.openjdk.jmh.runner.RunnerException
;
import
org.openjdk.jmh.runner.options.Options
;
import
org.openjdk.jmh.runner.options.OptionsBuilder
;
/**
* @author suzhenyu
* @date 2021/9/27
*/
// 测试方法平均执行时间
@BenchmarkMode
({
Mode
.
All
})
// 输出结果的时间粒度为微秒
@OutputTimeUnit
(
TimeUnit
.
MILLISECONDS
)
@State
(
Scope
.
Thread
)
//@Threads(2)
public
class
StartUpJmh2
{
//1-漂移算法,2-传统算法
final
static
short
method
=
2
;
public
static
void
main
(
String
[]
args
)
throws
RunnerException
{
Options
options
=
new
OptionsBuilder
().
include
(
StartUpJmh2
.
class
.
getName
()
+
".*"
)
.
warmupIterations
(
1
).
measurementIterations
(
5
).
forks
(
2
).
build
();
new
Runner
(
options
).
run
();
}
/**
* setup初始化容器的时候只执行一次
*/
@Setup
(
Level
.
Trial
)
public
void
init
()
{
IdGeneratorOptions
options
=
new
IdGeneratorOptions
();
options
.
WorkerIdBitLength
=
6
;
options
.
SeqBitLength
=
10
;
options
.
BaseTime
=
System
.
currentTimeMillis
();
options
.
Method
=
method
;
options
.
WorkerId
=
1
;
// 首先测试一下 IdHelper 方法,获取单个Id
YitIdHelper
.
setIdGenerator
(
options
);
YitIdHelper
.
nextId
();
}
@Benchmark
public
void
testGetPojo
()
{
YitIdHelper
.
nextId
();
}
}
README.md
浏览文件 @
db63286b
...
...
@@ -17,7 +17,7 @@ QQ群:646049993
❄ 兼容所有雪花算法(号段模式或经典模式,大厂或小厂),将来你可做任意的升级切换。(一般无须升级,但理论上支持)
❄ 这是计算机历史上最全面的雪花ID生成工具
,期待你来超越😀
❄ 这是计算机历史上最全面的雪花ID生成工具
。
#### 需求来源
...
...
@@ -108,7 +108,7 @@ QQ群:646049993
387750301904971 (运行3年)
646093214093387 (运行5年)
1292658282840139 (运行10年)
9007199254740992 (js Number 最大值)
9007199254740992 (js Number 最大值
,可以支撑70年
)
165399880288699493 (普通雪花算法生成的ID)
```
...
...
@@ -160,7 +160,7 @@ QQ群:646049993
第二版增加参数(非必须):
❄
***DataCenterId**
*
,数据中心ID(默认0),请确保全局唯一。
❄
***DataCenterId**
*
,数据中心ID(
机房ID,
默认0),请确保全局唯一。
❄
***DataCenterIdBitLength**
*
,数据中心ID长度(默认0)。
...
...
@@ -211,7 +211,7 @@ QQ群:646049993
🔍 当然,如果你的服务无需自动扩容,那就不必自动注册WorkerId,而是为它们分别设置全局唯一值。
🔍
发挥你的想象力,方法还有很多。此处抛砖引玉
:开发中心化的ID生成服务,由它为各端点服务(单个或批量)生成可用ID。
🔍
方法还有很多,例如
:开发中心化的ID生成服务,由它为各端点服务(单个或批量)生成可用ID。
#### 自动注册流程图
...
...
TypeScript/README.md
浏览文件 @
db63286b
...
...
@@ -168,6 +168,53 @@ $ ts-node test/test4.ts
8
ID
:
30043877339570182
bigint
长度
:
17
9
ID
:
30043877339570183
bigint
长度
:
17
<<<<<<<
HEAD
=======
```
## 同时兼容number和bigint的写法
如果您觉得这个用法更好,可以手动替换对应方法
```
js
/**
* 生成ID
* @returns
*/
public
NextId
():
number
|
bigint
{
if
(
this
.
_IsOverCost
)
{
//
let
id
=
this
.
NextOverCostId
()
if
(
id
>=
9007199254740992
n
)
return
id
else
return
parseInt
(
id
.
toString
())
}
else
{
//
let
id
=
this
.
NextNormalId
()
if
(
id
>=
9007199254740992
n
)
return
id
else
return
parseInt
(
id
.
toString
())
}
}
>>>>>>>
91459
b1538b24a03174b20eba10db410d8ef5268
```
## 其他帮助
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录