SnowFlake算法Java实现(雪花算法)生成分布式ID

本文深入解析Twitter开源的SnowFlake算法,探讨其在分布式环境下生成唯一ID的原理与实现,包括WorkerId和DatacenterId的自动生成、时钟回拨处理等,并提供Java版完整代码及业务定制案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【订阅专栏合集,关注公众号,作者所有付费文章都能看(持续更新)】

SnowFlake 算法是 Twitter 开源的分布式唯一 ID 生成算法,其具有高性能、低延迟、按时间趋势有序等特点。理论支持每毫秒生成 4096 个不同数字,能够满足绝大多数高并发场景下的互联网应用。

本文会讲到如下内容:

  • SnowFlake 的基本概念
  • 基于内存的 WorkerId、DatacenterId 自动生成
  • 单位毫秒内的ID初始值随机生成
  • 运行时、系统重启时 时钟回拨处理
  • 十进制数字字符串位数补齐
  • SnowFlake 完整实现:Java 版
  • 业务定制雪花算法案例:订单号生成

适合人群: 需要在实际应用中生成分布式唯一 ID 的技术人员,Java 程序员

gitchat地址https://gitbook.cn/gitchat/activity/5f43c3e4f9fe2a086081397e

详细代码:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 基于SnowFlake的Id生成器使用方法见下方的main方法中的测试demo
 * 可以自行传入workerId和datacenterId;也可以直接使用默认的构造器创建,开箱即用,简单方便
 */
public class IdGenerator {

    private static final Logger logger = LoggerFactory.getLogger(IdGenerator.class);

    //工作机器id
    private long workerId;
    //数据中心id
    private long datacenterId;
    //序列号
    private long sequence = 0L;

    //基准时间,一般取系统的最近时间(一旦确定不能变动)
    private long twepoch;

    private long workerIdBits;
    private long datacenterIdBits;
    private long maxWorkerId;
    private long maxDatacenterId;

    //毫秒内自增位数
    private long sequenceBits;
    //位与运算保证毫秒内Id范围
    private long sequenceMask;

    //工作机器id需要左移的位数
    private long workerIdShift;
    //数据中心id需要左移位数
    private long datacenterIdShift;
    //时间戳需要左移位数
    private long timestampLeftShift;

    //上次生成id的时间戳,初始值为负数
    private long lastTimestamp = -1L;

    //true表示毫秒内初始序列采用随机值
    private boolean randomSequence;
    //随机初始序列计数器
    private long count = 0L;

    //允许时钟回拨的毫秒数
    private long timeOffset;

    private final ThreadLocalRandom tlr = ThreadLocalRandom.current();

    /**
     * 无参构造器,自动生成workerId/datacenterId,开箱即用
     */
    public IdGenerator() {
        this(false, 10, null, 5L, 5L, 12L);
    }

    /**
     * 有参构造器,调用者自行保证数据中心ID+机器ID的唯一性
     * 标准snowflake实现
     *
     * @param workerId     工作机器 ID
     * @param datacenterId 数据中心ID
     */
    public IdGenerator(long workerId, long datacenterId) {
        this(workerId, datacenterId, false, 10, null, 5L, 5L, 12L);
    }

    /**
     * @param randomSequence   true表示每毫秒内起始序号使用随机值
     * @param timeOffset       允许时间回拨的毫秒数
     * @param epochDate        基准时间
     * @param workerIdBits     workerId位数
     * @param datacenterIdBits datacenterId位数
     * @param sequenceBits     sequence位数
     */
    public IdGenerator(boolean randomSequence, long timeOffset, Date epochDate, long workerIdBits, long datacenterIdBits, long sequenceBits) {
        if (null != epochDate) {
            this.twepoch = epochDate.getTime();
        } else {
            // 2012/12/12 23:59:59 GMT
            this.twepoch = 1355327999000L;
        }

        this.workerIdBits = workerIdBits;
        this.datacenterIdBits = datacenterIdBits;
        this.maxWorkerId = -1L ^ (-1L << workerIdBits);
        this.maxDatacenterId = -1L ^ (-1L << datacenterIdBits);

        this.sequenceBits = sequenceBits;
        this.sequenceMask = -1L ^ (-1L << sequenceBits);

        this.workerIdShift = sequenceBits;
        this.datacenterIdShift = sequenceBits + workerIdBits;
        this.timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;

        this.datacenterId = getDatacenterId(maxDatacenterId);
        this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
        this.randomSequence = randomSequence;
        this.timeOffset = timeOffset;
        String initialInfo = String.format("worker starting. timestamp left shift %d, datacenter id bits %d, worker id bits %d, sequence bits %d, datacenterid  %d, workerid %d",
                timestampLeftShift, datacenterIdBits, workerIdBits, sequenceBits, datacenterId, workerId);
        logger.info(initialInfo);
    }

    /**
     * 自定义workerId+datacenterId+其它初始配置
     * 调整workerId、datacenterId、sequence位数定制雪花算法,控制生成的Id的位数
     *
     * @param workerId         工作机器 ID
     * @param datacenterId     数据中心ID
     * @param randomSequence   true表示每毫秒内起始序号使用随机值
     * @param timeOffset       允许时间回拨的毫秒数
     * @param epochDate        基准时间
     * @param workerIdBits     workerId位数
     * @param datacenterIdBits datacenterId位数
     * @param sequenceBits     sequence位数
     */
    public IdGenerator(long workerId, long datacenterId, boolean randomSequence, long timeOffset, Date epochDate, long workerIdBits, long datacenterIdBits, long sequenceBits) {
        this.workerIdBits = workerIdBits;
        this.datacenterIdBits = datacenterIdBits;
        this.maxWorkerId = -1L ^ (-1L << workerIdBits);
        this.maxDatacenterId = -1L ^ (-1L << datacenterIdBits);

        if (workerId > maxWorkerId || workerId < 0) {
            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0\r\n", maxWorkerId));
        }
        if (datacenterId > maxDatacenterId || datacenterId < 0) {
            throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0\r\n", maxDatacenterId));
        }

        if (null != epochDate) {
            this.twepoch = epochDate.getTime();
        } else {
            // 2012/12/12 23:59:59 GMT
            this.twepoch = 1355327999000L;
        }

        this.sequenceBits = sequenceBits;
        this.sequenceMask = -1L ^ (-1L << sequenceBits);

        this.workerIdShift = sequenceBits;
        this.datacenterIdShift = sequenceBits + workerIdBits;
        this.timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;

        this.workerId = workerId;
        this.datacenterId = datacenterId;
        this.timeOffset = timeOffset;
        this.randomSequence = randomSequence;

        String initialInfo = String.format("worker starting. timestamp left shift %d, datacenter id bits %d, worker id bits %d, sequence bits %d, datacenterid  %d, workerid %d",
                timestampLeftShift, datacenterIdBits, workerIdBits, sequenceBits, datacenterId, workerId);
        logger.info(initialInfo);
    }

    private static long getDatacenterId(long maxDatacenterId) {
        long id = 0L;
        try {
            InetAddress ip = InetAddress.getLocalHost();
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            if (network == null) {
                id = 1L;
            } else {
                byte[] mac = network.getHardwareAddress();
                if (null != mac) {
                    id = ((0x000000FF & (long) mac[mac.length - 1]) | (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
                    id = id % (maxDatacenterId + 1);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("GetDatacenterId Exception", e);
        }
        return id;
    }

    private static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
        StringBuilder macIpPid = new StringBuilder();
        macIpPid.append(datacenterId);
        try {
            String name = ManagementFactory.getRuntimeMXBean().getName();
            if (name != null && !name.isEmpty()) {
                //GET jvmPid
                macIpPid.append(name.split("@")[0]);
            }
            //GET hostIpAddress
            String hostIp = InetAddress.getLocalHost().getHostAddress();
            String ipStr = hostIp.replaceAll("\\.", "");
            macIpPid.append(ipStr);
        } catch (Exception e) {
            throw new RuntimeException("GetMaxWorkerId Exception", e);
        }
        //MAC + PID + IP的 hashcode 取低16位
        return (macIpPid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
    }

    public synchronized long nextId() {
        long currentTimestamp = timeGen();

        //获取当前时间戳如果小于上次时间戳,则表示时间戳获取出现异常
        if (currentTimestamp < lastTimestamp) {
            // 校验时间偏移回拨量
            long offset = lastTimestamp - currentTimestamp;
            if (offset > timeOffset) {
                throw new RuntimeException("Clock moved backwards, refusing to generate id for [" + offset + "ms]");
            }

            try {
                // 时间回退timeOffset毫秒内,则允许等待2倍的偏移量后重新获取,解决小范围的时间回拨问题
                this.wait(offset << 1);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            currentTimestamp = timeGen();
            if (currentTimestamp < lastTimestamp) {
                throw new RuntimeException("Clock moved backwards, refusing to generate id for [" + offset + "ms]");
            }
        }

        //如果获取的当前时间戳等于上次时间戳(即同一毫秒内),则序列号自增
        if (lastTimestamp == currentTimestamp) {
            // randomSequence为true表示随机生成允许范围内的起始序列,否则毫秒内起始值从0L开始自增
            long tempSequence = sequence + 1;
            if (randomSequence) {
                sequence = tempSequence & sequenceMask;
                count = (count + 1) & sequenceMask;
                if (count == 0) {
                    currentTimestamp = this.tillNextMillis(lastTimestamp);
                }
            } else {
                sequence = tempSequence & sequenceMask;
                if (sequence == 0) {
                    currentTimestamp = this.tillNextMillis(lastTimestamp);
                }
            }
        } else {
            sequence = randomSequence ? tlr.nextLong(sequenceMask + 1) : 0L;
            count = 0L;
        }

        lastTimestamp = currentTimestamp;

        return ((currentTimestamp - twepoch) << timestampLeftShift) |
                (datacenterId << datacenterIdShift) |
                (workerId << workerIdShift) |
                sequence;
    }

    private long tillNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }

    private long timeGen() {
        return System.currentTimeMillis();
    }

//测试
    public static void main(String[] args) {
//        for (int i = 0; i < 10; i++) {
//            IdGenerator idGenerator = new IdGenerator();//使用默认构造器创建,开箱即用
//            new Thread(() -> {
//                for (int j = 0; j < 100; j++) {
//                    System.out.println(idGenerator.nextId());
//                }
//            }).start();
//        }

//        IdGenerator idGenerator = new IdGenerator(1, 1);
//        for (int j = 0; j < 2000; j++) {
//            System.out.println(System.currentTimeMillis() + " " + idGenerator.nextId());
//        }

//        IdGenerator idGenerator = new IdGenerator(true, 10, null, 3L, 2L, 7L);
//        for (int j = 0; j < 2000; j++) {
//            System.out.println(System.currentTimeMillis() + " " + idGenerator.nextId());
//        }

        IdGenerator shortIdGenerator = new IdGenerator(7, 3, true, 10, null, 3, 2, 7);
        for (int j = 0; j < 1000; j++) {
            System.out.println(System.currentTimeMillis() + " " + shortIdGenerator.nextId());
        }
    }
}

如有问题请留言~

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程猿薇茑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值