mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
refactor: optimize FlexIDKeyGenerator.java
This commit is contained in:
parent
5c89401d92
commit
1da3869dbd
@ -33,7 +33,7 @@ import java.util.concurrent.ThreadLocalRandom;
|
|||||||
* 1、每台机器允许最大的并发量为 10w/s。
|
* 1、每台机器允许最大的并发量为 10w/s。
|
||||||
* 2、出现时间回拨,重启机器时,在时间回拨未恢复的情况下,可能出现 id 重复。
|
* 2、出现时间回拨,重启机器时,在时间回拨未恢复的情况下,可能出现 id 重复。
|
||||||
* <p>
|
* <p>
|
||||||
* ID组成:时间(7+)| 毫秒内的时间自增 (00~99:2)| 机器ID(0 ~ 9:1)| 随机数(000~999:3)用于分库分表时,通过 id 取模,保证分布均衡。
|
* ID组成:时间(7+)| 毫秒内的时间自增 (00~99:2)| 机器ID(00 ~ 99:2)| 随机数(00~99:2)用于分库分表时,通过 id 取模,保证分布均衡。
|
||||||
*/
|
*/
|
||||||
public class FlexIDKeyGenerator implements IKeyGenerator {
|
public class FlexIDKeyGenerator implements IKeyGenerator {
|
||||||
|
|
||||||
@ -85,12 +85,14 @@ public class FlexIDKeyGenerator implements IKeyGenerator {
|
|||||||
lastTimeMillis = currentTimeMillis;
|
lastTimeMillis = currentTimeMillis;
|
||||||
|
|
||||||
long diffTimeMillis = currentTimeMillis - INITIAL_TIMESTAMP;
|
long diffTimeMillis = currentTimeMillis - INITIAL_TIMESTAMP;
|
||||||
return diffTimeMillis * 1000000 + clockSeq * 10000 + workId * 1000 + getRandomInt();
|
|
||||||
|
//ID组成:时间(7+)| 毫秒内的时间自增 (00~99:2)| 机器ID(00 ~ 99:2)| 随机数(00~99:2)
|
||||||
|
return diffTimeMillis * 1000000 + clockSeq * 10000 + workId * 100 + getRandomInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int getRandomInt() {
|
private int getRandomInt() {
|
||||||
return ThreadLocalRandom.current().nextInt(1000);
|
return ThreadLocalRandom.current().nextInt(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user