当前位置:网站首页>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);
}
}
边栏推荐
- Why can ping fail while traceroute can
- 【NOI模拟赛】给国与时光鸡(构造)
- [pytoch basic tutorial 31] youtubednn model analysis
- 普通人没有学历,自学编程可以月入过万吗?
- 玄铁E906移植----番外0:玄铁C906仿真环境搭建
- The form image uploaded in chorme cannot view the binary image information of the request body
- Kaformer personal notes
- YOLOX backbone——CSPDarknet的实现
- 【NOI模拟赛】摆(线性代数,杜教筛)
- 目标检测系列——Fast R-CNN
猜你喜欢

【使用 PicGo+腾讯云对象存储COS 作为图床】

MySQL | store notes of Master Kong MySQL from introduction to advanced

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

嵌入式 | 硬件转软件的几条建议

One article explains in detail | those things about growth

【NOI模拟赛】寄(树形DP)

Prompt code when MySQL inserts Chinese data due to character set problems: 1366

What is SRE? A detailed explanation of SRE operation and maintenance system

Qingcloud based "real estate integration" cloud solution

linux(centos7.9)安装部署mysql-cluster 7.6
随机推荐
[e325: attention] VIM editing error
MySQL | store notes of Master Kong MySQL from introduction to advanced
1528. 重新排列字符串
IDEA另起一行快捷键
数据中台:数据中台技术架构详解
2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
数据中台:数据中台全栈技术架构解析,附带行业解决方案
数据中台:数据采集和抽取的技术栈详解
Telnet port login method with user name for liunx server
The form image uploaded in chorme cannot view the binary image information of the request body
【LeetCode】415. 字符串相加
[Niuke] length of the last word of HJ1 string
Become an IEEE student member
JS to find and update the specified value in the object through the key
2138. splitting a string into groups of length k
mysql写的代码数据 增删查改等等
4274. suffix expression
[noi Simulation Competition] geiguo and time chicken (structure)
[use picgo+ Tencent cloud object to store cos as a map bed]
Database to query the quantity of books lent in this month. If it is higher than 10, it will display "more than 10 books lent in this month". Otherwise, it will display "less than 10 books lent in thi