https://blog.youkuaiyun.com/heroguo007/article/details/78490278
最近使用spirngcloud来搭建分布式项目,遇到插入重复问题,决定用redis生成唯一ID来解决。
/**
* 获取唯一Id
* @param key
* @param hashKey
* @param delta 增加量(不传采用1)
* @return
* @throws BusinessException
*/
public Long incrementHash(String key,String hashKey,Long delta) throws BusinessException{
try {
if (null == delta) {
delta=1L;
}
return redisTemplate.opsForHash().increment(key, hashKey, delta);
} catch (Exception e) {//redis宕机时采用uuid的方式生成唯一id
int first = new Random(10).nextInt(8) + 1;
int randNo=UUID.randomUUID().toString().hashCode();
if (randNo < 0) {
randNo=-randNo;
}
return Long.valueOf(first + String.format("%16d", randNo));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Python全栈学完需要多少钱?
---------------------
作者:tony-guo
来源:优快云
原文:https://blog.youkuaiyun.com/heroguo007/article/details/78490278
版权声明:本文为博主原创文章,转载请附上博文链接!