类 Snowflake
java.lang.Object
com.gdxsoft.easyweb.function.Snowflake
https://github.com/twitter/snowflake
Twitter_Snowflake
SnowFlake的结构如下(每部分用-分开):
0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000
1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0
41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69
10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId
12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号
加起来刚好64位,为一个Long型。
SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。
SnowFlake的结构如下(每部分用-分开):
0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000
1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0
41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69
10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId
12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号
加起来刚好64位,为一个Long型。
SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明formatId(long id) extract and display time stamp, datacenterId, workerId and sequence number information from the given id in humanization formatlonggetEpoch()longlongnextId()generate an unique and incrementing idlong[]parseId(long id) extract time stamp, datacenterId, workerId and sequence number information from the given idprotected longget current time stamptoString()show settings of Snowflakeprotected longwaitNextMillis(long currTimestamp) running loop blocking until next millisecond
-
构造器详细资料
-
Snowflake
public Snowflake(long datacenterId, long workerId) - 参数:
datacenterId- data center number the process running on, value range: [0,31]workerId- machine or process number, value range: [0,31]
-
-
方法详细资料
-
nextId
public long nextId()generate an unique and incrementing id- 返回:
- id
-
getWaitCount
public long getWaitCount()- 返回:
- the amount of calling
waitNextMillis(long)method
-
waitNextMillis
protected long waitNextMillis(long currTimestamp) running loop blocking until next millisecond- 参数:
currTimestamp- current time stamp- 返回:
- current time stamp in millisecond
-
timestampGen
protected long timestampGen()get current time stamp- 返回:
- current time stamp in millisecond
-
toString
show settings of Snowflake -
getEpoch
public long getEpoch() -
parseId
public long[] parseId(long id) extract time stamp, datacenterId, workerId and sequence number information from the given id- 参数:
id- a snowflake id generated by this object- 返回:
- an array containing time stamp, datacenterId, workerId and sequence number
-
formatId
extract and display time stamp, datacenterId, workerId and sequence number information from the given id in humanization format- 参数:
id- snowflake id in Long format- 返回:
- snowflake id in String format
-