美团-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

相关推荐
27669582929 天前
H5 最新mtgsig
美团·mtgsig·mtgsig1.2·美团h5·mtgsig逆向·mtgsig4.2·mtgsig最新逆向
大猫和小黄22 天前
Java开发过程中的各种ID生成策略
java·开发语言·id
大猫和小黄22 天前
Java ID生成策略全面解析:从单机到分布式的最佳实践
java·开发语言·分布式·id
lkbhua莱克瓦241 个月前
CPU三大核心部件功能详解
计算机·cpu·oc·ir·id
lkbhua莱克瓦241 个月前
手机选购关注的参数-CPU篇
计算机·智能手机·cpu·oc·ir·id
27669582921 个月前
美团websocket 分析
websocket·网络协议·npm·美团·mtgsig·美团websoket·美团商家端
居7然5 个月前
美团大模型“龙猫”登场,能否重塑本地生活新战局?
人工智能·大模型·生活·美团
没事学AI6 个月前
美团搜索推荐统一Agent之交互协议与多Agent协同
人工智能·agent·美团·多agent
卷心菜不卷Iris6 个月前
第4章唯一ID生成器——4.5 美团点评开源方案Leaf
雪花算法·美团·分布式系统·leaf·分布式唯一id·点评
励碼8 个月前
美团完整面经
java·面试·职场和发展·sass·美团