From 18f02272c1ec15882072b8bbe3d64c319080975a Mon Sep 17 00:00:00 2001 From: Xargin Date: Mon, 9 Jul 2018 20:05:21 +0800 Subject: [PATCH] update di --- ch6-cloud/ch6-07-dist-id.md | 38 +++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/ch6-cloud/ch6-07-dist-id.md b/ch6-cloud/ch6-07-dist-id.md index 7039fb5..5fd37f6 100644 --- a/ch6-cloud/ch6-07-dist-id.md +++ b/ch6-cloud/ch6-07-dist-id.md @@ -66,6 +66,40 @@ mysql> select last_insert_id(); ## 开源实例 -gosnowflake +`github.com/bwmarrin/snowflake` 是一个相当轻量化的 snowflake 的 Go 实现。其文档指出: -sonyflake \ No newline at end of file +``` ++--------------------------------------------------------------------------+ +| 1 Bit Unused | 41 Bit Timestamp | 10 Bit NodeID | 12 Bit Sequence ID | ++--------------------------------------------------------------------------+ +``` + +和标准的 snowflake 完全一致。使用上比较简单: + +```go +package main + +import ( + "fmt" + "os" + + "github.com/bwmarrin/snowflake" +) + +func main() { + n, err := snowflake.NewNode(1) + if err != nil { + println(err) + os.Exit(1) + } + + for i := 0; i < 3; i++ { + id := n.Generate() + fmt.Println("id", id) + fmt.Println("node: ", id.Node(), "step: ", id.Step(), "time: ", id.Time(), "\n") + } +} + +``` + +sonyflake