mybatis-plus 雪花算法id长度,mybatis自动生成id

  mybatis-plus 雪花算法id长度,mybatis自动生成id

  

目录

一、实现MyBatis ID构建接口二、雪花身份生成工具类

 

  

一、实现MyBatis ID构建接口

@Slf4j@Componentpublic类自定义生成器实现标识符生成器{ @ Override public Long nextId(对象实体){//生成ID long ID=snowflakeutils。nextid();log.info(生成id : id);返回id;}}

 

  

二、雪花ID生成工具类

@ SLF 4j公共级雪花石膏{ /**初始偏移时间戳*/私有静态最终长偏移量=1546300800L/**机器id (0~15保留16~31作为备份机器)*/private static final long WORKER _ ID;/** 机器编号所占位数(5位,支持最大机器数2^5=32)*/私有静态最终长worker _ id _ bits=5l/**自增序列所占位数(16位,支持最大每秒生成2^16=65536) */私有静态最终长序列_ id _ bits=16l/**机器编号偏移位数*/私有静态最终长WORKER _ SHIFT _ BITS=SEQUENCE _ ID _ bit;/** 自增序列偏移位数*/私有静态final long OFFSET _ SHIFT _ BITS=SEQUENCE _ ID _ BITS WORKER _ ID _ BITS;/** 机器标识最大值(2^5/2 - 1=15) */私有静态最终长worker _ id _ max=((1 worker _ id _ bits)-1)1;/** 备份机器身份开始位置(2^5/2=16) */私有静态最终长back _ worker _ id _ begin=(1 worker _ id _ bits)1;/** 自增序列最大值(2^16 - 1=65535) */私有静态最终长序列_MAX=(1序列_ id _比特)-1;/** 发生时间回拨时容忍的最大回拨时间(秒)*/私有静态final long BACK _ TIME _ MAX=1000 l/* *上次生成身份的时间戳(秒)*/私有静态long lastTimestamp=0L/**当前秒内序列(2^16)*/私静长序列=0l/**备份机器上次生成身份的时间戳(秒)*/private static long lastimestampbak=0L;/** 备份机器当前秒内序列(2^16)*/私静态长序列=0l静态{ //初始化机器ID long workerId=getWorkId();if(workerId 0 workerId WORKER _ ID _ MAX){ throw new IllegalArgumentException(string。格式( cmall shop。工人Id范围: 0 ~ %d目前: %d ,WORKER_ID_MAX,WORKER ID));} WORKER _ ID=workerId } private static Long getwork ID(){ try { String host address=inet 4 address。getlocalhost().getHostAddress();int[]ints=字符串实用程序。去端点(主机地址);int sums

 

  = 0; for(int b : ints){ sums += b; } return (long)(sums % WORKER_ID_MAX); } catch (UnknownHostException e) { // 如果获取失败,则使用随机数备用 return RandomUtils.nextLong(0,WORKER_ID_MAX-1); } } /** 私有构造函数禁止外部访问 */ private SnowFlakeUtils() {} /** * 获取自增序列 * @return long */ public static long nextId() { return nextId(SystemClock.now() / 1000); } /** * 主机器自增序列 * @param timestamp 当前Unix时间戳 * @return long */ private static synchronized long nextId(long timestamp) { // 时钟回拨检查 if (timestamp < lastTimestamp) { // 发生时钟回拨 log.warn("时钟回拨, 启用备份机器ID: now: [{}] last: [{}]", timestamp, lastTimestamp); return nextIdBackup(timestamp); } // 开始下一秒 if (timestamp != lastTimestamp) { lastTimestamp = timestamp; sequence = 0L; } if (0L == (++sequence & SEQUENCE_MAX)) { // 秒内序列用尽// log.warn("秒内[{}]序列用尽, 启用备份机器ID序列", timestamp); sequence--; return nextIdBackup(timestamp); } return ((timestamp - OFFSET) << OFFSET_SHIFT_BITS) (WORKER_ID << WORKER_SHIFT_BITS) sequence; } /** * 备份机器自增序列 * @param timestamp timestamp 当前Unix时间戳 * @return long */ private static long nextIdBackup(long timestamp) { if (timestamp < lastTimestampBak) { if (lastTimestampBak - SystemClock.now() / 1000 <= BACK_TIME_MAX) { timestamp = lastTimestampBak; } else { throw new RuntimeException(String.format("时钟回拨: now: [%d] last: [%d]", timestamp, lastTimestampBak)); } } if (timestamp != lastTimestampBak) { lastTimestampBak = timestamp; sequenceBak = 0L; } if (0L == (++sequenceBak & SEQUENCE_MAX)) { // 秒内序列用尽// logger.warn("秒内[{}]序列用尽, 备份机器ID借取下一秒序列", timestamp); return nextIdBackup(timestamp + 1); } return ((timestamp - OFFSET) << OFFSET_SHIFT_BITS) ((WORKER_ID ^ BACK_WORKER_ID_BEGIN) << WORKER_SHIFT_BITS) sequenceBak; } /** * 并发数 */ private static final int THREAD_NUM = 30000; private static volatile CountDownLatch countDownLatch = new CountDownLatch(THREAD_NUM); public static void main(String[] args) { ConcurrentHashMap<Long,Long> map = new ConcurrentHashMap<>(THREAD_NUM); List<Long> list = Collections.synchronizedList(new LinkedList<>()); for (int i = 0; i < THREAD_NUM; i++) { Thread thread = new Thread(() -> { // 所有的线程在这里等待 try { countDownLatch.await(); Long id = SnowFlakeUtils.nextId(); list.add(id); map.put(id,1L); } catch (InterruptedException e) { e.printStackTrace(); } }); thread.start(); // 启动后,倒计时计数器减一,代表有一个线程准备就绪了 countDownLatch.countDown(); } try{ Thread.sleep(50000); }catch (Exception e){ e.printStackTrace(); } System.out.println("listSize:"+list.size()); System.out.println("mapSize:"+map.size()); System.out.println(map.size() == THREAD_NUM); }}到此这篇关于MyBatis使用雪花ID的实现的文章就介绍到这了,更多相关MyBatis 雪花ID内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: