Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

Leaf

> 编程语言
Open source

Distributed ID Generate Service

6.8K stars0 likes4 views
WebsiteGitHub

About

Distributed ID Generate Service

Leaf

There are no two identical leaves in the world.

​ — Leibnitz

中文文档 | English Document

Introduction

Leaf refers to some common ID generation schemes in the industry, including redis, UUID, snowflake, etc. Each of the above approaches has its own problems, so we decided to implement a set of distributed ID generation services to meet the requirements. At present, Leaf covers Meituan review company's internal finance, catering, takeaway, hotel travel, cat's eye movie and many other business lines. On the basis of 4C8G VM, through the company RPC method, QPS pressure test results are nearly 5w/s, TP999 1ms.

You can use it to encapsulate a distributed unique id distribution center in a service-oriented SOA architecture as the id distribution provider for all applications

Quick Start

Leaf Server

Leaf provides an HTTP service based on spring boot to get the id

run Leaf Server

build
bash
git clone [email protected]:Meituan-Dianping/Leaf.git
cd leaf
mvn clean install -DskipTests
cd leaf-server
run
maven
bash
mvn spring-boot:run

or

shell command
bash
sh deploy/run.sh
test
bash
#segment
curl http://localhost:8080/api/segment/get/leaf-segment-test
#snowflake
curl http://localhost:8080/api/snowflake/get/test

Configuration

Leaf provides two ways to generate ids (segment mode and snowflake mode), which you can turn on at the same time or specify one way to turn on (both are off by default).

Leaf Server configuration is in the leaf-server/src/main/resources/leaf.properties

configuration meaning default
leaf.name leaf service name
leaf.segment.enable whether segment mode is enabled false
leaf.jdbc.url mysql url
leaf.jdbc.username mysql username
leaf.jdbc.password mysql password
leaf.snowflake.enable whether snowflake mode is enabled false
leaf.snowflake.zk.address zk address under snowflake mode
leaf.snowflake.port service registration port under snowflake mode

Segment mode

In order to use segment mode, you need to create DB table first, and configure leaf.jdbc.url, leaf.jdbc.username, leaf.jdbc.password

If you do not want use it, just configure leaf.segment.enable=false to disable it.

sql
CREATE DATABASE leaf
CREATE TABLE `leaf_alloc` (
  `biz_tag` varchar(128)  NOT NULL DEFAULT '', -- your biz unique name
  `max_id` bigint(20) NOT NULL DEFAULT '1',
  `step` int(11) NOT NULL,
  `description` varchar(256)  DEFAULT NULL,
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`biz_tag`)
) ENGINE=InnoDB;

insert into leaf_alloc(biz_tag, max_id, step, description) values('leaf-segment-test', 1, 2000, 'Test leaf Segment Mode Get Id')

Snowflake mode

The algorithm is taken from twitter's open-source snowflake algorithm.

If you do not want to use it, just configure leaf.snowflake.enable=false to disable it.

Configure the zookeeper address

leaf.snowflake.zk.address=${address}
leaf.snowflake.enable=true
leaf.snowflake.port=${port}

configure leaf.snowflake.zk.address in the leaf.properties, and configure the leaf service listen port leaf.snowflake.port.

monitor page

segment mode: http://localhost:8080/cache

Leaf Core

Of course, in order to pursue higher performance, you need to deploy the Leaf service through RPC Server, which only needs to introduce the leaf-core package and encapsulate the API that generates the ID into the specified RPC framework.

Attention

Note that leaf's current IP acquisition logic in the case of snowflake mode takes the first network card IP directly (especially for services that change IP) to avoid wasting the workId

Issues· 95 open

View all issuesOpen on GitHub
  • #200

    最小步长设置问题

    Updated Dec 9, 2025
  • #211

    号段模式,max_id的增长不是根据步长来的吧

    Updated Jul 31, 2025
  • #206

    leaf_alloc的biz_tag为主键,频繁页分裂问题

    Updated Jul 9, 2025
  • #221

    [BUG] 不支持Mysql8.0及以上版本

    Updated Mar 22, 2025
  • #220

    我改了一版,需要的可以直接安装。主要改动是加了id长度限制和按日期循环生成id

    Updated Mar 15, 2025
  • #217

    按文档配置启动出现报错

    Updated Feb 25, 2025
  • #177

    是否能将号段模式和snowflake模式相结合以提高可用性,增强服务性能?

    Updated Jan 15, 2025
  • #216

    leaf snowflake真的解决了时钟回拨引起的重复id问题了吗

    Updated Nov 18, 2024
  • #214

    Field properties in com.tencent.devops.leaf.plugin.LeafSpringBootStarterAutoConfigure required a single bean, but 2 were found

    Updated Oct 28, 2024
  • #215

    预加载第二个segment的时候,是不是可以不用加写锁

    Updated May 14, 2024

> Tags

Javadistributed-id-generatorleaf

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 23, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言