分布式id生成数据库号段算法的golang实现

分布式id生成数据库号段算法的golang实现

    • 介绍
    • 项目结构
    • 使用说明
    • 核心流程说明
      • [1. 定义id生成器结构体](#1. 定义id生成器结构体)
      • [2. id生成器共有Monitor,GetOne, Close三个对外暴露的方法。](#2. id生成器共有Monitor,GetOne, Close三个对外暴露的方法。)
      • [3. 数据表结构](#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定义

使用说明

  1. 项目采用go1.20编写,采用go mod进行包管理
  2. 编译运行 go build && ./go-tinyid
  3. 项目提供http和grpc两种访问方式,可自行选择

核心流程说明

1. 定义id生成器结构体

复制代码
   type IdSequence struct {
      idListLength int64           // 号段长度,可根据业务qps自行设置
      biz          string          // 业务类型
      ids          chan int64      // 生成的id list, chan通道
      stopMonitor  chan bool       // 停止标志channel类型
   }

2. id生成器共有Monitor,GetOne, 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-tinyid;https://github.com/007LiZhen/go-tinyid

欢迎大家积极提issue和MR, 共建golang版本的tinyid

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
相关推荐
Skrrapper3 分钟前
【编程史】微软的起家之路:一代传奇的诞生
数据库·c++·microsoft
q***31148 分钟前
【JAVA进阶篇教学】第十二篇:Java中ReentrantReadWriteLock锁讲解
java·数据库·python
y***n61411 分钟前
Redis设置密码
数据库·redis·缓存
浮尘笔记14 分钟前
Go语言中如何实现线程安全的map
开发语言·后端·安全·golang
小坏讲微服务37 分钟前
Spring Boot 4.0 与 Spring Cloud Alibaba 2025 整合完整指南
java·spring boot·分布式·后端·spring cloud·微服务·架构
瀚高PG实验室40 分钟前
HGDB兼容性之oracle的rowid
数据库·oracle·瀚高数据库
晨晖21 小时前
MyBatisPlus的条件构造器
java·数据库·windows
有味道的男人1 小时前
速卖通商品详情接口(速卖通API系列)
java·大数据·数据库
蟹至之1 小时前
【MySQL】用户和权限管理
数据库·mysql·权限
沐夜听风1 小时前
MySQL的主从---1
数据库·mysql·主从原理