提前准备

下载源码

GitHub地址:https://github.com/Meituan-Dianping/Leaf

下载下来 然后 maven install 安装到本地仓库

再需要用到该ID算法的项目中引入 以下内容

<!--  本地仓库中的Leaf      -->
<dependency>
    <artifactId>leaf-boot-starter</artifactId>
    <groupId>com.sankuai.inf.leaf</groupId>
    <version>1.0.1-RELEASE</version>
    <!--  这个如无冲突 也无需处理      -->
    <exclusions>
        <exclusion>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </exclusion>
    </exclusions>
</dependency>


<!--  以下是在于使用snowflake方案是报错 最后引入这俩解决的 如无问题 可以不做处理     -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>2.6.0</version> <!-- 确保版本匹配你的 Spring Boot 版本 -->
</dependency>
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>2.6.0</version>
</dependency>

安装部署zookeeper (segment 无需安装)

下载地址:https://zookeeper.apache.org/releases.html

美团-Leaf ID算法集成到SpringBoot项目_ID

解压 copy conf中的配置文件

美团-Leaf ID算法集成到SpringBoot项目_spring_02


修改zoo.cfg 自定义数据地址

美团-Leaf ID算法集成到SpringBoot项目_唯一_03

启动zookeeper

sh zkServer.sh start

美团-Leaf ID算法集成到SpringBoot项目_Leaf_04

完善leaf.properties文件

leaf.name=com.sankuai.leaf.opensource.test
leaf.segment.enable=false
leaf.segment.url=
leaf.segment.username=
leaf.segment.password=

leaf.snowflake.enable=false
leaf.snowflake.address=
leaf.snowflake.port=

美团-Leaf ID算法集成到SpringBoot项目_唯一_05

@EnableLeafServer

Leaf-segment数据库方案

这种方案依赖数据库表
执行以下sql

CREATE DATABASE leaf
CREATE TABLE `leaf_alloc` (
  `biz_tag` varchar(128)  NOT NULL DEFAULT '',
  `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')
import com.pointlion.Application;
import com.sankuai.inf.leaf.common.Result;
import com.sankuai.inf.leaf.service.SegmentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class IdTest {
   
    @Autowired
    private SegmentService segmentService;
    
    @Test
    public void testSegment() {
        Result id = segmentService.getId("order");
        System.out.println("------------------------------");
        System.out.println(id.getId());
        System.out.println("------------------------------");
    }
}

Leaf-snowflake方案

import com.pointlion.Application;
import com.sankuai.inf.leaf.common.Result;
import com.sankuai.inf.leaf.service.SnowflakeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class IdTest {

    @Autowired
    private SnowflakeService snowflakeService;

    @Test
    public void testSnowflake() {
        Result id = snowflakeService.getId("order");
        System.out.println("------------------------------");
        System.out.println(id.getId());
        System.out.println("------------------------------");

    }
}

具体的算法实现讲解

Leaf——美团点评分布式ID生成系统:https://tech.meituan.com/2017/04/21/mt-leaf.html