DefaultKeyGenerator 生成器生成的主键不能产生奇数
主键的最后12位是序列号,该序列号只有在同一秒内才会自增
主键最后的一个二进制位会是0 , 这样就不能产生奇数了
代码如下 sequence 在非同一秒内都是0 public synchronized Number generateKey() { long currentMillis = timeService.getCurrentMillis(); Preconditions.checkState(lastTime <= currentMillis, "Clock is moving backwards, last time is %d milliseconds, current time is %d milliseconds", lastTime, currentMillis); if (lastTime == currentMillis) { if (0L == (sequence = ++sequence & SEQUENCE_MASK)) { currentMillis = waitUntilNextTime(currentMillis); } } else { sequence = 0; } lastTime = currentMillis; if (log.isDebugEnabled()) { log.debug("{}-{}-{}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date(lastTime)), workerId, sequence); } return ((currentMillis - EPOCH) << TIMESTAMP_LEFT_SHIFT_BITS) | (workerId << WORKER_ID_LEFT_SHIFT_BITS) | sequence; }
请先仔细阅读文档,如无充足时间,请至少阅读下faq
嗯,确实没看faq,我是在测试过程中,发现数据都到一个分片去了