1.引入依赖
<dependency>
<groupId>com.xy</groupId>
<artifactId>xy-core-ids-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
2.添加配置
com.xy.id.generator.zk.enable=true
com.xy.id.generator.zk.connect-string=${server-addr}:2181
com.xy.id.generator.cloud-service=ali
com.xy.id.generator.zk.username=
3.代码使用
String newBizNo = BizNoUtils.getNewBizNo(9);
4.组成说明
private static String getNewBizNo(String systemNum, Integer bizNum, Integer databaseNum, Integer tableNum, String version) {
Preconditions.checkArgument(StringUtils.isNotBlank(systemNum) && systemNum.length() == 3, "[ID Generator]流水号生成参数错误: 系统号为固定长度[3].");
Preconditions.checkArgument(bizNum != null && bizNum > -1 && bizNum < 100, "[ID Generator]流水号生成参数错误: 业务号范围[0~99].");
Preconditions.checkArgument(databaseNum != null && databaseNum > -1 && databaseNum < 100, "[ID Generator]流水号生成参数错误: 库名编号范围[0~99].");
Preconditions.checkArgument(tableNum != null && tableNum > -1 && tableNum < 1000, "[ID Generator]流水号生成参数错误: 表名编号范围[0~999].");
String uid = Long.toString(idGenManager.genUniqueId());
Preconditions.checkArgument(StringUtils.isNotBlank(uid) && uid.length() == 19, "[ID Generator]流水号生成错误: UID不能为空. ");
String bizNumStr = StringUtils.leftPad(bizNum.toString(), 2, '0');
String databaseNumStr = StringUtils.leftPad(databaseNum.toString(), 2, '0');
String tableNumStr = StringUtils.leftPad(tableNum.toString(), 3, '0');
String cloudService = EnvironmentManager.getCloudService().getValue();
Preconditions.checkArgument(StringUtils.isNotBlank(cloudService) && cloudService.length() == 1, "[ID Generator]流水号生成参数错误: 云服务类型为固定长度[1].");
String env = EnvironmentManager.getEnvValue();
Preconditions.checkArgument(StringUtils.isNotBlank(env) && env.length() == 1, "[ID Generator]流水号生成参数错误: 环境为固定长度[1].");
return StringUtils.join(new String[]{systemNum, bizNumStr, cloudService, env, version, databaseNumStr, tableNumStr, uid});
}
public static Map<BizNoPropertiesEnum, Object> parseNewBizNo(String no) {
Preconditions.checkArgument(StringUtils.isNotBlank(no) && no.length() == 32, "[ID Generator]流水号解析参数错误: 流水号为固定长度[32].");
String sysNum = no.substring(0, 3);
String bizNum = no.substring(3, 5);
String cloudService = no.substring(5, 6);
String env = no.substring(6, 7);
String version = no.substring(7, 8);
String databaseNum = no.substring(8, 10);
String tableNum = no.substring(10, 13);
String uid = no.substring(13);
Map<BizNoPropertiesEnum, Object> map = new HashMap();
map.put(BizNoPropertiesEnum.SYSTEM_NUM, Integer.valueOf(sysNum));
map.put(BizNoPropertiesEnum.BIZ_NUM, Integer.valueOf(bizNum));
map.put(BizNoPropertiesEnum.CLOUD_SERVICE, cloudService);
map.put(BizNoPropertiesEnum.ENV, env);
map.put(BizNoPropertiesEnum.VERSION, version);
map.put(BizNoPropertiesEnum.DATABASE_NUM, Integer.valueOf(databaseNum));
map.put(BizNoPropertiesEnum.TABLE_NUM, Integer.valueOf(tableNum));
map.put(BizNoPropertiesEnum.BIZ_ID, uid);
return map;
}
3位系统号+2位业务号+1位部署云+1位环境编号+1位版本号+2位表编号+13位递增随机数