当前位置:网站首页>基於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算法
- 數據庫自增
边栏推荐
猜你喜欢

高通&MTK&麒麟 手機平臺USB3.0方案對比

Cannot change version of project facet Dynamic Web Module to 2.3.

js 变量作用域和函数的学习笔记
![[template] KMP string matching](/img/f9/cd8b6f8e2b0335c2ec0a76fc500c9b.jpg)
[template] KMP string matching

Reno7 60W超级闪充充电架构

Reno7 60W super flash charging architecture

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

高通&MTK&麒麟 手机平台USB3.0方案对比

History object

Esp8266 uses Arduino to connect Alibaba cloud Internet of things
随机推荐
GCC compilation options
Symbolic representation of functions in deep learning papers
[template] KMP string matching
Characteristics, task status and startup of UCOS III
Unit test - unittest framework
Variable star user module
物联网系统框架学习
RuntimeError: cuDNN error: CUDNN_ STATUS_ NOT_ INITIALIZED
Comparison of solutions of Qualcomm & MTK & Kirin mobile platform USB3.0
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
Time slice polling scheduling of RT thread threads
Understanding of AMBA, AHB, APB and Axi
Pytoch implements simple linear regression demo
Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation
FreeRTOS 任务函数里面的死循环
ESP8266使用arduino连接阿里云物联网
sklearn之feature_extraction.text.CountVectorizer / TfidVectorizer
PyTorch四种常用优化器测试
Common properties of location