生成唯一key方法 --UUID 生成数字key 处理逻辑

// todo
//因为 雪花算法测试发现重复数据所以使用UUID方式来实现获取订单id
UUID uuid = UUID.randomUUID();
log.info("UUid====:{}",uuid);
String uuidStr = uuid.toString();
BigInteger bigInteger = new BigInteger(uuidStr.replace("-", ""), 16);
long result = bigInteger.longValue();
long absValue = Math.abs(result);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中使用Snowflake算法生成8-4-4-4-12格式的UUID,可以稍微修改上面的示例代码。具体来说,我们需要将48位的数字分成5个部分,其中前8位表示时间戳的高位,接下来的4位表示数据中心标识,4位表示工作机器标识,接下来的4位表示时间戳的低位,最后的36位表示序列号。 下面是一个示例代码,可用于生成8-4-4-4-12格式的UUID: ```java import java.util.UUID; public class SnowflakeIdGenerator { private final long datacenterId; private final long workerId; private long sequence = 0L; private final long twepoch = 1288834974657L; private final long datacenterIdBits = 4L; private final long workerIdBits = 4L; private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); private final long maxWorkerId = -1L ^ (-1L << workerIdBits); private final long sequenceBits = 12L; private final long workerIdShift = sequenceBits; private final long datacenterIdShift = sequenceBits + workerIdBits; private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; private final long sequenceMask = -1L ^ (-1L << sequenceBits); private long lastTimestamp = -1L; public SnowflakeIdGenerator(long datacenterId, long workerId) { if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException("datacenterId can't be greater than maxDatacenterId or less than 0"); } if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException("workerId can't be greater than maxWorkerId or less than 0"); } this.datacenterId = datacenterId; this.workerId = workerId; } public synchronized UUID nextId() { long timestamp = timeGen(); if (timestamp < lastTimestamp) { throw new RuntimeException("Clock moved backwards. Refusing to generate id for " + (lastTimestamp - timestamp) + " milliseconds"); } if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; if (sequence == 0) { timestamp = tilNextMillis(lastTimestamp); } } else { sequence = 0L; } lastTimestamp = timestamp; long mostSigBits = (timestamp - twepoch) << timestampLeftShift; mostSigBits |= (datacenterId << datacenterIdShift); mostSigBits |= (workerId << workerIdShift); mostSigBits |= (sequence); // Split into 5 parts String uuidStr = String.format("%016x", mostSigBits); String part1 = uuidStr.substring(0, 8); String part2 = uuidStr.substring(8, 12); String part3 = uuidStr.substring(12, 16); String part4 = uuidStr.substring(16, 20); String part5 = uuidStr.substring(20, 32); // Combine the parts into UUID format String uuid = part1 + "-" + part2 + "-" + part3 + "-" + part4 + "-" + part5; return UUID.fromString(uuid); } private long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } private long timeGen() { return System.currentTimeMillis(); } } ``` 要使用此类生成UUID,只需实例化`SnowflakeIdGenerator`类并调用`nextId()`方法。例如: ```java SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1); UUID uuid = idGenerator.nextId(); System.out.println(uuid.toString()); ``` 这将生成一个唯一UUID,格式为8-4-4-4-12。在此示例中,`datacenterId`和`workerId`都设置为1,可以根据需要更改它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值