Spring Boot專案整合UidGenerato的方法步驟
阿新 • • 發佈:2020-12-13
前言
UidGenerato 基於snowflake演算法實現
UidGenerato 由百度開發,基於SnowFlake演算法的唯一ID生成器。UidGenerato 已元件的形式工作在應用專案中,支援自定義workeid位數和初始化策略,從而適用docker等虛擬化環境下例項自動重啟等場景。
準備一個maven專案,構建兩個模組。分別作為使用方和提供方。(建兩個模組主要是為了“造輪子”,其他模組或專案可以直接引用,無需關心uid配置,如果沒有分模組,可以指忽略構建兩個模組)
下載uid原始碼,放在專案中,開源地址 https://github.com/baidu/uid-generator
資料庫建表
DROP TABLE IF EXISTS WORKER_NODE;CREATE TABLE WORKER_NODE ( ID BIGINT NOT NULL AUTO_INCREMENT COMMENT 'auto increment id',HOST_NAME VARCHAR(64) NOT NULL COMMENT 'host name',PORT VARCHAR(64) NOT NULL COMMENT 'port',TYPE INT NOT NULL COMMENT 'node type: ACTUAL or CONTAINER',LAUNCH_DATE DATE NOT NULL COMMENT 'launch date',MODIFIED TIMESTAMP NOT NULL COMMENT 'modified time',CREATED TIMESTAMP NOT NULL COMMENT 'created time',PRIMARY KEY(ID)) COMMENT='DB WorkerID Assigner for UID Generator',ENGINE = INNODB;
Spring 配置
CachedUidGennerator:
UidGenerator 有兩個具體的實現類,分別是 DefaultUidGenerator 和 CachedUidGenerator, 官方推薦使用效能較強的 CachedUidGenerator。
我們直接引用 UdiGenerator原始碼中的 cached-uid-spring.xml檔案,使用預設配置
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- UID generator --> <bean id="disposableWorkerIdAssigner" class="com.baidu.fsg.uid.worker.DisposableWorkerIdAssigner" /> <bean id="cachedUidGenerator" class="com.baidu.fsg.uid.impl.CachedUidGenerator"> <property name="workerIdAssigner" ref="disposableWorkerIdAssigner" /> <!-- 以下為可選配置,如未指定將採用預設值 --> <!-- RingBuffer size擴容引數,可提高UID生成的吞吐量. --> <!-- 預設:3, 原bufferSize=8192,擴容後bufferSize= 8192 << 3 = 65536 --> <!--<property name="boostPower" value="3"></property>--> <!-- 指定何時向RingBuffer中填充UID,取值為百分比(0,100),預設為50 --> <!-- 舉例: bufferSize=1024,paddingFactor=50 -> threshold=1024 * 50 / 100 = 512. --> <!-- 當環上可用UID數量 < 512時,將自動對RingBuffer進行填充補全 --> <!--<property name="paddingFactor" value="50"></property>--> <!-- 另外一種RingBuffer填充時機,在Schedule執行緒中,週期性檢查填充 --> <!-- 預設:不配置此項,即不實用Schedule執行緒. 如需使用,請指定Schedule執行緒時間間隔,單位:秒 --> <!--<property name="scheduleInterval" value="60"></property>--> <!-- 拒絕策略: 當環已滿,無法繼續填充時 --> <!-- 預設無需指定,將丟棄Put操作,僅日誌記錄. 如有特殊需求,請實現RejectedPutBufferHandler介面(支援Lambda表示式) --> <!--<property name="rejectedPutBufferHandler" ref="XxxxYourPutRejectPolicy"></property>--> <!-- 拒絕策略: 當環已空,無法繼續獲取時 --> <!-- 預設無需指定,將記錄日誌,並丟擲UidGenerateException異常. 如有特殊需求,請實現RejectedTakeBufferHandler介面(支援Lambda表示式) --> <!--<property name="rejectedPutBufferHandler" ref="XxxxYourPutRejectPolicy"></property>--> </bean> </beans>
引入cached-uid-spring.xml配置檔案,在我們自己新建的 UidConfig中
package com.xxx.uid.config; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; /** * @author lishuzhen * @date 2020/8/11 16:10 */ @Configuration @ImportResource(locations = {"classpath:/uid/cached-uid-spring.xml"}) public class UidConfig { }
在另一個模組中maven引入,建立一個UidGenUtils工具類,方便使用
package com.xxxx.utils; import com.xxx.uid.UidGenerator; import org.springframework.stereotype.Component; import javax.annotation.Resource; /** * @author lishuzhen * @date 2020/8/11 16:13 */ @Component public class UidGenUtils { @Resource private UidGenerator uidGenerator; public long getUid() { return uidGenerator.getUID(); } public String getUidStr() { return String.valueOf(uidGenerator.getUID()); } }
到此這篇關於Spring Boot專案整合UidGenerato的方法步驟的文章就介紹到這了,更多相關Spring Boot整合UidGenerato內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!