分离式部署中使用mybatisPlus的雪花算法生成id会重复

Scroll Down

docker容器中用mybatisPlus的雪花算法生成id重复

最近上线一个新的系统,用的是SSM加mybatisPlus,中间生成id的时候,用的是mybatisplus的雪花算法生成的,但是在生产的docker容器中,同时启动4个服务,在并发高的情况下就出现了id重复,导致无法插入数据库的问题,所以就来看mybatisplus的算法到底有什么问题.

雪花算法介绍

Twitter的雪花算法SnowFlake,使用Java语言实现。

SnowFlake算法产生的ID是一个64位的整型,结构如下(每一部分用“-”符号分隔):

0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000
1位标识部分,在java中由于long的最高位是符号位,正数是0,负数是1,一般生成的ID为正数,所以为0;

41位时间戳部分,这个是毫秒级的时间,一般实现上不会存储当前的时间戳,而是时间戳的差值(当前时间-固定的开始时间),这样可以使产生的ID从更小值开始;41位的时间戳可以使用69年,(1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69年;

10位节点部分,Twitter实现中使用前5位作为数据中心标识,后5位作为机器标识,可以部署1024个节点;

12位序列号部分,支持同一毫秒内同一个节点可以生成4096个ID;

SnowFlake算法生成的ID大致上是按照时间递增的,用在分布式系统中时,需要注意数据中心标识和机器标识必须唯一,这样就能保证每个节点生成的ID都是唯一的。或许我们不一定都需要像上面那样使用5位作为数据中心标识,5位作为机器标识,可以根据我们业务的需要,灵活分配节点部分,如:若不需要数据中心,完全可以使用全部10位作为机器标识;若数据中心不多,也可以只使用3位作为数据中心,7位作为机器标识。

snowflake生成的ID整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由datacenter和workerId作区分),并且效率较高。据说:snowflake每秒能够产生26万个ID。

代码分析

首先看数据中心,我们这里数据中心用的是获取本台机器的网卡地址,在通过将网卡地址最后一位&0xFF(获取最后8位),倒数第二位网卡地址&0xFF00(获取中间8位)再左移8位得到,最后再右移6位并对32位进行除余得到结果

机器码,机器码是通过获取jvm的id,jvm的name一般都是数字@字符串, 27@b2f9546258eb,然后取出第一串的数字,获取这串数字的hashcode,再进行&0xFFFF运算(获取最后16位),再对32进行除余获得结果.

为什么要除以32,是因为机器码和数据中心各占5位,5位的话最大值就是32,不能超出32,所以这里要进行对32除余

原有代码:


代码看完之后,感觉发现不了什么问题,只能打日志进行输出

输出日志后进行对比,总共四台服务器,输出结果为:

118
2019-11-01  16:55:58.236 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac0->id = [2]
2019-11-01  16:55:58.236 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac1->id = [66]
2019-11-01  16:55:58.236 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac2->id = [10]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac3->id = [0]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac4->id = [0]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac5->id = [99]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress0->id = [10]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress1->id = [0]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress2->id = [0]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress3->id = [99]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 5->id = [1]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->datacenterId = [1]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - N->name = [27@b2f9546258eb]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->workerId = [22]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 6->timestamp = [1572598558236]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 7->lastTimestamp = [-1]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 8->(timestamp - twepoch) = [283763583579]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 9->(timestampLeftShift) = [22]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 10->(datacenterId << datacenterIdShift) = [131072]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 11->(workerId << workerIdShift) = [90112]
2019-11-01  16:55:58.237 [DubboServerHandler-10.0.0.99:20880-thread-9] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 12->sequence = [0]

119
2019-11-01  16:54:16.413 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac0->id = [2]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac1->id = [66]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac2->id = [10]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac3->id = [0]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac4->id = [0]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac5->id = [97]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress0->id = [10]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress1->id = [0]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress2->id = [0]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress3->id = [97]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 5->id = [1]
2019-11-01  16:54:16.414 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->datacenterId = [1]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - N->name = [27@36c87fb78a26]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->workerId = [22]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac0->id = [2]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac1->id = [66]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac2->id = [10]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac3->id = [0]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac4->id = [0]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac5->id = [97]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress0->id = [10]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress1->id = [0]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress2->id = [0]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress3->id = [97]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 5->id = [1]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->datacenterId = [1]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - N->name = [27@36c87fb78a26]
2019-11-01  16:54:16.415 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->workerId = [22]
2019-11-01  16:54:16.417 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 6->timestamp = [1572598456416]
2019-11-01  16:54:16.417 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 7->lastTimestamp = [-1]
2019-11-01  16:54:16.417 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 8->(timestamp - twepoch) = [283763481759]
2019-11-01  16:54:16.417 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 9->(timestampLeftShift) = [22]
2019-11-01  16:54:16.417 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 10->(datacenterId << datacenterIdShift) = [131072]
2019-11-01  16:54:16.418 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 11->(workerId << workerIdShift) = [90112]
2019-11-01  16:54:16.418 [DubboServerHandler-10.0.0.97:20880-thread-12] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 12->sequence = [0]

239
2019-11-01  16:56:47.239 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac0->id = [2]
2019-11-01  16:56:47.239 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac1->id = [66]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac2->id = [10]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac3->id = [0]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac4->id = [0]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac5->id = [96]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress0->id = [10]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress1->id = [0]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress2->id = [0]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress3->id = [96]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 5->id = [1]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->datacenterId = [1]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - N->name = [27@04d3a8d92dbf]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->workerId = [22]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac0->id = [2]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac1->id = [66]
2019-11-01  16:56:47.240 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac2->id = [10]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac3->id = [0]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac4->id = [0]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac5->id = [96]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress0->id = [10]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress1->id = [0]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress2->id = [0]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress3->id = [96]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 5->id = [1]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->datacenterId = [1]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - N->name = [27@04d3a8d92dbf]
2019-11-01  16:56:47.241 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->workerId = [22]
2019-11-01  16:56:47.243 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 6->timestamp = [1572598607242]
2019-11-01  16:56:47.243 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 7->lastTimestamp = [-1]
2019-11-01  16:56:47.243 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 8->(timestamp - twepoch) = [283763632585]
2019-11-01  16:56:47.243 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 9->(timestampLeftShift) = [22]
2019-11-01  16:56:47.243 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 10->(datacenterId << datacenterIdShift) = [131072]
2019-11-01  16:56:47.243 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 11->(workerId << workerIdShift) = [90112]
2019-11-01  16:56:47.243 [DubboServerHandler-10.0.0.96:20880-thread-4] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 12->sequence = [0]

240


2019-11-01  16:57:07.567 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac0->id = [2]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac1->id = [66]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac2->id = [10]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac3->id = [0]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac4->id = [0]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac5->id = [98]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress0->id = [10]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress1->id = [0]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress2->id = [0]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress3->id = [98]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 5->id = [1]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->datacenterId = [1]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - N->name = [27@66be61263171]
2019-11-01  16:57:07.568 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->workerId = [22]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac0->id = [2]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac1->id = [66]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac2->id = [10]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac3->id = [0]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac4->id = [0]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - mac5->id = [98]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress0->id = [10]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress1->id = [0]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress2->id = [0]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - ipAdress3->id = [98]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 5->id = [1]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->datacenterId = [1]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - N->name = [27@66be61263171]
2019-11-01  16:57:07.569 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 1->workerId = [22]
2019-11-01  16:57:07.571 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 6->timestamp = [1572598627570]
2019-11-01  16:57:07.571 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 7->lastTimestamp = [-1]
2019-11-01  16:57:07.571 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 8->(timestamp - twepoch) = [283763652913]
2019-11-01  16:57:07.571 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 9->(timestampLeftShift) = [22]
2019-11-01  16:57:07.571 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 10->(datacenterId << datacenterIdShift) = [131072]
2019-11-01  16:57:07.571 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 11->(workerId << workerIdShift) = [90112]
2019-11-01  16:57:07.571 [DubboServerHandler-10.0.0.98:20880-thread-17] [] [] ERROR com.baomidou.mybatisplus.toolkit.Sequence - 12->sequence = [0]

对比之后发现是由于网卡地址太过接近,一通位运算之后导致数据差异太小,最后获取到的数据中心的id一直为1,然后有三台机器获取到的是相同的jvm的ID(很奇怪),导致数据中心+机器码都相同,在并发高一点的情况下,因为不同的服务,但是却有相同的数据标识,机器码,时间戳导致会生成重复id了,这里我对他进行了一点改动,获取IP地址,并对每个IP地址取后8位,然后累计求和并对32除余,获得的值四台机器就都是唯一的了,如果还需要对机器码进行去重的话,可以考虑在机器码中不要获取jvm的ID,获取服务本身的hostName,这样保证机器码+数据标识 唯一,即时是同一个时间戳,也不会产生重复的记录.

int[] ints = StringUtils.toCodePoints(SystemUtils.getHostName());
        int sums = 0;
        for (int i: ints) {
            sums += i;
        }
        return (long)(sums % 32);