MyBatis使用雪花ID怎么实现
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了MyBatis使用雪花ID怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇MyBatis使用雪花ID怎么实现文章都会有所收获,下面我们一起来看看吧。
一、实现MyBatis ID构建接口
@Slf4j@Componentpublic class CustomIdGenerator implements IdentifierGenerator { @Override public Long nextId(Object entity) { //生成ID long id = SnowFlakeUtils.nextId(); log.info("生成ID: " + id); return id; }}
二、雪花ID生成工具类
@Slf4jpublic class SnowFlakeUtils { private static final long OFFSET = 1546300800L; private static final long WORKER_ID; private static final long WORKER_ID_BITS = 5L; private static final long SEQUENCE_ID_BITS = 16L; private static final long WORKER_SHIFT_BITS = SEQUENCE_ID_BITS; private static final long OFFSET_SHIFT_BITS = SEQUENCE_ID_BITS + WORKER_ID_BITS; private static final long WORKER_ID_MAX = ((1 << WORKER_ID_BITS) - 1) >> 1; private static final long BACK_WORKER_ID_BEGIN = (1 << WORKER_ID_BITS) >> 1; private static final long SEQUENCE_MAX = (1 << SEQUENCE_ID_BITS) - 1; private static final long BACK_TIME_MAX = 1000L; private static long lastTimestamp = 0L; private static long sequence = 0L; private static long lastTimestampBak = 0L; private static long sequenceBak = 0L; static { // 初始化机器ID long workerId = getWorkId(); if (workerId < 0 || workerId > WORKER_ID_MAX) { throw new IllegalArgumentException(String.format("cmallshop.workerId范围: 0 ~ %d 目前: %d", WORKER_ID_MAX, workerId)); } WORKER_ID = workerId; } private static Long getWorkId(){ try { String hostAddress = Inet4Address.getLocalHost().getHostAddress(); int[] ints = StringUtils.toCodePoints(hostAddress); 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() {} public static long nextId() { return nextId(SystemClock.now() / 1000); } 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; } 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怎么实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341