網(wǎng)站首頁 編程語言 正文
ID的組成部分:
- 符號位:1bit,永遠為0
- 時間戳:31bit,以秒為單位,可以使用69年
- 序列號:32bit,秒內(nèi)的計數(shù)器,支持每秒產(chǎn)生2^32個不同ID
生成代碼:
public class RedisIdWorker {
? ? /**
? ? ?* 開始時間戳
? ? ?*/
? ? private static final long BEGIN_TIMESTAMP = 1640995200L;
? ? /**
? ? ?* 序列號的位數(shù)
? ? ?*/
? ? private static final int COUNT_BITS = 32;
? ? private StringRedisTemplate stringRedisTemplate;
?? ??? ?//構(gòu)造方法形式注入
? ? 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實現(xiàn)全局唯一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"));
? ? ? ? //自定義編號規(guī)則
? ? ? ? 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;
? ? }
? ? /**
? ? ?* 判斷當前時間距離第二天凌晨的秒數(shù)
? ? ?* @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
相關(guān)推薦
- 2022-06-23 Python函數(shù)和模塊的使用詳情_python
- 2023-03-23 Python?input函數(shù)實現(xiàn)獲取鍵盤輸入的字符串流程講解_python
- 2023-07-02 oracle數(shù)據(jù)庫排序后如何獲取第一條數(shù)據(jù)_oracle
- 2022-11-23 Android?IdleHandler基本使用及應用案例詳解_Android
- 2022-11-09 WPF使用WrapPanel實現(xiàn)虛擬化效果_C#教程
- 2022-08-05 Entity?Framework主從表數(shù)據(jù)加載方式_C#教程
- 2022-05-11 剖析數(shù)據(jù)庫中重要而又常被曲解的概念
- 2023-02-06 python?wordcloud庫實例講解使用方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支