亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

0 | 0001100 10100010 10111110 10001001 01011100 00 | 10001 | 1 1001 | 0000 00000000

Twitter在把存儲系統從MySQL遷移到Cassandra的過程中由于Cassandra沒有順序ID生成機制,snowflake 算法是 twitter 開源的分布式 id 生成算法,

就是

把一個 64 位的 long 型的 id,

1 個 bit 是不用的,

用其中的 41 bit 作為毫秒數,

用 10 bit 作為工作機器 id,

12 bit 作為序列號

 

于是自己開發了一套全局唯一ID生成服務:Snowflake。

1 bit:不用,為啥呢?因為二進制里第一個 bit 為如果是 1,那么都是負數,但是我們生成的 id 都是正數,所以第一個 bit 統一都是 0。

41位的時間序列(精確到毫秒,41位的長度可以使用69年)

10位的機器標識(10位的長度最多支持部署1024個節點)

12位的計數順序號(12位的計數順序號支持每個節點每毫秒產生4096個ID序號)

 

最高位是符號位,始終為0。

優點:高性能,低延遲;獨立的應用;按時間有序。

缺點:需要獨立的開發和部署。

 

package com.NETtyrpc.test.util;public class IdWorker {private final long workerId;private final static long twepoch = 1288834974657L;private long sequence = 0L;private final static long workerIdBits = 4L;public final static long maxWorkerId = -1L ^ -1L << workerIdBits;private final static long sequenceBits = 10L;private final static long workerIdShift = sequenceBits;private final static long timestampLeftShift = sequenceBits + workerIdBits;public final static long sequenceMask = -1L ^ -1L << sequenceBits;private long lastTimestamp = -1L;public IdWorker(final long workerId) {super();if (workerId > this.maxWorkerId || workerId < 0) {throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", this.maxWorkerId));}this.workerId = workerId;}public synchronized long nextId() {long timestamp = this.timeGen();if (this.lastTimestamp == timestamp) {this.sequence = (this.sequence + 1) & this.sequenceMask;if (this.sequence == 0) {System.out.println("###########" + sequenceMask);timestamp = this.tilNextMillis(this.lastTimestamp);}} else {this.sequence = 0;}if (timestamp < this.lastTimestamp) {try {throw new Exception(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds",this.lastTimestamp - timestamp));} catch (Exception e) {e.printStackTrace();}}this.lastTimestamp = timestamp;long nextId = ((timestamp - twepoch << timestampLeftShift)) | (this.workerId << this.workerIdShift)| (this.sequence);System.out.println("timestamp:" + timestamp + ",timestampLeftShift:" + timestampLeftShift + ",nextId:" + nextId+ ",workerId:" + workerId + ",sequence:" + sequence);return nextId;}private long tilNextMillis(final long lastTimestamp) {long timestamp = this.timeGen();while (timestamp <= lastTimestamp) {timestamp = this.timeGen();}return timestamp;}private long timeGen() {return System.currentTimeMillis();}public static void main(String[] args) {IdWorker worker2 = new IdWorker(2);System.out.println(worker2.nextId());}}

分享到:
標簽:snowflake
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定