redis 生成分佈式ID

一、根據需求生成的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位
相關文章
相關標籤/搜索