根據 Twitter SnowFlake 算法, 實現的分佈式線程安全 UID 生成器node
線程安全的 UID 生成器git
綠色可插拔,無需依賴 Redis,Mysql,無狀態github
適合分佈式系統算法
實現 Twitter SnowFlake 理論sql
0 41 51 64 +---------------+----------------+-----------+ |timestamp(ms) | worker node id | sequence | +---------------+----------------+-----------+ id = timestamp | workerid | sequence (eg. 1451063443347648410)
由三部分與運算組合而成,分別是毫秒級別的時間戳,機器 workerid, 以及爲了解決衝突的序列號安全
go get github.com/zheng-ji/goSnowFlake
import ( "fmt" "github.com/zheng-ji/goSnowFlake" ) func main() { // Params: Given the workerId, 0 < workerId < 1024 iw, err := goSnowFlake.NewIdWorker(1) if err!= nil { fmt.Println(err) } for i := 0; i < 100; i++ { if id, err := iw.NextId(); err != nill { fmt.Println(id) } } }
Copyright (c) 2015 by zheng-ji released under MIT License.ui