全局ID生成器,是一种在分布式系统下用来生成全局唯一ID的工具,一般要满足下列特性:
生成策略有:
UUID
Redis自增
雪花算法
数据库自增
Redis自增ID策略
ID的组成部分:符号位:1bit,永远为0
时间戳:31bit,以秒为单位,可以使用69年
序列号:32bit,秒内的计数器,支持每秒产生2^32个不同ID
package com.hmdp.utils;
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;
@Component
public class RedisIdWorker {
//定义一个开始的时间戳
private static final long BEGIN_TIMESTAMP=1640995200L;//比如用2022.1.1 0:00:00改时间的时间戳
//序列号的位数
private static final int COUNT_BITS=32;
private static 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.生成序列号,使用redis自增长
//2.1获取当前日期,精确到天
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
//2.2自增长
long count=stringRedisTemplate.opsForValue().increment("icr:"+keyPrefix+":"+date);//每次自增1
//3.拼接并返回
//符号位0+时间戳31位+序列号32位 总共64位
//时间戳左移32位
return timestamp << COUNT_BITS | count;//位运算
}
/* public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 0, 0, 0);
long second = localDateTime.toEpochSecond(ZoneOffset.UTC);
System.out.println(second);
LocalDateTime now = LocalDateTime.now();
long nowSecond = now.toEpochSecond(ZoneOffset.UTC);
String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
System.out.println(date);
long count=stringRedisTemplate.opsForValue().increment("icr:"+":"+date);
System.out.println(count);
}*/
}