Commit 242783f5 authored by zhiguo.liu's avatar zhiguo.liu

# 去掉 timeBits 配置项

parent 43762b4d
...@@ -20,40 +20,38 @@ import java.util.concurrent.TimeUnit; ...@@ -20,40 +20,38 @@ import java.util.concurrent.TimeUnit;
/** /**
* Represents an implementation of {@link UidGenerator} * Represents an implementation of {@link UidGenerator}
* 基于百度开源项目 uid-generator 的增强版,Snowflake Java 实现版本。项目 Github:https://github.com/baidu/uid-generator * 基于百度开源项目 uid-generator 的增强版,Snowflake Java 实现版本。项目 Github:https://github.com/baidu/uid-generator
* * <p>
* uid-generator 通过对 64 位数字分区来生成唯一 ID,由以下组成: * uid-generator 通过对 64 位数字分区来生成唯一 ID,由以下组成:
* * <p>
* -----------------------------------------------------------------------------------
* | sign | delta seconds | worker id | sequence
* ----------------------------------------------------------------------------------- * -----------------------------------------------------------------------------------
* | sign | delta seconds | worker id | sequence * 1bits 28bits 22bits 13bits
* -----------------------------------------------------------------------------------
* 1bits 28bits 22bits 13bits
* ----------------------------------------------------------------------------------- * -----------------------------------------------------------------------------------
* 其中 delta seconds 为 当前时间 - 指定起始时间。 * 其中 delta seconds 为 当前时间 - 指定起始时间。
* 该版本有三个问题 * 该版本有三个问题
* 1. delta seconds 位数有限,28bits 也只能允许运行 8.7 年左右。 * 1. delta seconds 位数有限,28bits 也只能允许运行 8.7 年左右。
* 2. worker id 生成号码为用后即弃,可容纳重启次数有限。 * 2. worker id 生成号码为用后即弃,可容纳重启次数有限。
* 3. 微服务分布式的情况下,无法使用统一数据源,则不同服务生成 worker id 时会重复 * 3. 微服务分布式的情况下,无法使用统一数据源,则不同服务生成 worker id 时会重复
* * <p>
* 于是做出以下改进 * 于是做出以下改进
* 1. worker id 拆分成 data center id,每个服务通过约定指定自己的 data center id 。 * 1. worker id 拆分成 data center id,每个服务通过约定指定自己的 data center id 。
* 2. worker id 通过 redis 自增指定,设计为首尾相连的环形,自增数字达到设定的最大值时,会从0开始。 * 2. worker id 通过 redis 自增指定,设计为首尾相连的环形,自增数字达到设定的最大值时,会从0开始。
* 2. 不限制使用 delta seconds 的位数,则实现了无限时间的使用。当位数增长到 64 为后,改用 BigInteger 的位运算实现。 * 2. 不限制使用 delta seconds 的位数,则实现了无限时间的使用。当位数增长到 64 为后,改用 BigInteger 的位运算实现。
* * <p>
* 经测试,BigInteger 实现时,性能降低 60% 左右,每秒发号约为 100w~150w。 * 经测试,BigInteger 实现时,性能降低 60% 左右,每秒发号约为 100w~150w。
* 现在 uid 由以下组成 * 现在 uid 由以下组成
* --------------------------------------------------------------------------------------------------------- * ---------------------------------------------------------------------------------------------------------
* | sign(length < 64) | delta seconds (unlimited) | data center id | worker id | sequence * | sign(length < 64) | delta seconds (unlimited) | data center id | worker id | sequence
* --------------------------------------------------------------------------------------------------------- * ---------------------------------------------------------------------------------------------------------
* 1bits 28bits 22bits 22bits 13bits * 1bits 28bits 22bits 22bits 13bits
* --------------------------------------------------------------------------------------------------------- * ---------------------------------------------------------------------------------------------------------
* 其中 data center id + worker id + sequence 设定的位数不大于 63。 * 其中 data center id + worker id + sequence 设定的位数不大于 63。
* * <p>
* 使用注意: * 使用注意:
* 1. 号码的位数不固定,会随着时间增长。data center id + worker id + sequence 总数设定越大,号码位数越长 * 1. 号码的位数不固定,会随着时间增长。data center id + worker id + sequence 总数设定越大,号码位数越长
* 2. 各个分区的位数、起始时间一旦设定完成投入使用,则后续不能更改。否则会导致发号重复。 * 2. 各个分区的位数、起始时间一旦设定完成投入使用,则后续不能更改。否则会导致发号重复。
* *
*
*
* @author zhiguo.liu * @author zhiguo.liu
*/ */
@Component @Component
...@@ -66,8 +64,6 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean { ...@@ -66,8 +64,6 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
/** /**
* Bits allocate * Bits allocate
*/ */
@Value("${uid.timeBits:28}")
protected int timeBits;
@Value("${uid.dataCenterIdBits:9}") @Value("${uid.dataCenterIdBits:9}")
protected int dataCenterIdBits; protected int dataCenterIdBits;
@Value("${uid.workerBits:13}") @Value("${uid.workerBits:13}")
...@@ -104,6 +100,7 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean { ...@@ -104,6 +100,7 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
// initialize bits allocator // initialize bits allocator
int timeBits = 64 - 1 - dataCenterIdBits - workerBits - seqBits;
bitsAllocator = new BitsAllocator(timeBits, dataCenterIdBits, workerBits, seqBits); bitsAllocator = new BitsAllocator(timeBits, dataCenterIdBits, workerBits, seqBits);
// initialize worker id // initialize worker id
workerId = workerIdAssigner.assignWorkerId(dataCenterId, bitsAllocator); workerId = workerIdAssigner.assignWorkerId(dataCenterId, bitsAllocator);
...@@ -220,12 +217,6 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean { ...@@ -220,12 +217,6 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); return TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
} }
public void setTimeBits(int timeBits) {
if (timeBits > 0) {
this.timeBits = timeBits;
}
}
public void setWorkerBits(int workerBits) { public void setWorkerBits(int workerBits) {
if (workerBits > 0) { if (workerBits > 0) {
this.workerBits = workerBits; this.workerBits = workerBits;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment