SnowFlakeId 分布式雪花id算法
Posted huanghuanghui
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SnowFlakeId 分布式雪花id算法相关的知识,希望对你有一定的参考价值。
package com.jn.baseservice.utils; import com.jn.baseservice.common.Number; import lombok.Getter; import lombok.Setter; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; import org.springframework.context.ApplicationContext; @Log4j2 @Getter @Setter public class SnowFlake // 起始的时间戳 private final static long START_TIMESTAMP = 1555894860007l; // 每一部分占用的位数,就三个 private final static long SEQUENCE_BIT = 12;// 序列号占用的位数 private final static long MACHINE_BIT = 5;// 机器标识占用的位数 private final static long DATA_CENTER_BIT = 5;// 数据中心占用的位数 // 每一部分最大值 private final static long MAX_DATA_CENTER_NUM = 2 << DATA_CENTER_BIT - 1; private final static long MAX_MACHINE_NUM = 2 << MACHINE_BIT - 1; private final static long MAX_SEQUENCE = 2 << SEQUENCE_BIT - 1; // 每一部分向左的位移 private final static long MACHINE_LEFT = SEQUENCE_BIT; private final static long DATA_CENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT; private final static long TIMESTAMP_LEFT = DATA_CENTER_LEFT + DATA_CENTER_BIT; private static long dataCenterId; // 数据中心 -- 分组按照 应用分组 private static long machineId; // 机器标识 private static long sequence = 0L; // 序列号 private static long lastTimestamp = -1L;// 上一次时间戳 public static boolean check; /** * 构造器 * * @param dataCenterId * @param machineId */ private static void SnowFlake(long dataCenterId, long machineId) if (dataCenterId > MAX_DATA_CENTER_NUM || dataCenterId < 0) throw new IllegalArgumentException("dataCenterId can‘t be greater than MAX_DATA_CENTER_NUM or less than 0"); if (machineId > MAX_MACHINE_NUM || machineId < 0) throw new IllegalArgumentException("machineId can‘t be greater than MAX_MACHINE_NUM or less than 0"); SnowFlake.dataCenterId = dataCenterId; SnowFlake.machineId = machineId; /** * 初始化检查 * * @param context */ public synchronized static void initOrCheck(ApplicationContext context) String applicationName = context.getId(); long applicationHashCode = Math.abs(applicationName.hashCode() % MAX_DATA_CENTER_NUM); log.info("applicationName:,dataCenterId:", applicationName, applicationHashCode); long ipHashCode = getIpCode(); log.info("ipHashCode:", ipHashCode); SnowFlake(applicationHashCode, ipHashCode); check = true; /** * 获取ip * * @return */ public static long getIpCode() String httpString = HttpUtil.getRequest("http://2019.ip138.com/ic.asp"); log.info("获取ip回执:" + httpString); try if (httpString != null) httpString = httpString.substring(httpString.indexOf("[") + Number.ONE, httpString.indexOf("]")); catch (Exception e) log.error("处理ip异常:", e); log.info("处理后ip:" + httpString); long ipHashCode = StringUtils.isEmpty(httpString) ? System.currentTimeMillis() % MAX_MACHINE_NUM : Math.abs(httpString.hashCode() % MAX_MACHINE_NUM); return ipHashCode; /** * 实际获取id * * @return */ public synchronized static long nextId() long currStamp = System.currentTimeMillis(); if (currStamp < lastTimestamp) throw new RuntimeException("Clock moved backwards. Refusing to generate id"); if (currStamp == lastTimestamp) //if条件里表示当前调用和上一次调用落在了相同毫秒内,只能通过第三部分,序列号自增来判断为唯一,所以+1. sequence = (sequence + 1) & MAX_SEQUENCE; //同一毫秒的序列数已经达到最大,只能等待下一个毫秒 if (sequence == 0L) currStamp = getNextMill(); else //不同毫秒内,序列号置为0 //执行到这个分支的前提是currTimestamp > lastTimestamp,说明本次调用跟上次调用对比,已经不再同一个毫秒内了,这个时候序号可以重新回置0了。 sequence = 0L; lastTimestamp = currStamp; //就是用相对毫秒数、机器ID和自增序号拼接 return (currStamp - START_TIMESTAMP) << TIMESTAMP_LEFT //时间戳部分 | dataCenterId << DATA_CENTER_LEFT //数据中心部分 | machineId << MACHINE_LEFT //机器标识部分 | sequence; //序列号部分 /** * 获取时间戳 * * @return */ private static long getNextMill() long mill = System.currentTimeMillis(); while (mill <= lastTimestamp) mill = System.currentTimeMillis(); return mill;
//获取雪花id public static long getSnowFlakeId() if (!SnowFlake.check) SnowFlake.initOrCheck(SpringConfigTool.getContext()); return SnowFlake.nextId();
以上是关于SnowFlakeId 分布式雪花id算法的主要内容,如果未能解决你的问题,请参考以下文章