Snowflake class

Twitter的Snowflake 算法
分布式系统中,有一些需要使用全局唯一ID的场景,有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。

snowflake的结构如下(每部分用-分开):

符号位(1bit)- 时间戳相对值(41bit)- 数据中心标志(5bit)- 机器标志(5bit)- 递增序号(12bit)
0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000

第一位为未使用(符号位表示正数),接下来的41位为毫秒级时间(41位的长度可以使用69年)
然后是5位datacenterId和5位workerId(10位的长度最多支持部署1024个节点)
最后12位是毫秒内的计数(12位的计数顺序号支持每个节点每毫秒产生4096个ID序号)

并且可以通过生成的id反推出生成时间,datacenterId和workerId

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

getDataCenterId(int id) int
根据Snowflake的ID,获取数据中心id
getGenerateDateTime(int id) int
根据Snowflake的ID,获取生成时间
getWorkerId(int id) int
根据Snowflake的ID,获取机器id
nextId() int
下一个ID
nextIdStr() String
下一个ID(字符串形式)
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

getInstance({DateTime? epochDate, int? workerId, int? dataCenterId, int timeOffset = _defaultTimeOffset}) Future<Snowflake>
获取实例
getInstanceSync({DateTime? epochDate, int? workerId, required int dataCenterId, int timeOffset = _defaultTimeOffset}) Snowflake
获取实例的同步方法,此方法dataCenterId不能为空