当前位置:网站首页>基于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算法
- 数据库自增
边栏推荐
- 机器学习--线性回归(sklearn)
- Arm pc=pc+8 is the most understandable explanation
- Using LinkedHashMap to realize the caching of an LRU algorithm
- E-commerce data analysis -- User Behavior Analysis
- Kaggle competition two Sigma connect: rental listing inquiries
- XML file explanation: what is XML, XML configuration file, XML data file, XML file parsing tutorial
- Dependency in dependencymanagement cannot be downloaded and red is reported
- AMBA、AHB、APB、AXI的理解
- Linux Yum install MySQL
- Characteristics, task status and startup of UCOS III
猜你喜欢

Detailed explanation of 5g working principle (explanation & illustration)

STM32 how to locate the code segment that causes hard fault

RT-Thread的main线程“卡死”的一种可能原因及解决方案

ToggleButton实现一个开关灯的效果

Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries

MySQL realizes read-write separation

Esp8266 uses Arduino to connect Alibaba cloud Internet of things

Correspondence between STM32 model and contex M

电商数据分析--用户行为分析

Reno7 60W超级闪充充电架构
随机推荐
Programmers can make mistakes. Basic pointers and arrays of C language
map文件粗略分析
XML文件详解:XML是什么、XML配置文件、XML数据文件、XML文件解析教程
Kconfig Kbuild
高通&MTK&麒麟 手机平台USB3.0方案对比
高通&MTK&麒麟 手機平臺USB3.0方案對比
機器學習--線性回歸(sklearn)
Priority inversion and deadlock
JS object and event learning notes
电商数据分析--用户行为分析
[esp32 learning-1] construction of Arduino esp32 development environment
Basic use of pytest
RT-Thread 线程的时间片轮询调度
Vert. x: A simple login access demo (simple use of router)
ESP学习问题记录
Reading notes of difficult career creation
AMBA、AHB、APB、AXI的理解
嵌入式启动流程
ESP learning problem record
Those commonly used tool classes and methods in hutool