網站首頁 編程語言 正文
ID的組成部分:
- 符號位:1bit,永遠為0
- 時間戳:31bit,以秒為單位,可以使用69年
- 序列號:32bit,秒內的計數器,支持每秒產生2^32個不同ID
生成代碼:
public class RedisIdWorker {
? ? /**
? ? ?* 開始時間戳
? ? ?*/
? ? private static final long BEGIN_TIMESTAMP = 1640995200L;
? ? /**
? ? ?* 序列號的位數
? ? ?*/
? ? private static final int COUNT_BITS = 32;
? ? private 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"));
? ? ? ? long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);
? ? ? ? //3.拼接并返回
? ? ? ? return timestamp << COUNT_BITS | count;
? ? }
}
PS:Redis實現全局唯一id生成
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
/**
?* 描述:
?* 唯一ID生成器
?* @author jimmy
?* @create 2020-11-06 16:06
?*/
@Component
public class GenerateIDUtil {
? ? @Autowired
? ? private RedisTemplate redisTemplate;
? ? /**
? ? ?* 生成每天的初始Id
? ? ?* @param key
? ? ?* @return
? ? ?*/ ?public String initPrimaryId(String key) {
? ? ? ? Assert.hasLength(key, "hashName不能為空");
? ? ? ? String hashCol = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
? ? ? ? //自定義編號規則
? ? ? ? String hashColVal = hashCol + "00001";
// ? ? ? ?redisTemplate.opsForHash().putIfAbsent(hashName, hashCol, hashColVal);
? ? ? ? Long expiresTime = getSecondsNextEarlyMorning();
? ? ? ? redisTemplate.opsForValue().set(key, Long.valueOf(hashColVal), expiresTime, TimeUnit.SECONDS);
? ? ? ? return hashColVal;
? ? }
? ? /**
? ? ?* 獲取分布式Id ? ??
? ? ?* @param key
? ? ?* @return
? ? ?*/
? ? public String getPrimaryId(String key) {
? ? ? ? String id = "";
? ? ? ? if(redisTemplate.hasKey(key)){
? ? ? ? ? ? // redisTemplate.opsForValue().get(key);
? ? ? ? ? ? // redisTemplate.delete(key);
? ? ? ? ? ? id = String.valueOf(redisTemplate.opsForValue().increment(key, 1));
? ? ? ? } else {
? ? ? ? ? ? id = initPrimaryId(key);
? ? ? ? }
? ? ? ? return id;
? ? }
? ? /**
? ? ?* 判斷當前時間距離第二天凌晨的秒數
? ? ?* @return 返回值單位為[s:秒]
? ? ?*/
? ? public Long getSecondsNextEarlyMorning() {
? ? ? ? Calendar cal = Calendar.getInstance();
? ? ? ? cal.add(Calendar.DAY_OF_YEAR, 1);
? ? ? ? cal.set(Calendar.HOUR_OF_DAY, 0);
? ? ? ? cal.set(Calendar.SECOND, 0);
? ? ? ? cal.set(Calendar.MINUTE, 0);
? ? ? ? cal.set(Calendar.MILLISECOND, 0);
? ? ? ? return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
? ? }
}
原文鏈接:https://blog.csdn.net/qq_45721579/article/details/125359490
相關推薦
- 2022-08-15 Dubbo3基礎配置安裝及整合Springboot
- 2022-07-15 Android視頻加水印之FFmpeg的簡單應用實例_Android
- 2022-06-20 C語言中數據是如何存儲在內存中的_C 語言
- 2022-10-11 C++eof()判斷是否讀取到文件尾
- 2022-03-24 聊一聊redis奇葩數據類型與集群知識_Redis
- 2022-04-19 C語言庫函數qsort及bsearch快速排序算法使用解析_C 語言
- 2022-07-02 python?np.arange?步長0.1的問題需要特別注意_python
- 2022-11-24 redis使用skiplist跳表的原因解析_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支