分布式6大核心专题_分布式ID
生活随笔
收集整理的這篇文章主要介紹了
分布式6大核心专题_分布式ID
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、號段模式
- 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=true2. 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 leaf5. 初始化表結構和數據
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
總結
以上是生活随笔為你收集整理的分布式6大核心专题_分布式ID的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TortoiseGit 克隆_入门试炼_
- 下一篇: 一、项目管理框架【PMP 】