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失败");
}
}