{"id":16178949,"url":"https://github.com/zhongxunking/ids","last_synced_at":"2025-03-19T01:30:52.603Z","repository":{"id":135438131,"uuid":"118220796","full_name":"zhongxunking/ids","owner":"zhongxunking","description":"高效的分布式id生成器，每个客户端实例tps可达到100万，服务端毫无压力。即使服务端宕机了，id生成依然可用。支持多数据中心，支持id加密。","archived":false,"fork":false,"pushed_at":"2019-02-26T16:39:08.000Z","size":53,"stargazers_count":47,"open_issues_count":0,"forks_count":23,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-17T01:35:14.818Z","etag":null,"topics":["distributed-id","id","id-generator","idcenter","ids"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zhongxunking.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-20T07:41:29.000Z","updated_at":"2022-04-08T01:08:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"82b0f441-1cf6-4d83-a14a-6d6c2dbc6228","html_url":"https://github.com/zhongxunking/ids","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhongxunking%2Fids","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhongxunking%2Fids/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhongxunking%2Fids/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhongxunking%2Fids/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhongxunking","download_url":"https://codeload.github.com/zhongxunking/ids/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244336129,"owners_count":20436773,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["distributed-id","id","id-generator","idcenter","ids"],"created_at":"2024-10-10T05:24:57.893Z","updated_at":"2025-03-19T01:30:52.390Z","avatar_url":"https://github.com/zhongxunking.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 分布式id生成器-ids\n\n1. 简介\n\u003e 生成全局唯一的id（流水号），是很多公司都需要解决的问题。如果还是采用时间戳+随机数形式生成，在并发量大时，很有可能会生成重复的id。重复id的危害就是会导致一系列问题，比如幂等性。\n\n\u003e ids专门用来高效的生成全局唯一id，支持多数据中心，支持对id进行加密，每个应用实例的tps可达到100万，而且服务端毫无压力。即使服务端和zookeeper都宕机了，id生成依然可用（ids弱依赖zookeeper）。\n\n2. 环境要求：\n\u003e * jdk1.8\n\u003e * zookeeper\n\n\n\u003e 注意：本项目已经上传到[maven中央库](http://search.maven.org/#search%7Cga%7C1%7Corg.antframework.ids)\n\n3. 技术交流和支持\n\u003e 欢迎加我微信（zhong_xun_），进行技术交流和支持。如果本项目对你有帮助，欢迎Star和Fork。\n\n## 1. 整体设计\nids是结合开源项目[“idcenter”](https://github.com/zhongxunking/idcenter)和[“本地id生成”](https://github.com/zhongxunking/ant-common-util#7-%E6%9C%AC%E5%9C%B0id%E7%94%9F%E6%88%90%E5%99%A8)，进行组合出的适用于绝大多数公司的分布式id生成器。如果ids生成的id不满足你的需求，你完全可以自己根据“idcenter”和“本地id生成”组装出适合自己的分布式id生成器。\n\n### 1.1 整体设计图\n\u003cimg src=\"https://note.youdao.com/yws/api/personal/file/WEB54a4eae4524569272aadcc611a4355f2?method=download\u0026shareKey=0776adfb2e6be8406898b74e19c58ffa\" width=700 /\u003e\n\n### 1.2 id生成过程\n分为以下两种情况：\n1. idcenter可用时：ids会优先使用从idcenter获取到的id，ids平均每5分钟请求一次idcenter（对服务端压力非常小），并且会预存足够使用10至15分钟的id存量。\n2. idcenter不可用时：idcenter不可用的接下来10到15分钟使用的依然是从idcenter获取到的id（因为有id存量）。只有当从idcenter获取到的id使用完时，ids才会使用“本地id生成”进行生成的id。ids会先从zookeeper获取workerId（只会获取一次，对zookeeper压力非常小），并把workerId保存到缓存文件，当以后zookeeper不可用时，会直接从缓存文件读取workerId（弱依赖zookeeper）。\n\n### 1.3 id结构\n整体结构：\n- id长度：20 + 数据中心编码的长度。\n- 生成的id样例：20180908120000000001（无多数据中心情况）、2018090812010000000001（数据中心编码为：01）\n\nid结构详解（无多数据中心情况）:\n1. 从idcenter获取的id：id前10位为生成id时间的yyyyMMddHH，后10位为0到100亿趋势递增的数字。样例：20180908120000000123\n2. 从“本地id生成”获取的id：id前8位为生成id时间的yyyyMMdd，中间5位为应用的workerId+25000，最后7位为0到1000万趋势递增的数字。样例：20180908250010000123\n\nid结构详解（多数据中心情况）:\u003cbr/\u003e\n根据上面无多数据中心情况生成的id，然后在id中间插入数据中心的编码。比如数据中心编码为01生成的id样例：2018090812010000000123（从idcenter获取）、2018090825010010000123（从“本地id生成”获取）\n\n### 1.4 限制\n1. idcenter生成的id：每小时可生成的id数量上限为100亿个（对于绝大多数公司完全足够），如果超过100亿，则会预先透支下一个小时的id。\n2. “本地id生成”的id：每个应用节点每天可生成的id数量上限为1000万个，如果超过1000万，则会预先透支下一天的id。\n3. 应用节点的数量上限是37500个（因为workerId的长度限制为5位，再减去25000，还有为了效率考虑做了一些措施）。如果你的应用实例超过了37500，则可以考虑使用多数据中心，或者参考ids自己实现一个id位数更长的分布式id生成器（比如25或30位）。\n\n### 1.5 其他\n1. ids有安全控制，即使系统突然宕机、时钟被回拨，都不会出现问题，生成的id永远都是唯一的。\n2. 总的来说生成的id基本上是从idcenter获取的，“本地id生成”只是一种保底措施，保证idcenter不可用时，依然可以正常生成id。\n3. 生成的id的时间前缀有5分钟误差，比如20180908170000000001这个id真正的生成时间是2018-09-08 17:00至\n2018-09-08 18:05这期间。\n\n## 2. 部署\n### 2.1 部署idcenter\n1. 按照[“idcenter”](https://github.com/zhongxunking/idcenter#2-%E6%9C%8D%E5%8A%A1%E7%AB%AF%E9%83%A8%E7%BD%B2)的文档部署服务端。\n2. 通过idcenter的[后台管理页面](https://github.com/zhongxunking/idcenter#4-id%E7%AE%A1%E7%90%86%E4%BB%8B%E7%BB%8D)添加一个id提供者：\n```\nid编码：uid\n名称：全局唯一id\n周期类型：每小时\nid最大值：10000000000（100亿）\n单次最大数量：1000000（100万）\n```\n\u003cimg src=\"https://note.youdao.com/yws/api/personal/file/WEB9ef67268db4219b2ef75b89b6a48b1e7?method=download\u0026shareKey=eccf4d8860b9db9d2baa48428da81e1c\" width=700 /\u003e\n\u003cimg src=\"https://note.youdao.com/yws/api/personal/file/WEB58ebd47274c8ab99e15529bcf305bc54?method=download\u0026shareKey=0017bcf71e43be97b225984daba21d24\" width=700 /\u003e\n\n### 2.2 部署zookeeper\n1. 如果你所在的公司有现有的zookeeper，则直接使用现有的zookeeper就行，ids只会操作“/ids”路径下的节点。\n2. 如果你所在的公司没有部署zookeeper，则需要部署zookeeper。对于怎么部署zookeeper，网上有很多介绍，在此就不论述了。\n\n## 3. 使用ids\n### 3.1 引入ids依赖：\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.antframework.ids\u003c/groupId\u003e\n    \u003cartifactId\u003eids\u003c/artifactId\u003e\n    \u003cversion\u003e1.2.2.RELEASE\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### 3.2 系统启动阶段设置ids的参数：\n```java\n// 必填：idcenter服务端地址\nSystem.setProperty(IdsParams.IDCENTER_URL_KEY, \"http://localhost:6210\");\n// 必填：缓存文件夹路径\nSystem.setProperty(IdsParams.HOME_PATH_KEY, \"/var/apps/ids\");\n// 必填：应用实例的编码（每个实例必须唯一），比如可以使用ip+端口\nSystem.setProperty(IdsParams.WORKER_KEY, \"192.168.0.1:8080\");\n// 必填：zookeeper地址，存在多个则以“,”分隔（比如：192.168.0.1:2181,192.168.0.2:2181）\nSystem.setProperty(IdsParams.ZK_URLS_KEY, \"localhost:2181\");\n\n// 选填：数据中心编码（如果不存在多数据中心这种情况，则不用填）\nSystem.setProperty(IdsParams.IDC_ID_KEY, \"01\");\n// 选填：加密种子（如果不需要对id进行加密，则不用填）\nSystem.setProperty(IdsParams.ENCRYPTION_SEED_KEY, \"123\");\n```\n\n### 3.3 获取id\n```java\n// 使用UID获取全局唯一id\n// 不使用加密情况：\nString id1 = UID.newId();  // 2018090812010000000001\nString id2 = UID.newId();  // 2018090812010000000002\n\n// 使用加密情况：\nString id1 = UID.newId();  // 2018090812015960159601\nString id2 = UID.newId();  // 2018090812018742387423\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhongxunking%2Fids","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhongxunking%2Fids","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhongxunking%2Fids/lists"}