当前位置:网站首页>基於Redis的分布式ID生成器
基於Redis的分布式ID生成器
2022-07-06 12:12: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算法
- 數據庫自增
边栏推荐
- Embedded startup process
- RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
- R & D thinking 01 ----- classic of embedded intelligent product development process
- .elf .map .list .hex文件
- Analysis of charging architecture of glory magic 3pro
- Important methods of array and string
- Priority inversion and deadlock
- 1081 rational sum (20 points) points add up to total points
- imgcat使用心得
- Working principle of genius telephone watch Z3
猜你喜欢
![[esp32 learning-1] construction of Arduino esp32 development environment](/img/31/dc16f776b7a95a08d177b1fd8856b8.png)
[esp32 learning-1] construction of Arduino esp32 development environment

优先级反转与死锁

Characteristics, task status and startup of UCOS III

Togglebutton realizes the effect of switching lights

Programmers can make mistakes. Basic pointers and arrays of C language

ES6语法总结--上篇(基础篇)

Kaggle竞赛-Two Sigma Connect: Rental Listing Inquiries(XGBoost)

ESP学习问题记录

Reno7 60W超级闪充充电架构

Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
随机推荐
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
Keyword inline (inline function) usage analysis [C language]
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
机器学习--决策树(sklearn)
Vscode basic configuration
程序员老鸟都会搞错的问题 C语言基础 指针和数组
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
Reno7 60W super flash charging architecture
arduino获取随机数
Detailed explanation of Union [C language]
小天才电话手表 Z3工作原理
Principle and implementation of MySQL master-slave replication
ARM PC=PC+8 最便于理解的阐述
map文件粗略分析
Machine learning -- decision tree (sklearn)
Navigator object (determine browser type)
List and set
Dependency in dependencymanagement cannot be downloaded and red is reported
RT-Thread的main线程“卡死”的一种可能原因及解决方案
JS變量類型以及常用類型轉換