当前位置:网站首页>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);
}
}
边栏推荐
- Essay - Reflection
- 数云发布2022美妆行业全域消费者数字化经营白皮书:全域增长破解营销难题
- every()、map()、forEarch()方法。数组里面有对象的情况
- Data middle office: overview of data governance
- 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的地理信息企业研发云解决方案
- 小程序云数据,数据请求一个集合数据的方法
- A tip to read on Medium for free
- Data midrange: detailed explanation of the technical stack of data acquisition and extraction
猜你喜欢
![[Niuke] length of the last word of HJ1 string](/img/8b/6ba6506415b8112aea957ac5647121.png)
[Niuke] length of the last word of HJ1 string

Squid代理服务器应用

I heard that you are still spending money to buy ppt templates from the Internet?

华为路由器:ipsec技术

eBanb B1手环刷固件异常中断处理

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

"I can't understand Sudoku, so I choose to play Sudoku."

【LeetCode】387. First unique character in string

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

WebRTC系列-网络传输之5选择最优connection切换
随机推荐
[e325: attention] VIM editing error
Double pointer analog
Liunx Mysql安装
2020中国全国各省市,三级联动数据,数据机构(数据来自国家统计局官网)
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
Telnet port login method with user name for liunx server
【牛客】HJ1 字符串最后一个单词的长度
Spark - the number of leftouterjoin results is inconsistent with that of the left table
Fast and slow pointer series
Jenkins is deployed automatically and cannot connect to the dependent service [solved]
十二、所有功能实现效果演示
mysql写的代码数据 增删查改等等
Sword finger offer 55 - I. depth DFS method of binary tree
OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
1528. rearrange strings
JS to find and update the specified value in the object through the key
Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems
双指针模拟
Unable to change the virtual machine power status and report an error solution
PM2 deploy nuxt3 JS project