开篇
之前调试了美团的leaf,感觉不错,果然是大厂开源出来的ID生成器,整个架构和ID生成规则都经过考量,完全胜任大厂的ID生成规则。那么今天就来调试一下滴滴的开源的tinyid。我这里只调试用法,性能方面如果感兴趣还是自己去测试吧,官方是这样说的:http方式访问,性能取决于http server的能力,网络传输速度。java-client方式,id为本地生成,号段长度(step)越长,qps越大,如果将号段设置足够大,则qps可达1000w+。如果觉得有用,转发收藏吧。另外别光当"码住",给个关注哦。
当然之前调试过美团的分布式ID生成器,刚兴趣的可以去看看:
美团开源的QPS压测结果近5w/s的分布式ID生成器leaf的调试实战
核心调试
1、一如既往的,从开源地址导入滴滴的分布式ID生成器,开源地址是:
https://github.com/didi/tinyid
2、配置数据库相关,当然,滴滴开源的数据库的脚本,比美团的多了一个表。
以下是数据库脚本如下,建议直接拷贝出来:
CREATE TABLE `tiny_id_info` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增主键', `biz_type` varchar(63) NOT NULL DEFAULT '' COMMENT '业务类型,唯一', `begin_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '开始id,仅记录初始值,无其他含义。初始化时begin_id和max_id应相同', `max_id` bigint(20) NOT NULL DEFAULT '0' COMMENT '当前最大id', `step` int(11) DEFAULT '0' COMMENT '步长', `delta` int(11) NOT NULL DEFAULT '1' COMMENT '每次id增量', `remainder` int(11) NOT NULL DEFAULT '0' COMMENT '余数', `create_time` timestamp NOT NULL DEFAULT '2010-01-01 00:00:00' COMMENT '创建时间', `update_time` timestamp NOT NULL DEFAULT '2010-01-01 00:00:00' COMMENT '更新时间', `version` bigint(20) NOT NULL DEFAULT '0' COMMENT '版本号', PRIMARY KEY (`id`), UNIQUE KEY `uniq_biz_type` (`biz_type`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT 'id信息表'; CREATE TABLE `tiny_id_token` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增id', `token` varchar(255) NOT NULL DEFAULT '' COMMENT 'token', `biz_type` varchar(63) NOT NULL DEFAULT '' COMMENT '此token可访问的业务类型标识', `remark` varchar(255) NOT NULL DEFAULT '' COMMENT '备注', `create_time` timestamp NOT NULL DEFAULT '2010-01-01 00:00:00' COMMENT '创建时间', `update_time` timestamp NOT NULL DEFAULT '2010-01-01 00:00:00' COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT 'token信息表'; INSERT INTO `tiny_id_info` (`id`, `biz_type`, `begin_id`, `max_id`, `step`, `delta`, `remainder`, `create_time`, `update_time`, `version`) VALUES (1, 'test', 1, 1, 100000, 1, 0, '2018-07-21 23:52:58', '2018-07-22 23:19:27', 1); INSERT INTO `tiny_id_info` (`id`, `biz_type`, `begin_id`, `max_id`, `step`, `delta`, `remainder`, `create_time`, `update_time`, `version`) VALUES (2, 'test_odd', 1, 1, 100000, 2, 1, '2018-07-21 23:52:58', '2018-07-23 00:39:24', 3); INSERT INTO `tiny_id_token` (`id`, `token`, `biz_type`, `remark`, `create_time`, `update_time`) VALUES (1, '0f673adf80504e2eaa552f5d791b644c', 'test', '1', '2017-12-14 16:36:46', '2017-12-14 16:36:48'); INSERT INTO `tiny_id_token` (`id`, `token`, `biz_type`, `remark`, `create_time`, `update_time`) VALUES (2, '0f673adf80504e2eaa552f5d791b644c', 'test_odd', '1', '2017-12-14 16:36:46', '2017-12-14 16:36:48');
3、修改相关配置,如下图:
4、启动吧。
5、开始生成ID试试。请求路径是:http://localhost:9999/tinyid/id/nextId?bizType=test&token=0f673adf80504e2eaa552f5d791b644c
当然,不止这些,有以下几种方式。建议自己去试试,这都是RestfulAPI的。
nextId: curl 'http://localhost:9999/tinyid/id/nextId?bizType=test&token=0f673adf80504e2eaa552f5d791b644c' response:{"data":[2],"code":200,"message":""} nextId Simple: curl 'http://localhost:9999/tinyid/id/nextIdSimple?bizType=test&token=0f673adf80504e2eaa552f5d791b644c' response: 3 with batchSize: curl 'http://localhost:9999/tinyid/id/nextIdSimple?bizType=test&token=0f673adf80504e2eaa552f5d791b644c&batchSize=10' response: 4,5,6,7,8,9,10,11,12,13 Get nextId like 1,3,5,7,9... bizType=test_odd : delta is 2 and remainder is 1 curl 'http://localhost:9999/tinyid/id/nextIdSimple?bizType=test_odd&batchSize=10&token=0f673adf80504e2eaa552f5d791b644c' response: 3,5,7,9,11,13,15,17,19,21
结语:
这个tinyid是滴滴开源的分布式ID生成器,更多用法,大家自己去探索吧。滴滴开源的网址上有详细介绍,这里只是说说调试过程和用法,觉得有用就点个赞转个发吧。
另外我还调试了很多开源项目,感兴趣的可以去看看
调试个开源Java 轻量级高性能IM,单机支持几十万至百万在线用户
Java 开源 的spring cloud 微服务 化 开发平台 调试实战
基于 Java Spring cloud的开源在线教育系统调试实战