1.引入依赖

  1. <dependency>
  2. <groupId>com.xy</groupId>
  3. <artifactId>xy-core-ids-boot-starter</artifactId>
  4. <version>2.0.0</version>
  5. </dependency>

2.添加配置

  1. com.xy.id.generator.zk.enable=true
  2. com.xy.id.generator.zk.connect-string=${server-addr}:2181
  3. com.xy.id.generator.cloud-service=ali
  4. com.xy.id.generator.zk.username=

3.代码使用

  1. String newBizNo = BizNoUtils.getNewBizNo(9);

4.组成说明

  1. private static String getNewBizNo(String systemNum, Integer bizNum, Integer databaseNum, Integer tableNum, String version) {
  2. Preconditions.checkArgument(StringUtils.isNotBlank(systemNum) && systemNum.length() == 3, "[ID Generator]流水号生成参数错误: 系统号为固定长度[3].");
  3. Preconditions.checkArgument(bizNum != null && bizNum > -1 && bizNum < 100, "[ID Generator]流水号生成参数错误: 业务号范围[0~99].");
  4. Preconditions.checkArgument(databaseNum != null && databaseNum > -1 && databaseNum < 100, "[ID Generator]流水号生成参数错误: 库名编号范围[0~99].");
  5. Preconditions.checkArgument(tableNum != null && tableNum > -1 && tableNum < 1000, "[ID Generator]流水号生成参数错误: 表名编号范围[0~999].");
  6. String uid = Long.toString(idGenManager.genUniqueId());
  7. Preconditions.checkArgument(StringUtils.isNotBlank(uid) && uid.length() == 19, "[ID Generator]流水号生成错误: UID不能为空. ");
  8. String bizNumStr = StringUtils.leftPad(bizNum.toString(), 2, '0');
  9. String databaseNumStr = StringUtils.leftPad(databaseNum.toString(), 2, '0');
  10. String tableNumStr = StringUtils.leftPad(tableNum.toString(), 3, '0');
  11. String cloudService = EnvironmentManager.getCloudService().getValue();
  12. Preconditions.checkArgument(StringUtils.isNotBlank(cloudService) && cloudService.length() == 1, "[ID Generator]流水号生成参数错误: 云服务类型为固定长度[1].");
  13. String env = EnvironmentManager.getEnvValue();
  14. Preconditions.checkArgument(StringUtils.isNotBlank(env) && env.length() == 1, "[ID Generator]流水号生成参数错误: 环境为固定长度[1].");
  15. return StringUtils.join(new String[]{systemNum, bizNumStr, cloudService, env, version, databaseNumStr, tableNumStr, uid});
  16. }
  17. public static Map<BizNoPropertiesEnum, Object> parseNewBizNo(String no) {
  18. Preconditions.checkArgument(StringUtils.isNotBlank(no) && no.length() == 32, "[ID Generator]流水号解析参数错误: 流水号为固定长度[32].");
  19. String sysNum = no.substring(0, 3);
  20. String bizNum = no.substring(3, 5);
  21. String cloudService = no.substring(5, 6);
  22. String env = no.substring(6, 7);
  23. String version = no.substring(7, 8);
  24. String databaseNum = no.substring(8, 10);
  25. String tableNum = no.substring(10, 13);
  26. String uid = no.substring(13);
  27. Map<BizNoPropertiesEnum, Object> map = new HashMap();
  28. map.put(BizNoPropertiesEnum.SYSTEM_NUM, Integer.valueOf(sysNum));
  29. map.put(BizNoPropertiesEnum.BIZ_NUM, Integer.valueOf(bizNum));
  30. map.put(BizNoPropertiesEnum.CLOUD_SERVICE, cloudService);
  31. map.put(BizNoPropertiesEnum.ENV, env);
  32. map.put(BizNoPropertiesEnum.VERSION, version);
  33. map.put(BizNoPropertiesEnum.DATABASE_NUM, Integer.valueOf(databaseNum));
  34. map.put(BizNoPropertiesEnum.TABLE_NUM, Integer.valueOf(tableNum));
  35. map.put(BizNoPropertiesEnum.BIZ_ID, uid);
  36. return map;
  37. }

3位系统号+2位业务号+1位部署云+1位环境编号+1位版本号+2位表编号+13位递增随机数