黑马点评–优惠卷秒杀
全局ID生成器:
是一种在分布式系统下用来生成全局唯一ID的工具,一般要满足下列特性:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lvc2qD5J-1668574944843)(C:\Users\20745\AppData\Roaming\Typora\typora-user-images\image-20221113221848596.png)]](https://i-blog.csdnimg.cn/blog_migrate/88966fde7e6a53aaaf26bb4598e7a014.png)
为了增加ID的安全性,我们可以不直接使用Redis自增的数值,而是拼接一些其它信息:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FM7N8l3N-1668574944844)(C:\Users\20745\AppData\Roaming\Typora\typora-user-images\image-20221113222812554.png)]](https://i-blog.csdnimg.cn/blog_migrate/5c2ee8a2ac3a82d1e8ccbeb157aff739.png)
Redis自增ID策略:
- 每天一个key,方便统计订单量
- iD结构是时间戳+计数器
/**
* 开始时间戳
*/
private static final long BEGIN_TIMESTAMP = 1640995200;
/**
* 序列号的位数
*/
private static final int COUNT_BITS = 32;
/**
* id生成策略
*
* @param keyPrefix 业务前缀
* @return
*/
@Resource
private StringRedisTemplate stringRedisTemplate;
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自增长
Long count = stringRedisTemplate.opsForValue().increment("icr:" + keyPrefix + ":" + date);
//3.拼接并返回
return timestamp << COUNT_BITS | count;
}
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);
}
测试ID自增生成策略:
500个线程每一个线程生成100id一共50000个id花的时间:
private ExecutorService service = Executors.newFixedThreadPool(500);
@Test
void testIdWorker() throws InterruptedException {
CountDownLatch latch =

最低0.47元/天 解锁文章
1726

被折叠的 条评论
为什么被折叠?



