当前位置:网站首页>基于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算法
- 数据库自增
边栏推荐
- STM32 如何定位导致发生 hard fault 的代码段
- 【ESP32学习-2】esp32地址映射
- Amba, ahb, APB, Axi Understanding
- Kaggle competition two Sigma connect: rental listing inquiries (xgboost)
- Linux Yum install MySQL
- Mall project -- day09 -- order module
- 荣耀Magic 3Pro 充电架构分析
- 数据分析之缺失值填充(重点讲解多重插值法Miceforest)
- ES6语法总结--下篇(进阶篇 ES6~ES11)
- Arduino gets the length of the array
猜你喜欢
Gallery's image browsing and component learning
ES6 grammar summary -- Part 2 (advanced part es6~es11)
几个关于指针的声明【C语言】
Principle and implementation of MySQL master-slave replication
ToggleButton实现一个开关灯的效果
Kaggle competition two Sigma connect: rental listing inquiries
MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解
Arduino uno R3 register writing method (1) -- pin level state change
Time slice polling scheduling of RT thread threads
ESP learning problem record
随机推荐
Machine learning -- decision tree (sklearn)
open-mmlab labelImg mmdetection
Reno7 60W super flash charging architecture
Mysql database interview questions
Esp8266 uses Arduino to connect Alibaba cloud Internet of things
Distribute wxWidgets application
Kconfig Kbuild
Fashion-Gen: The Generative Fashion Dataset and Challenge 论文解读&数据集介绍
MySQL realizes read-write separation
RT-Thread 线程的时间片轮询调度
C language callback function [C language]
Matlab learning and actual combat notes
Dead loop in FreeRTOS task function
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
Linux Yum install MySQL
共用体(union)详解【C语言】
Pytorch实现简单线性回归Demo
Dependency in dependencymanagement cannot be downloaded and red is reported
Understanding of AMBA, AHB, APB and Axi
Pytorch-温度预测