nodejs雪花ID算法(SnowflakeID)

前言

项目中常使用的三种id类型,分别是自增id、uuid、雪花id,这三种各有优劣。本篇主要实现nodejs中snowflake算法的代码。

一、Snowflake实现

这里需要加入big-integer的模块,下载npm install --save big-integer

var Snowflake = (function() {
            function Snowflake(_workerId, _dataCenterId, _sequence) {
                this.twepoch = 1702032780;//初始时间,设置为0,只能到2100年,尽可能设置当前时间,可以延长时间
                this.workerIdBits = 5; //工作机器码位数
                this.dataCenterIdBits = 5;//数据中心码位数
                this.maxWrokerId = -1 ^ (-1 << this.workerIdBits); // 31位
                this.maxDataCenterId = -1 ^ (-1 << this.dataCenterIdBits); //31位
                this.sequenceBits = 12;//序号位数
                this.workerIdShift = this.sequenceBits; // 12位
                this.dataCenterIdShift = this.sequenceBits + this.workerIdBits; //17位
                this.timestampLeftShift = this.sequenceBits + this.workerIdBits + this.dataCenterIdBits; // 22位
                this.sequenceMask = -1 ^ (-1 << this.sequenceBits); //4095
                this.lastTimestamp = -1;
                //设置默认值
                this.workerId = 1;
                this.dataCenterId = 1;
                this.sequence = 0;
          //
if (this.workerId > this.maxWrokerId || this.workerId < 0) { throw new Error('config.worker_id must max than 0 and small than maxWrokerId-[' + this.maxWrokerId + ']'); } if (this.dataCenterId > this.maxDataCenterId || this.dataCenterId < 0) { throw new Error('config.data_center_id must max than 0 and small than maxDataCenterId-[' + this.maxDataCenterId + ']'); } this.workerId = _workerId; this.dataCenterId = _dataCenterId; this.sequence = _sequence; }
        //获取下一个时间戳 Snowflake.prototype.tilNextMillis
= function(lastTimestamp) { var timestamp = this.timeGen(); while (timestamp <= lastTimestamp) { timestamp = this.timeGen(); } return timestamp; };
       //获取时间戳 Snowflake.prototype.timeGen
= function() { //new Date().getTime() === Date.now() return Math.floor(Date.now()/1000); //这里获取秒级别主要是生成53位内的id数 };
      //生成id Snowflake.prototype.nextId
= function() { var timestamp = this.timeGen(); if (timestamp < this.lastTimestamp) { throw new Error('Clock moved backwards. Refusing to generate id for ' + (this.lastTimestamp - timestamp)); } if (this.lastTimestamp === timestamp) { this.sequence = (this.sequence + 1) & this.sequenceMask; if (this.sequence === 0) { timestamp = this.tilNextMillis(this.lastTimestamp); } } else { this.sequence = 0; } this.lastTimestamp = timestamp; var shiftNum = (this.dataCenterId << this.dataCenterIdShift) | (this.workerId << this.workerIdShift) | this.sequence; // dataCenterId:1,workerId:1,sequence:0 shiftNum:135168 var nfirst = new bigInt(String(timestamp - this.twepoch), 10); nfirst = nfirst.shiftLeft(this.timestampLeftShift); var nnextId = nfirst.or(new bigInt(String(shiftNum), 10)).toString(10); return nnextId; }; return Snowflake; }());

二、一些问题

  若正常的snowflakeID需要64位支持,但是在nodejs中这样需要字符串或者bigInt进行存储。这里可以尝试使用其他方式将snowflakeID降低到53位

    1.将时间戳改为秒级别,2^10刚好是1024,这样产生id不是很多。
2.将10位工作编号舍去,刚好53位(这个适合单机模式)  
3.12位序列号+10位机器号适当减少都可以,满足一定条件

参考

Snowflake(雪花算法)的JavaScript实现:http://hk.javashuo.com/article/p-moygzecn-bd.html

posted @ 2024-01-26 21:26  Auler  阅读(466)  评论(0编辑  收藏  举报