前言
全局ID生成器,是一种在分布式系统下用来生成全局唯一ID的工具,一般要满足下列特性:唯一性、高可用、高性能、递增性、安全性。另外雪花算法也能实现类似的功能。
代码实现
package com.hmdp.utils;
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;
/**
* @auther Kou
* @date 2022/7/2 21:52
*/
@Component
public class RedisIdworker {
private StringRedisTemplate stringRedisTemplate;
/**
* 使用构造函数,或者使用 @Autowired\@Resource将StringRedisTemplate注入
*
* @param stringRedisTemplate
*/
public RedisIdworker(StringRedisTemplate stringRedisTemplate) {
this.stringRedisTemplate = stringRedisTemplate;
}
/**
* 定义一个开始的时间戳
*/
private static final long BEGIN_TIMESTAMP = 1640995200L;
/**
* 序列号的位数
*/
private static final int COUNT_BITS = 32;
/**
* 开始时间戳可以用以main函数生成,或者百度时间戳转换工具,输入具体时间,直接转换成对应的时间戳
*/
public static void main(String[] args) {
LocalDateTime time = LocalDateTime.of(2022, 1, 1, 0, 0, 0);
//获得具体的秒数
long second = time.toEpochSecond(ZoneOffset.UTC);
System.out.println(second);// 打印出来结果为:1640995200
}
public long nextId(String keyPrefix) {
// 获取当前的秒数
long newSecond = LocalDateTime.now().toEpochSecond(ZoneOffset.UTC);
// 计算出需要拼接的时间戳,当前时间减去开始时间戳
long times = newSecond - BEGIN_TIMESTAMP;
//获取当前日期精确到天
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
// 生成序列号
long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + date);
// 拼接并返回
return count << COUNT_BITS | count;
}
}