服装网站模板免费下载,响应式电影资讯网站,网络建设费计入哪个科目,wordpress页面标题居中分布式id生成数据库号段算法的golang实现 介绍项目结构使用说明核心流程说明1. 定义id生成器结构体2. id生成器共有Monitor#xff0c;GetOne, Close三个对外暴露的方法。3. 数据表结构 参与贡献 介绍
项目地址#xff1a;gitee#xff1b;github 本项目主要利用go语言(go1… 分布式id生成数据库号段算法的golang实现 介绍项目结构使用说明核心流程说明1. 定义id生成器结构体2. id生成器共有MonitorGetOne, Close三个对外暴露的方法。3. 数据表结构 参与贡献 介绍
项目地址giteegithub 本项目主要利用go语言(go1.20)实现了一种id生成器并提供了http和grpc两种访问方式。项目中采用的生成算法主要基于数据库号段算法实现。关于这个算法可以参考 美团Left。
项目结构
main.go - 程序入口项目初始化并实现了平滑停服
router - 路由
controller - api接口
model - 数据模型定义
dao - 数据表操作
logic - 逻辑操作grpcserver - grpc服务器idsequence - 实现了数据号段生成算法
conf - 数据库配置信息
common - 公共库config - viper配置dto - 请求响应/返回值结构体merrors - 错误码、错误信息定义mysql - 数据库连接池xgrpc - grpc server的proto定义使用说明
项目采用go1.20编写采用go mod进行包管理编译运行 go build ./go-tinyid项目提供http和grpc两种访问方式可自行选择
核心流程说明
1. 定义id生成器结构体 type IdSequence struct {idListLength int64 // 号段长度可根据业务qps自行设置biz string // 业务类型ids chan int64 // 生成的id list, chan通道stopMonitor chan bool // 停止标志channel类型}2. id生成器共有MonitorGetOne, Close三个对外暴露的方法。 Monitor方法主要实现对id list的监控当检测到id list为空时会调用add方法向id list中添加idListLength个新id在添加新id过程中
会使用mysql 乐观锁以防止其他进程也在更新获取到的最新id;GetOne方法主要会从id list里面获取一个新的id;Close方法主要是关闭channel停止写入新的id;3. 数据表结构
create table if not exists test.sequence
(id bigint unsigned auto_increment primary key,biz varchar(128) default not null comment 业务类型,value bigint default 0 not null comment id值,version bigint default 0 not null comment 乐观锁,is_del tinyint default 0 not null comment 是否软删标志,create_time timestamp default CURRENT_TIMESTAMP not null comment 创建时间,update_time timestamp default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP,unique (version)
) charset utf8mb4;参与贡献
项目地址https://gitee.com/git-lz/go-tinyidhttps://github.com/007LiZhen/go-tinyid
欢迎大家积极提issue和MR, 共建golang版本的tinyid
Fork 本仓库新建 Feat_xxx 分支提交代码新建 Pull Request