文章目录
- 一、号段模式
- 1. 拉取项目源码编译
- 2. springboot集成Leaf
- 3. 配置leaf.properties
- 4. 创建数据库
- 5. 初始化表结构和数据
- 6. 测试案例
- 7. 浏览器测试
美团Leaf的号段模式和雪花算法模式生成分布式全局唯一id方式2种
一、号段模式
目前jar在maven仓库中没有上传
1. 拉取项目源码编译
git clone git@github.com:Meituan-Dianping/Leaf.git
cd Leaf/
git checkout feature/spring-boot-starter
mvn clean install -Dmaven.test.skip=true
2. springboot集成Leaf
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.18.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.gblfy</groupId><artifactId>distributed</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!--SpringMVC启动器--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><artifactId>leaf-boot-starter</artifactId><groupId>com.sankuai.inf.leaf</groupId><version>1.0.1-RELEASE</version></dependency><!--zk--><dependency><groupId>org.apache.curator</groupId><artifactId>curator-recipes</artifactId><version>2.6.0</version><exclusions><exclusion><artifactId>log4j</artifactId><groupId>log4j</groupId></exclusion></exclusions></dependency></dependencies>
</project>
3. 配置leaf.properties
leaf.name=com.sankuai.leaf.opensource.test
#是否开启号段模式
leaf.segment.enable=true
#数据库连接url
leaf.segment.url=jdbc:mysql://localhost:3306/dca?useSSL=false&serverTimezone=GMT%2B8
#用户名
leaf.segment.username=root
#密码
leaf.segment.password=root#是否开启雪花算法
leaf.snowflake.enable=true
#zk服务端ip
leaf.snowflake.address=192.168.0.113
#zk服务端端口号
leaf.snowflake.port=2181注:服务端防火墙要开启2181端口权限
4. 创建数据库
CREATE DATABASE leaf
5. 初始化表结构和数据
use leaf;
CREATE TABLE `leaf_alloc` (`biz_tag` varchar(128) NOT NULL DEFAULT '', -- your biz unique name`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')
6. 测试案例
package com.gblfy.distributedid;import com.sankuai.inf.leaf.service.SegmentService;
import com.sankuai.inf.leaf.service.SnowflakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** 参考博客:* 美团Leaf源码——snowflake模式源码解析* https://blog.csdn.net/bskfnvjtlyzmv867/article/details/90247036*/
@RestController
public class LeafController {@Autowiredprivate SegmentService segmentService;@Autowiredprivate SnowflakeService snowflakeService;/*** 号段模式** @return*/@RequestMapping(value = "/api/segment/get/")public String getSegmentId() {//这个key需要和数据库的字段biz_tag保持一致 名称和好短可以自定义return String.valueOf(segmentService.getId("leaf-segment-test").getId());}/*** 雪花算法模式** @return*/@RequestMapping(value = "/api/snowflake/get/{key}")public String getSnowflakeId(@PathVariable String key) {//这里的key无实际意义,但是必须要传可以写死return String.valueOf(snowflakeService.getId(key).getId());}
}
7. 浏览器测试
号段模式
http://localhost/api/segment/get/
雪花算法模式
http://localhost/api/snowflake/get/key