Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Bunny 2024-08-08 22:17:03 +08:00
commit e5c4ce2845
5 changed files with 9 additions and 184 deletions

View File

@ -1,25 +0,0 @@
package cn.bunny.common.service.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "bunny.snowflake")
@Data
public class SnowflakeProperties {
// 数据中心id
private Long datacenterId;
// 数据中心id位数
private Long datacenterBits;
// 机器id
private Long workerId;
// 机器id位数
private Long workerBits;
// 序列id所占位数
private Long sequenceBits;
// 时间戳起始点毫秒
private Long twepoch;
// 单次批量生成id的最大数量
private Integer maxBatchCount;
}

View File

@ -1,149 +0,0 @@
package cn.bunny.common.service.utils;
import cn.bunny.common.service.properties.SnowflakeProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class SnowflakeIdGenerator {
// 数据中心id
private final long datacenterId;
// 数据中心id位数
private final long datacenterBits;
// 机器id
private final long workerId;
// 机器id位数
private final long workerBits;
// 序列id所占位数
private final long sequenceBits;
// 时间戳起始点毫秒
private final long twepoch;
// 数据中心最大id
private final long maxDatacenterId;
// 机器最大id
private final long maxWorkerId;
// 最大序列号
private final long maxSequence;
// 机器id左移位数
private final long workerIdShift;
// 数据中心id左移位数
private final long datacenterIdShift;
// 毫秒数左移位数
private final long timestampLeftShift;
// 单次批量生成id的最大数量
private final int maxBatchCount;
// 序列号
private long sequence = 0L;
// 上一次时间戳
private long lastTimestamp = -1L;
public SnowflakeIdGenerator(SnowflakeProperties properties) {
// 数据中心id
this.datacenterId = properties.getDatacenterId();
// 数据中心id位数
this.datacenterBits = properties.getDatacenterBits();
// 机器id
this.workerId = properties.getWorkerId();
// 机器id位数
this.workerBits = properties.getWorkerBits();
// 序列id所占位数
this.sequenceBits = properties.getSequenceBits();
// 时间戳起始点毫秒
this.twepoch = properties.getTwepoch();
// 数据中心最大id
this.maxDatacenterId = -1L ^ (-1L << properties.getDatacenterBits());
// 机器最大id
this.maxWorkerId = -1L ^ (-1L << properties.getWorkerBits());
// 最大序列号
this.maxSequence = -1L ^ (-1L << properties.getSequenceBits());
this.workerIdShift = properties.getSequenceBits();
// 数据中心id左移位数
this.datacenterIdShift = properties.getSequenceBits() + properties.getWorkerBits();
// 毫秒数左移位数
this.timestampLeftShift = properties.getSequenceBits() + properties.getWorkerBits() + properties.getSequenceBits();
// 单次批量生成id的最大数量
this.maxBatchCount = properties.getMaxBatchCount();
// 校验datacenterId和workerId是否超出最大值
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(String.format("数据中心Id不能大于%d或小于0", maxDatacenterId));
}
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(String.format("机器Id不能大于%d或小于0", maxWorkerId));
}
}
/**
* id生成方法(单个)
*/
public synchronized long nextId() {
// 获取当前时间的毫秒数
long timestamp = currentTime();
// 判断时钟是否回拨
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format("时钟回拨,回拨毫秒数:%d", lastTimestamp - timestamp));
}
// 设置序列号
if (lastTimestamp == timestamp) {
// 设置序列号递增如果当前毫秒内序列号已经达到最大值则直到下一毫秒在重新从0开始计算序列号
sequence = (sequence + 1) & maxSequence;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
// 计算id
return ((timestamp - twepoch) << timestampLeftShift) |
(datacenterId << datacenterIdShift) |
(workerId << workerIdShift) |
sequence;
}
/**
* id生成方法(批量)
*/
public synchronized List<Long> nextIds(int count) {
if (count > maxBatchCount || count < 0) {
throw new IllegalArgumentException(String.format("批量生成id的数量不能大于%d或小于0", maxBatchCount));
}
List<Long> ids = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
ids.add(nextId());
}
return ids;
}
/**
* 循环等待直至获取到新的毫秒时间戳
* 确保生成的时间戳总是向前移动的即使在相同的毫秒内请求多个ID时也能保持唯一性
*/
private long tilNextMillis(long lastTimestamp) {
long timestamp = currentTime();
// 循环等待直至获取到新的毫秒时间戳
while (timestamp <= lastTimestamp) {
timestamp = currentTime();
}
return timestamp;
}
/**
* 获取当前时间的毫秒数
*/
private long currentTime() {
return System.currentTimeMillis();
}
}

View File

@ -18,4 +18,7 @@ COPY target/*.jar /home/bunny/app.jar
ENTRYPOINT ["java","-jar","/home/bunny/app.jar"]
#暴露 8800 端口
EXPOSE 8800
EXPOSE 8080
# maven 打包
# mvn clean package -Pprod -DskipTests

View File

@ -1,3 +1,8 @@
# 线上禁用文档
knife4j:
enable: true
production: true
bunny:
datasource:
host: 192.168.3.100

View File

@ -68,12 +68,3 @@ bunny:
accessKey: ${bunny.minio.accessKey}
secretKey: ${bunny.minio.secretKey}
bucket-name: ${bunny.minio.bucket-name}
snowflake:
datacenterBits: 5 # 数据中心id位数
workerBits: 5 # 机器id位数
sequenceBits: 12 # 序列id所占位数
datacenterId: 1 # 数据中心id,范围0-2^5-1
workerId: 1 # 机器id,范围0-2^5-1
twepoch: 1704038400000 # 时间戳起始点2024-01-01 00::00:00 的毫秒数)
maxBatchCount: 100000 #单次批量生成id的最大数量 默认10万