java 生成15位分布式数字ID
时间: 2023-09-08 14:12:04 浏览: 494
可以使用Snowflake算法来生成分布式数字ID。Snowflake算法可以生成一个64位的ID,其中包含了时间戳、机器ID和序列号。下面是一个Java实现的示例代码,可以生成15位的分布式数字ID:
```java
public class SnowflakeIdWorker {
// 开始时间戳(2022-01-01 00:00:00)
private final long twepoch = 1640995200000L;
// 机器ID所占的位数
private final long workerIdBits = 5L;
// 数据标识ID所占的位数
private final long datacenterIdBits = 5L;
// 支持的最大机器ID,结果是31
private final long maxWorkerId = ~(-1L << workerIdBits);
// 支持的最大数据标识ID,结果是31
private final long maxDatacenterId = ~(-1L << datacenterIdBits);
// 序列号所占的位数
private final long sequenceBits = 12L;
// 机器ID向左移12位
private final long workerIdShift = sequenceBits;
// 数据标识ID向左移17位(12+5)
private final long datacenterIdShift = sequenceBits + workerIdBits;
// 时间戳向左移22位(5+5+12)
private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
// 生成序列的掩码,这里为4095
private final long sequenceMask = ~(-1L << sequenceBits);
private long workerId;
private long datacenterId;
private long sequence = 0L;
private long lastTimestamp = -1L;
public SnowflakeIdWorker(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
/**
* 产生下一个ID
*
* @return SnowflakeId
*/
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
// 相同毫秒内,序列号自增
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
// 同一毫秒的序列号已经用完,等待下一毫秒
timestamp = tilNextMillis(lastTimestamp);
}
} else {
// 不同毫秒内,序列号重置
sequence = 0L;
}
lastTimestamp = timestamp;
// 时间戳部分
long id = (timestamp - twepoch) << timestampLeftShift;
// 数据标识ID部分
id |= datacenterId << datacenterIdShift;
// 机器ID部分
id |= workerId << workerIdShift;
// 序列号部分
id |= sequence;
return id;
}
/**
* 等待下一个毫秒的到来
*
* @param lastTimestamp 上次生成ID的时间戳
* @return 当前时间戳
*/
private long tilNextMillis(long lastTimestamp) {
long timestamp = timeGen();
while (timestamp <= lastTimestamp) {
timestamp = timeGen();
}
return timestamp;
}
/**
* 获取当前的时间戳
*
* @return 当前时间戳
*/
private long timeGen() {
return System.currentTimeMillis();
}
}
```
使用示例:
```java
SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 1);
long id = idWorker.nextId();
System.out.println(id);
```
输出结果为一个15位的数字ID,例如:
```
246168155236608
```
阅读全文