当前位置:网站首页>Redis实现全局唯一ID
Redis实现全局唯一ID
2022-06-24 07:28:00 【步尔斯特】
分布式系统中由于跨进程跨系统,在某些场景中,我们需要生成全局的唯一ID,例如订单系统,并发情况下,不同的系统需要同时生成不一样的订单ID方便后续的订单下单与查询等等。
全局唯一ID生成策略
- UUID
- Redis自增
- snowflake算法
- 数据库自增
- 百度开源的UidGenerator
- 美图点评的Leaf
Redis自增ID策略
- 每天一个key,方便统计订单量
- ID构造:时间戳 + 计数器

ID的组成部分:
- 符号位:1bit,永远为0,表示正数
- 时间戳:31bit,最大2147483648秒,大概69年
- 序列号:32bit,秒内的计数器,支持每秒产生2^32个不同ID
利用redis生成全局唯一ID,其实redis扮演的角色就是一个计数器的作用,方便后续的统计。
- 优点:高性能,高并发,唯一性,递增性,安全性。
- 缺点:需要依赖redis去实现
代码实现
/** * @author issavior */
@Component
public class RedisIdWorker {
/** * 开始时间戳 */
private static final long BEGIN_TIMESTAMP = 1640995200L;
/** * 序列号的位数 */
private static final int COUNT_BITS = 32;
private final StringRedisTemplate stringRedisTemplate;
public RedisIdWorker(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
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.自增长
Long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);
// 3.拼接并返回
return timestamp << COUNT_BITS | (count == null ? 0 : count);
}
/** * 获取2022年1月1号0点0时0分的时间戳 * @param args */
public static void main(String[] args) {
LocalDateTime startLocalTime = LocalDateTime.of(2022, 1, 1, 0, 0, 0);
long startTime = startLocalTime.toEpochSecond(ZoneOffset.UTC);
System.out.println(startTime);
LocalDateTime now = LocalDateTime.now();
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd:HH:mm"));
System.out.println(date);
}
}
边栏推荐
- Camera projection matrix calculation
- 基于单片机开发的酒精浓度测试仪方案
- IDEA另起一行快捷键
- On the routing tree of gin
- 110. 平衡二叉树-递归法
- I heard that you are still spending money to buy ppt templates from the Internet?
- 【NOI模拟赛】摆(线性代数,杜教筛)
- 2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
- Qingcloud based "real estate integration" cloud solution
- [noi Simulation Competition] geiguo and time chicken (structure)
猜你喜欢

eBanb B1手环刷固件异常中断处理
![Jenkins is deployed automatically and cannot connect to the dependent service [solved]](/img/fe/f294955a9bdf7492aab360e44e052d.png)
Jenkins is deployed automatically and cannot connect to the dependent service [solved]

用VNC Viewer的方式远程连接无需显示屏的树莓派
![[quantitative investment] discrete Fourier transform to calculate array period](/img/0d/aac02463ff403fb1ff871af5ff91fa.png)
[quantitative investment] discrete Fourier transform to calculate array period

目标检测系列——Fast R-CNN

【量化投资】离散傅里叶变换求数组周期

Leetcode -- wrong set

4274. 后缀表达式

Mysql数据(Liunx环境)定时备份

Opencv maximum filtering (not limited to images)
随机推荐
4274. 后缀表达式
Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems
1704. 判断字符串的两半是否相似
数据中台:数据中台技术架构详解
关于 GIN 的路由树
Sword finger offer 55 - I. depth DFS method of binary tree
开源之夏中选名单已公示,基础软件领域成为今年的热门申请
1844. replace all numbers with characters
【LeetCode】387. First unique character in string
tcpdump抓包实现过程
One article explains in detail | those things about growth
“不平凡的代理初始值设定不受支持”,出现的原因及解决方法
Data middle office: overview of data governance
Essay - Reflection
Why can ping fail while traceroute can
【使用 PicGo+腾讯云对象存储COS 作为图床】
什么是图神经网络?图神经网络有什么用?
Data middle office: middle office practice and summary
I heard that you are still spending money to buy ppt templates from the Internet?
opencv最大值滤波(不局限于图像)