类 Snowflake

java.lang.Object
com.gdxsoft.easyweb.function.Snowflake

public class Snowflake extends Object
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(long datacenterId, long workerId)
     
  • 方法概要

    修饰符和类型
    方法
    说明
    formatId(long id)
    extract and display time stamp, datacenterId, workerId and sequence number information from the given id in humanization format
    long
     
    long
     
    long
    generate an unique and incrementing id
    long[]
    parseId(long id)
    extract time stamp, datacenterId, workerId and sequence number information from the given id
    protected long
    get current time stamp
    show settings of Snowflake
    protected long
    waitNextMillis(long currTimestamp)
    running loop blocking until next millisecond

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • 构造器详细资料

    • 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

      public String toString()
      show settings of Snowflake
      覆盖:
      toString 在类中 Object
    • 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

      public String formatId(long id)
      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