在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:snowflake开源软件地址:https://gitee.com/teamlint/snowflake开源软件介绍:SnowflakeID FormatSnowflake is a distributed unique ID generator inspired by Twitter's Snowflake. By default, a Snowflake ID is composed of
Custom FormatYou can alter the number of bits used for the node id and step number (sequence) by New(NodeBits(16),SeqBits(6)) Custom Start TimeBy default this package uses the Twitter Epoch of 61026175693(UTC 1971-12-08 15:42:55.693). You can set your own epoch value by How it WorksEach time you generate an ID, it works, like this.
The default format shown below. +--------------------------------------------------------------------------+| 1 Bit Unused | 43 Bit Timestamp | 10 Bit NodeID | 10 Bit Sequence ID |+--------------------------------------------------------------------------+ Using the default settings, this allows for 1024 unique IDs to be generated every millisecond, per Node ID. Getting StartedInstallingThis assumes you already have a working Go environment, if not please seethis page first. go get github.com/teamlint/snowflake UsageImport the package into your project then construct a new snowflake Node using aunique node number. The default settings permit a node number range from 0 to 1023.If you have set a custom NodeBits value, you will need to calculate what yournode number range will be. With the node object call the ID() method togenerate and return a unique snowflake ID. Keep in mind that each node you create must have a unique node number, evenacross multiple servers. If you do not keep node numbers unique the generatorcannot guarantee unique IDs across all nodes. The function New creates a new Snowflake instance. func New(opts ...Option) (*Snowflake, error) You can configure Snowflake by the Option function: The Option function: type Option func(*Options)type Options struct { startTime int64 // 开始时间 node int64 // 节点 ID, 0 - 1023, 默认 0, 优先使用环境变量 SNOWFLAKE_NODE, 其次使用私有 IP 地址进行节点掩码计算 timeBits uint8 // 时间位数, 默认 43 位 nodeBits uint8 // 节点位数, 默认 10 位 seqBits uint8 // 递增序列位数, 默认 10 位} The option function provided are as follows:
In order to get a new unique ID, you just have to call the method ID. func (sf *Snowflake) ID() ID Example Program: package mainimport ( "fmt" "github.com/teamlint/snowflake")func main() { opts := []Option{Verbose(), NodeBits(8), StartTime(1314220021721)} sf, err := snowflake.New(opts...) if err != nil { fmt.Println(err) return } // Generate a snowflake ID. id := sf.ID() // Print out the ID in a few different ways. fmt.Printf("Int64 ID: %d\n", id) fmt.Printf("String ID: %s\n", id) fmt.Printf("Base2 ID: %s\n", id.Base2()) fmt.Printf("Base64 ID: %s\n", id.Base64()) // Print out the ID's timestamp fmt.Printf("ID Time : %d\n", id.Time(opts...)) // Print out the ID's node number fmt.Printf("ID Node : %d\n", id.Node(opts...)) // Print out the ID's sequence number fmt.Printf("ID Step : %d\n", id.Seq(opts...)) // Generate and print, all in one. fmt.Printf("ID : %d\n", sf.ID().Int64())} PerformanceWith default settings, this snowflake generator should be sufficiently fastenough on most systems to generate 1024 unique ID's per millisecond. This isthe maximum that the snowflake ID format supports. That is, around 110-112nanoseconds per operation. Since the snowflake generator is single threaded the primary limitation will bethe maximum speed of a single processor on your system. To benchmark the generator on your system run the following command inside thesnowflake package directory. $ go test -run=none -bench=. |
请发表评论