目录
第五步:启动类上加上 @MapperScan(“com.baidu.fsg”)
UidGenerator是百度开源的基于雪花算法的唯一id生成器(百度对雪花算法进行了改进)一种分布式id,对雪花算法的一种增强
GitHub - baidu/uid-generator: UniqueID generator
拉去项目导入idea中、并进行打包操作
新建一个springboot项目、并导入从源码打的jar包
mvn install:install-file -Dfile=D:\uid-generator-1.0.0-SNAPSHOT.jar -DgroupId=com.generator -DartifactId=uid-generator -Dversion=1.0.0-SNAPSHOT -Dpackaging=jar
引入依赖
- <dependency>
- <groupId>commons-langgroupId>
- <artifactId>commons-langartifactId>
- <version>2.6version>
- dependency>
- 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;
在新建的springboot项目的resources下新建mapper文件夹放入WORKER_NODE.xml文件。注:这个文件是从源码项目中copy过来的项目位置在一下文件夹中
-
- # 端口号
- server.port=9999
- # 数据库地址
- spring.datasource.url=jdbc:mysql://localhost:3306/study_test?serverTimezone=UTC
- # 用户名
- spring.datasource.username=root
- # 密码
- spring.datasource.password=0407
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- # UidGenerator 以下是自定义的配置信息
-
- uid.epochStr=2020-05-08
- # 时间位, 默认:30
- uid.timeBits=41
- # 机器位, 默认:16
- uid.workerBits=10
- # 序列号, 默认:7
- uid.seqBits=12
- # 是否容忍时钟回拨, 默认:true
- uid.enableBackward=true
- # RingBuffer size扩容参数, 可提高UID生成的吞吐量, 默认:3
- uid.CachedUidGenerator.boostPower=3
- # 指定何时向RingBuffer中填充UID, 取值为百分比(0, 100), 默认为50
- uid.CachedUidGenerator.paddingFactor=50
-
- #mybatis-plus配置mapper扫描 重点
- mybatis.mapper-locations=classpath*:mapper/**/*Mapper.xml,classpath*:/mapper/WORKER_NODE.xml
- package com.lyf.poi.config;
-
-
- import com.baidu.fsg.uid.buffer.RejectedPutBufferHandler;
- import com.baidu.fsg.uid.buffer.RejectedTakeBufferHandler;
- import com.baidu.fsg.uid.buffer.RingBuffer;
- import com.baidu.fsg.uid.impl.CachedUidGenerator;
- import com.baidu.fsg.uid.impl.DefaultUidGenerator;
- import com.baidu.fsg.uid.worker.DisposableWorkerIdAssigner;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- /**
- * @author liuyaofu
- * @create 2022-08-12-23:14
- * 两种生成策略注入Bean
- */
- @Configuration
- public class UidGeneratorConfiguration {
-
- /**
- * RingBuffer size扩容参数, 可提高UID生成的吞吐量. -->
- * 默认:3, 原bufferSize=8192, 扩容后bufferSize= 8192 << 3 = 65536
- * CachedUidGenerator 参数{@link UidGeneratorConfiguration#cachedUidGenerator()}
- */
- private int boostPower = 3;
- /**
- * 指定何时向RingBuffer中填充UID, 取值为百分比(0, 100), 默认为50 -->
- * 举例: bufferSize=1024, paddingFactor=50 -> threshold=1024 * 50 / 100 = 512.
- * 当环上可用UID数量 < 512时, 将自动对RingBuffer进行填充补全
- */
- private int paddingFactor = 50;
- /**
- * 另外一种RingBuffer填充时机, 在Schedule线程中, 周期性检查填充
- * 默认:不配置此项, 即不实用Schedule线程. 如需使用, 请指定Schedule线程时间间隔, 单位:秒
- */
- private Long scheduleInterval;
- /**
- * 拒绝策略: 当环已满, 无法继续填充时 -->
- * 默认无需指定, 将丢弃Put操作, 仅日志记录. 如有特殊需求, 请实现RejectedPutBufferHandler接口(支持Lambda表达式)
- */
- private RejectedPutBufferHandler rejectedPutBufferHandler;
- /**
- * 拒绝策略: 当环已空, 无法继续获取时 -->
- * 默认无需指定, 将记录日志, 并抛出UidGenerateException异常. 如有特殊需求, 请实现RejectedTakeBufferHandler接口(支持Lambda表达式)
- */
- private RejectedTakeBufferHandler rejectedTakeBufferHandler;
- /**
- * 暂时不知道
- */
- private RingBuffer ringBuffer;
-
- @Bean(name = "cachedUidGenerator")
- public CachedUidGenerator cachedUidGenerator(){
- CachedUidGenerator cachedUidGenerator = new CachedUidGenerator();
- cachedUidGenerator.setWorkerIdAssigner(disposableWorkerIdAssigner());
- return cachedUidGenerator;
- }
-
- @Bean(name = "disposableWorkerIdAssigner")
- public DisposableWorkerIdAssigner disposableWorkerIdAssigner(){
- return new DisposableWorkerIdAssigner();
- }
-
-
- /**
- * # 时间位, 默认:28
- */
- private int timeBits;
- /**
- * # 机器位, 默认:22
- */
- private int workerBits;
- /**
- * # 序列号, 默认:13
- */
- private int seqBits;
- /**
- * # 初始时间, 默认:"2016-05-20"
- */
- @Value("${uid.epochStr}")
- private String epochStr;
-
- @Bean(name = "defaultUidGenerator")
- public DefaultUidGenerator defaultUidGenerator(){
- DefaultUidGenerator defaultUidGenerator = new DefaultUidGenerator();
- defaultUidGenerator.setWorkerIdAssigner(disposableWorkerIdAssigner());
- return defaultUidGenerator;
- }
- }
- package com.lyf.poi.util;
-
-
- import com.baidu.fsg.uid.impl.CachedUidGenerator;
- import org.springframework.stereotype.Component;
-
- import javax.annotation.Resource;
-
- /**
- * @author liuyaofu
- * @create 2022-08-12-23:15
- * id自增的一种方式
- */
-
- @Component
- public class IdGenerator {
-
- @Resource(name = "cachedUidGenerator")
- private CachedUidGenerator cachedUidGenerator;
-
- /**
- * 获取uid
- *
- * @return
- */
- public long nextId() {
- return cachedUidGenerator.getUID();
- }
-
- /**
- * 格式化传入的uid,方便查看其实际含义
- *
- * @param uid
- * @return
- */
- public String parse(long uid) {
- return cachedUidGenerator.parseUID(uid);
- }
- }
- package com.lyf.poi.controller;
-
-
-
- import com.lyf.poi.util.DefGenerator;
- import com.lyf.poi.util.IdGenerator;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import javax.annotation.Resource;
-
- /**
- * @author liuyaofu
- * @create 2022-08-12-23:16
- */
- @RestController
- public class UidController {
-
- @Resource
- private IdGenerator idGenerator;
-
- @Resource
- private DefGenerator defGenerator;
-
-
- @GetMapping(value = "getUid")
- public long getUid(){
- long l = idGenerator.nextId();
- String parse = idGenerator.parse(l);
- System.out.println(parse);
- return l;
- }
-
- @GetMapping(value = "getUidDef")
- public long getUidDef(){
- long l = defGenerator.nextId();
- String parse = defGenerator.parse(l);
- System.out.println(parse);
- return l;
- }
-
-
-
- }
测试结果