paddy 1246fb0595 init | il y a 2 ans | |
---|---|---|
api | il y a 2 ans | |
client | il y a 2 ans | |
cmd | il y a 2 ans | |
configs | il y a 2 ans | |
entity | il y a 2 ans | |
library | il y a 2 ans | |
repository | il y a 2 ans | |
server | il y a 2 ans | |
service | il y a 2 ans | |
.gitignore | il y a 2 ans | |
LICENSE | il y a 2 ans | |
README.md | il y a 2 ans | |
gitconfig.sh | il y a 2 ans | |
go.mod | il y a 2 ans |
gid 是使用golang开发的生成分布式Id系统,基于数据库号段算法实现
go get github.com/hwholiday/gid/v2@gidV2
//go mod 内
//github.com/hwholiday/gid/v2 v2.0.6
package main
import (
"context"
"fmt"
gidSrv "github.com/hwholiday/gid/v2/api"
)
func main() {
cli, err := InitGrpc([]string{"127.0.0.1:2379"}, 15)
c, _ := cli.GetGidGrpcClient()
res, err := c.GetId(context.TODO(), &gidSrv.ReqId{
BizTag: "111",
})
fmt.Println(res, err)
}
create database gid;
use gid;
create table segments
(
biz_tag varchar(128) not null,
max_id bigint null,
step int null,
create_time bigint null,
update_time bigint null,
constraint segments_pk
primary key (biz_tag)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_bin;
go get github.com/hwholiday/gid/v2@gidV2 OR git clone -b gidV2 https://github.com/hwholiday/gid.git
cd gid/cmd
go build -o gid
./gid -conf ./gid.toml
BenchmarkService_GetId-4 2046296 583 ns/op
Begin
UPDATE table SET max_id=max_id+step WHERE biz_tag=xxx
SELECT tag, max_id, step FROM table WHERE biz_tag=xxx
Commit