From b362bdd3966b632695ca13f9ab114808951e4b5e Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Tue, 30 Jul 2024 13:38:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20:sparkles:=20?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=89=93=E5=8C=85=E7=8E=AF=E5=A2=83=E5=92=8C?= =?UTF-8?q?=E7=BA=BF=E4=B8=8A=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../properties/SnowflakeProperties.java | 25 --- .../service/utils/SnowflakeIdGenerator.java | 149 ------------------ service/Dockerfile | 5 +- .../src/main/resources/application-prod.yml | 5 + service/src/main/resources/application.yml | 9 -- 5 files changed, 9 insertions(+), 184 deletions(-) delete mode 100644 common/service-utils/src/main/java/cn/bunny/common/service/properties/SnowflakeProperties.java delete mode 100644 common/service-utils/src/main/java/cn/bunny/common/service/utils/SnowflakeIdGenerator.java diff --git a/common/service-utils/src/main/java/cn/bunny/common/service/properties/SnowflakeProperties.java b/common/service-utils/src/main/java/cn/bunny/common/service/properties/SnowflakeProperties.java deleted file mode 100644 index 19b0057..0000000 --- a/common/service-utils/src/main/java/cn/bunny/common/service/properties/SnowflakeProperties.java +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/common/service-utils/src/main/java/cn/bunny/common/service/utils/SnowflakeIdGenerator.java b/common/service-utils/src/main/java/cn/bunny/common/service/utils/SnowflakeIdGenerator.java deleted file mode 100644 index 8504b27..0000000 --- a/common/service-utils/src/main/java/cn/bunny/common/service/utils/SnowflakeIdGenerator.java +++ /dev/null @@ -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 nextIds(int count) { - if (count > maxBatchCount || count < 0) { - throw new IllegalArgumentException(String.format("批量生成id的数量不能大于%d或小于0", maxBatchCount)); - } - List 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(); - } -} \ No newline at end of file diff --git a/service/Dockerfile b/service/Dockerfile index 447a2bc..9c9d24f 100644 --- a/service/Dockerfile +++ b/service/Dockerfile @@ -18,4 +18,7 @@ COPY target/*.jar /home/bunny/app.jar ENTRYPOINT ["java","-jar","/home/bunny/app.jar"] #暴露 8800 端口 -EXPOSE 8800 \ No newline at end of file +EXPOSE 8080 + +# maven 打包 +# mvn clean package -Pprod -DskipTests \ No newline at end of file diff --git a/service/src/main/resources/application-prod.yml b/service/src/main/resources/application-prod.yml index 59d1992..de6c595 100644 --- a/service/src/main/resources/application-prod.yml +++ b/service/src/main/resources/application-prod.yml @@ -1,3 +1,8 @@ +# 线上禁用文档 +knife4j: + enable: true + production: true + bunny: datasource: host: 192.168.3.100 diff --git a/service/src/main/resources/application.yml b/service/src/main/resources/application.yml index 4ef693f..22ee819 100644 --- a/service/src/main/resources/application.yml +++ b/service/src/main/resources/application.yml @@ -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万 \ No newline at end of file