当前位置:网站首页>基于Redis的分布式ID生成器
基于Redis的分布式ID生成器
2022-07-06 09:17:00 【阿杆.】
基于Redis的分布式ID生成器
ID自增策略
- 每天一个key,方便统计订单量
- ID构造是 时间戳 + 计数器
ID的组成部分
- 符号位:1bit,永远为0
- 时间戳:31bit,以秒为单位,从2022年1月开始计数,可以使用68年,也可以根据需求,修改为每分钟、每小时或每天的计数器,可以增大可用时间。
- 序列号:32bit,每天的计数器,支持每天产生2^32个不同ID,也可以根据需求,修改为每小时、每分钟或每秒的计数器,但需要和时间戳相配合,可以增大ID数量。
这里是以秒为单位的时间戳和以天为单位的序列化计数器组成的ID生成器。
package cn.sticki.common.redis.utils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
/** * @author 阿杆 * @version 1.0 * @date 2022/6/20 20:29 */
@Component
public class RedisIdGenerator {
/** * 开始时间戳 */
private static final long BEGIN_TIMESTAMP = 1640995200L;
/** * 序列号的位数 */
private static final int COUNT_BITS = 32;
private final RedisTemplate<String, Long> redisTemplate;
public RedisIdGenerator(RedisTemplate<String, Long> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@SuppressWarnings("ConstantConditions")
public long nextId(String keyPrefix) {
// 1. 生成时间戳
LocalDateTime now = LocalDateTime.now();
long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
long timestamp = nowSecond - BEGIN_TIMESTAMP;
// 2. 生成序列号
// 2.1 获取当前日期,精确到天
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
// 2.2 获取redis自增长值
long increment = redisTemplate.opsForValue().increment("id:" + keyPrefix + ":" + date);
// 3. 拼接并返回
return increment << COUNT_BITS | timestamp;
}
}
其他全局唯一ID生成策略
- UUID
- Redis自增
- snowflake算法
- 数据库自增
边栏推荐
- 树莓派 轻触开关 按键使用
- Time slice polling scheduling of RT thread threads
- Principle and implementation of MySQL master-slave replication
- Rough analysis of map file
- Redis interview questions
- Machine learning -- decision tree (sklearn)
- Bubble sort [C language]
- List and set
- Pytorch-温度预测
- There are three iPhone se 2022 models in the Eurasian Economic Commission database
猜你喜欢
Detailed explanation of 5g working principle (explanation & illustration)
Machine learning -- linear regression (sklearn)
Programmers can make mistakes. Basic pointers and arrays of C language
[esp32 learning-2] esp32 address mapping
电商数据分析--用户行为分析
C语言回调函数【C语言】
OPPO VOOC快充电路和协议
R & D thinking 01 ----- classic of embedded intelligent product development process
Redis interview questions
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
随机推荐
Matlab learning and actual combat notes
Détails du Protocole Internet
Working principle of genius telephone watch Z3
Bubble sort [C language]
JS object and event learning notes
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Some concepts often asked in database interview
共用体(union)详解【C语言】
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
高通&MTK&麒麟 手机平台USB3.0方案对比
Imgcat usage experience
优先级反转与死锁
gcc 编译选项
Pytoch temperature prediction
Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
Inline detailed explanation [C language]
B tree and b+ tree of MySQL index implementation
C language, log print file name, function name, line number, date and time
AMBA、AHB、APB、AXI的理解