網站首頁 編程語言 正文
簡介:
全局唯一ID生成器是一種在分布式系統下用來生成全局唯一ID的工具
特性:
- 唯一性
- 高性能
- 安全性
- 高可用
- 遞增性
生成規則:
有時為了增加ID的安全性,我們可以不直接使用Redis自增的數值,而是拼接一些其他信息
ID組成部分:
- 符號位:1bit,永遠為0
- 時間戳:31bit,以秒為單位,可以使用69年
- 序列號:32bit,秒內的計數器,支持每秒產生2^32個不同ID
?ID生成類:
package com.example.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
/**
?* @Author DaBai
?* @Date 2022/3/17 下午 09:25
?* @Version 1.0
?*/
@Component
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 nowScond = now.toEpochSecond(ZoneOffset.UTC);
? ? ? ? long timestamp = nowScond - BEGIN_TIMESTAMP;
? ? ? ? //2、生成序列號
? ? ? ? // 2.1 獲取當前日期,精確到天
? ? ? ? String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
? ? ? ? long count = stringRedisTemplate.opsForValue().increment("icr" + keyPrefix + ":" + date);
? ? ? ? //3、拼接字符串
? ? ? ? // 時間戳左移32位,然后 或 序列號,有1為1
? ? ? ? long ids = timestamp << COUNT_BITS | count;
? ? ? ? return ids;
? ? }
測試類:
@Resource
? ? RedisIDWorker redisIDWorker;
? ? private ExecutorService es = Executors.newFixedThreadPool(500);
? ? @Test
? ? public void ShowID() throws InterruptedException {
? ? ? ? CountDownLatch countDownLatch = new CountDownLatch(300);
? ? ? ? Runnable task = () -> {
? ? ? ? ? ? for (int i = 0; i < 100; i++) {
? ? ? ? ? ? ? ? long id = redisIDWorker.nextID("order");
? ? ? ? ? ? ? ? System.out.println("id = " + id);
? ? ? ? ? ? }
? ? ? ? ? ? countDownLatch.countDown();
? ? ? ? };
? ? ? ? long startTime = System.currentTimeMillis();
? ? ? ? for (int i = 0; i < 300; i++) {
? ? ? ? ? ? es.submit(task);
? ? ? ? }
? ? ? ? countDownLatch.await();
? ? ? ? long end = System.currentTimeMillis();
? ? ? ? System.out.println("time = " + (end - startTime));
? ? }
原文鏈接:https://blog.csdn.net/nnitxufd/article/details/124022266
相關推薦
- 2022-10-27 LyScript實現指令查詢功能的示例代碼_python
- 2022-05-18 C++?qt實現打開關閉狀態按鈕的代碼_C 語言
- 2022-05-31 docker-compose+nginx部署前后端分離的項目實踐_docker
- 2022-04-01 安裝k8s Error initializing network controller: Error
- 2022-07-18 Linux如何設置SSH免密登錄
- 2022-06-16 c語言單詞搜索的實現_C 語言
- 2023-01-02 Kotlin?RadioGroup與ViewPager實現底層分頁按鈕方法_Android
- 2021-12-12 Docker?Consul概述以及集群環境搭建步驟(圖文詳解)_docker
- 最近更新
-
- 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同步修改后的遠程分支