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

提前准备

下载源码

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

下载下来 然后 maven install 安装到本地仓库
再需要用到该ID算法的项目中引入 以下内容

xml 复制代码
<!--  本地仓库中的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

解压 copy conf中的配置文件

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

启动zookeeper

bash 复制代码
sh zkServer.sh start

完善leaf.properties文件

java 复制代码
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=

@EnableLeafServer

Leaf-segment数据库方案

这种方案依赖数据库表

执行以下sql

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')
java 复制代码
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方案

java 复制代码
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

相关推荐
没事学AI4 天前
美团搜索推荐统一Agent之交互协议与多Agent协同
人工智能·agent·美团·多agent
卷心菜不卷Iris18 天前
第4章唯一ID生成器——4.5 美团点评开源方案Leaf
雪花算法·美团·分布式系统·leaf·分布式唯一id·点评
励碼2 个月前
美团完整面经
java·面试·职场和发展·sass·美团
27669582924 个月前
美团优选小程序 mtgsig 分析 mtgsig1.2
java·python·小程序·美团·mtgsig·mtgsig1.2·美团优选
27669582924 个月前
美团民宿 mtgsig 小程序 mtgsig1.2 分析
java·python·小程序·美团·mtgsig·mtgsig1.2·美团民宿
Cloud_.5 个月前
美团Leaf分布式ID生成器:雪花算法原理与应用
雪花算法·分布式id·leaf·美团分布式id算法·美团leaf
shyの同学5 个月前
分布式ID生成方案:数据库号段、Redis与第三方开源实现
redis·分布式·mysql·id
萌萌的提莫队长9 个月前
PICO 获取设备号 SN码
vr·pico·sn·id
276695829210 个月前
大众点评 web mtgsig 1.2分析
java·python·node·美团·大众点评·mtgsig