You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 1.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637
  1. ## 运行环境
  2. JDK 1.8+
  3. ## 引用 maven 包
  4. ```
  5. <dependency>
  6. <groupId>com.github.yitter</groupId>
  7. <artifactId>yitter-idgenerator</artifactId>
  8. <version>1.0.5</version>
  9. </dependency>
  10. ```
  11. ## 调用示例(Java)
  12. 第1步,**全局** 初始化(应用程序启动时执行一次):
  13. ```
  14. // 创建 IdGeneratorOptions 对象,构造函数输入 WorkerId:
  15. IdGeneratorOptions options = new IdGeneratorOptions(1);
  16. // options.WorkerIdBitLength = 10; // WorkerIdBitLength 默认值6,支持的 WorkerId 最大值为2^6-1,若 WorkerId 超过64,可设置更大的 WorkerIdBitLength
  17. // ...... 其它参数设置参考 IdGeneratorOptions 定义,一般来说,只要再设置 WorkerIdBitLength (决定 WorkerId 的最大值)。
  18. // 保存参数(必须的操作,否则以上设置都不能生效):
  19. YitIdHelper.setIdGenerator(options);
  20. // 以上初始化过程只需全局一次,且必须在第2步之前设置。
  21. ```
  22. 第2步,生成ID:
  23. ```
  24. // 初始化以后,即可在任何需要生成ID的地方,调用以下方法:
  25. long newId = YitIdHelper.nextId();
  26. ```
  27. 如果基于DI框架集成,可以参考 YitIdHelper 去管理 IdGenerator 对象,须使用 **单例** 模式。

雪花算法中非常好用的数字ID生成器