redis 生成分布式ID

1、根据需求生成的ID,需要在32位范围中。
// 应用程序 4核, 8G window.
// redis 4核, 8G window.
Map<Long,Long> map = new HashMap<>();
// 测试redis 在60万的并发下,获取ID
Long start = System.currentTimeMillis();
int i=0;
while(i<600000){
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
Long value = redisTemplate.opsForValue().increment(redisKey,1);
if (null != map.get(value)) {
System.out.println(value+"已经存在!");
}else{
map.put(value,value);
}
}
});
i++;
}
Long end = System.currentTimeMillis();
System.out.println("finish! time="+(end-start));

运行结果:finish! time=2904

线程增加10倍,当把线程数量改成了600万,花费了 finish! time=34245。等待了34秒来是用户无法接受。


算法:
 twitter 提供了 snowflake算法 64位





posted on 2019-05-24 16:20  苦大师  阅读(766)  评论(0编辑  收藏  举报