基于Redis的分布式ID生成器

ID自增策略

  • 每天一个key,方便统计订单量
  • ID构造是 时间戳 + 计数器

ID的组成部分

image-20220621171917673

  • 符号位:1bit,永远为0
  • 时间戳:31bit,以秒为单位,从2022年1月开始计数,可以使用68年,也可以根据需求,修改为每分钟、每小时或每天的计数器,可以增大可用时间。
  • 序列号:32bit,每天的计数器,支持每天产生2^32个不同ID,也可以根据需求,修改为每小时、每分钟或每秒的计数器,但需要和时间戳相配合,可以增大ID数量。

这里是以秒为单位的时间戳和以天为单位的序列化计数器组成的ID生成器。

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 阿杆
 * @version 1.0
 * @date 2022/6/20 20:29
 */
@Component
public class RedisIdGenerator {

	/**
	 * 开始时间戳
	 */
	private static final long BEGIN_TIMESTAMP = 1640995200L;

	/**
	 * 序列号的位数
	 */
	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. 生成时间戳
		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"));
		// 2.2 获取redis自增长值
		long increment = redisTemplate.opsForValue().increment("id:" + keyPrefix + ":" + date);

		// 3. 拼接并返回
		return increment << COUNT_BITS | timestamp;
	}

}

其他全局唯一ID生成策略

  • UUID
  • Redis自增
  • snowflake算法
  • 数据库自增
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐