当前位置:网站首页>Générateur d'identification distribué basé sur redis
Générateur d'identification distribué basé sur redis
2022-07-06 12:12:00 【Un bâton.】
Basé surRedisDistribution deIDGénérateur
IDStratégie d'auto - augmentation
- Un par jourkey,Statistiques pratiques sur le volume des commandes
- IDLa construction est Horodatage + Compteur
IDLes composantes de

- Bits de symbole:1bit,Pour toujours0
- Horodatage:31bit,En secondes,De2022Année1Début du mois,Peut être utilisé68Année,Ou sur demande,Modifier pour chaque minute、Compteurs horaires ou quotidiens,Peut augmenter le temps disponible.
- Numéro de série:32bit,Compteurs quotidiens,Soutenir la production quotidienne2^32C'est différent.ID,Ou sur demande,Modifier à l'heure、Compteurs par minute ou par seconde,Mais il doit correspondre à l'horodatage,Peut être agrandiIDNombre.
Voici un horodatage en secondes et un compteur de sérialisation en joursIDGénérateur.
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 Un poteau. * @version 1.0 * @date 2022/6/20 20:29 */
@Component
public class RedisIdGenerator {
/** * Horodatage de démarrage */
private static final long BEGIN_TIMESTAMP = 1640995200L;
/** * Nombre de chiffres du numéro de série */
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. Générer un TIMESTAMP
LocalDateTime now = LocalDateTime.now();
long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
long timestamp = nowSecond - BEGIN_TIMESTAMP;
// 2. Générer un numéro de série
// 2.1 Obtenir la date actuelle,Au jour le jour
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
// 2.2 Accèsredis Valeur d'auto - croissance
long increment = redisTemplate.opsForValue().increment("id:" + keyPrefix + ":" + date);
// 3. Attelage et retour
return increment << COUNT_BITS | timestamp;
}
}
Autre unique au niveau mondial IDGénérer une politique
- UUID
- RedisAuto - augmentation
- snowflakeAlgorithmes
- Base de données auto - incrémentale
边栏推荐
猜你喜欢

The dolphin scheduler remotely executes shell scripts through the expect command

Detailed explanation of 5g working principle (explanation & illustration)

电商数据分析--用户行为分析

level16

基于Redis的分布式锁 以及 超详细的改进思路

Arduino JSON data information parsing

MP3mini播放模块arduino<DFRobotDFPlayerMini.h>函数详解

ES6语法总结--下篇(进阶篇 ES6~ES11)

Basic knowledge of lithium battery

JS regular expression basic knowledge learning
随机推荐
嵌入式启动流程
Detailed explanation of 5g working principle (explanation & illustration)
open-mmlab labelImg mmdetection
Kconfig Kbuild
Detailed explanation of Union [C language]
Togglebutton realizes the effect of switching lights
gcc 编译选项
Arduino uno R3 register writing method (1) -- pin level state change
[golang] leetcode intermediate - fill in the next right node pointer of each node & the k-smallest element in the binary search tree
Reno7 60W超级闪充充电架构
Comparaison des solutions pour la plate - forme mobile Qualcomm & MTK & Kirin USB 3.0
ESP学习问题记录
Correspondence between STM32 model and contex M
Machine learning -- linear regression (sklearn)
Rough analysis of map file
AMBA、AHB、APB、AXI的理解
. elf . map . list . Hex file
Custom view puzzle getcolor r.color The color obtained by colorprimary is incorrect
优先级反转与死锁
Mp3mini playback module Arduino < dfrobotdfplayermini H> function explanation