美团leaf生成分布式唯一id

1. 介绍
https://github.com/Meituan-Dianping/Leaf.git 源码  改为下载Leaf-feature-spring-boot-starter.zip包
本地安装后
<dependency>
	<artifactId>leaf-boot-starter</artifactId>
    <groupId>com.sankuai.inf.leaf</groupId>
    <version>1.0.1-RELEASE</version>
</dependency>

也可以使用
https://gitee.com/whisperofjune/wssnail-parent   封装美团的Leaf框架为starter,自动注入,不需要@EnableLeafServer注解了
优化改造 https://gitee.com/bestman_456/wssnail-parent.git,支持 jdk1.8
git  clone https://gitee.com/bestman_456/wssnail-parent.git
mvn clean install
添加依赖
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.wssnail</groupId>
                <artifactId>wssnail-dependencies</artifactId>
                <version>1.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

 <dependency>
            <groupId>com.wssnail</groupId>
            <artifactId>leaf-spring-boot-starter-server</artifactId>
        </dependency>

2. 使用

存在多数据源的问题,Leaf内部使用的阿里巴巴的Druid数据连接池,测试使用的是Hikari连接池,请注意:如果项目中也使用阿里的Druid连接池的话,可能会有多数据源的问题。
数据源配置

wssnail:
 mt:
  leaf:
    segment-enable: true
    jdbc-username: root
    jdbc-password: root
    jdbc-url: jdbc:mysql://127.0.0.1:3306/leaf?useUnicode=true&&useSSL=false&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
    snowflake-enable: false
    snowflake-zk-address: 192.168.139.184
    snowflake-port: 2181

创建表

DROP TABLE IF EXISTS `leaf_alloc`;
CREATE TABLE `leaf_alloc`  (
  `biz_tag` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '' COMMENT '业务标识',
  `max_id` bigint NOT NULL DEFAULT 1 COMMENT '最大ID',
  `step` int NOT NULL COMMENT '步调',
  `description` varchar(256) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '描述',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '时间',
  PRIMARY KEY (`biz_tag`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic Comment='美团leaf算法ID分配表';

#插入分段测试数据,标识符:biz_tag不能相同
INSERT INTO leaf_alloc(biz_tag, max_id, step, description) VALUES('leaf-segment-test', 1, 20, 'Test leaf Segment Mode Get Id');
 
#插入分段测试数据,标识符:biz_tag不能相同
INSERT INTO leaf_alloc(biz_tag, max_id, step, description) VALUES('my-snowflake-segment', 1, 20, 'Test leaf Snowflake Mode Get Id');
3. 测试

环境问题,Leaf-segment号段模式 能正常测试, Leaf-snowflake 需要借助zookeepr,没有固定返回0

package com.yl.leaf.controller;

import com.wssnail.leaf.core.common.Result;
import com.wssnail.leaf.core.common.Status;
import com.wssnail.leaf.server.service.SegmentService;
import com.wssnail.leaf.server.service.SnowflakeService;
import com.yl.leaf.annotation.RepeatSubmit;
import com.yl.leaf.base.RetModel;
import com.yl.leaf.base.RetResult;
import com.yl.leaf.model.UserQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试美团Leaf算法生成ID
 *
 * @author liuxubo
 * @date 2022/10/9 22:46
 */
@Slf4j
@RestController
@RequestMapping("/leaf")
public class TestController {
    @Autowired
    private SegmentService segmentService;
    @Autowired
    private SnowflakeService snowflakeService;


    /**
     * 测试号段生成id
     *
     * @return
     */
    @RepeatSubmit
    @PostMapping("/segment")
    public RetResult getSegmentId(@RequestBody UserQuery userQuery) {
        log.info(userQuery.toString());
        // 业务标识biz_tag 列
        Result r = segmentService.getId("leaf-segment-test");

        //判断是否成功,成功返回具体的id,不成功返回错误提示
        if (r.getStatus() == Status.SUCCESS) {
            return RetModel.ok().setData(r.getId());
        }

        return RetModel.fail("获取segment号段ID失败");
    }


    /**
     * 测试snowflake雪花算法生成id
     *
     * @return
     */
    @GetMapping("/snowflake")
    public RetResult getSnowflakeId() {
        //获取snowflake分布式ID
        //业务标识biz_tag 列
        Result r = snowflakeService.getId("my-snowflake-segment");

        //判断是否成功,成功返回具体的id,不成功返回错误提示
        if (r.getStatus() == Status.SUCCESS) {
            return RetModel.ok().setData(r.getId());
        }

        return RetModel.fail("获取snowflake分布式ID失败");
    }

}

根据提供的引用内容,我无法找到关于MyBatis Plus使用美团Leaf的具体信息。但是,我可以为您提供一些关于MyBatis Plus和Leaf的基本介绍。 MyBatis Plus是一个基于MyBatis的增强工具,它简化了MyBatis的开发流程,提供了更多的便利功能,如自动生成代码、分页查询、逻辑删除等。它可以与各种数据库进行集成,并提供了丰富的API和注解,使得开发更加高效和便捷。 美团Leaf是一个分布式ID生成系统,它可以生成全局唯一IDLeaf提供了两种ID生成算法:基于数据库的分段生成ID和雪花算法生成分布式ID。这些算法可以确保生成ID分布式环境下是唯一的。 如果您想在MyBatis Plus中使用美团Leaf,您可以按照以下步骤进行操作: 1. 首先,您需要将Leaf集成到您的项目中。您可以通过引用美团Leaf的Git仓库或者下载源代码来获取Leaf。 2. 然后,您需要根据Leaf的文档和示例代码,配置Leaf的相关参数,如数据库连接信息、ID生成算法等。 3. 接下来,您可以在MyBatis Plus的Mapper接口中定义一个方法,用于生成ID。您可以使用Leaf提供的API来生成ID,并将其作为参数传递给您的SQL语句。 4. 最后,您可以在您的业务逻辑中调用该方法,以生成唯一ID。 请注意,以上步骤仅为一种可能的集成方式,具体的实现方式可能因项目需求和配置而有所不同。建议您参考美团Leaf的官方文档和示例代码,以获得更详细的集成指南和使用方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值