首页 > 编程语言 >nodejs雪花ID算法(SnowflakeID)

nodejs雪花ID算法(SnowflakeID)

时间:2024-01-26 21:35:43浏览次数:27  
标签:dataCenterId nodejs timestamp sequence lastTimestamp ID SnowflakeID Snowflake id

前言

项目中常使用的三种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位机器号适当减少都可以,满足一定条件

参考

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

标签:dataCenterId,nodejs,timestamp,sequence,lastTimestamp,ID,SnowflakeID,Snowflake,id
From: https://www.cnblogs.com/zrl66/p/17990775

相关文章

  • FluentValidation在C#的应用
    FluentValidation在C#WPF中的应用  1.引言在.NET开发领域,FluentValidation以其优雅、易扩展的特性成为开发者进行属性验证的首选工具。它不仅适用于Web开发,如MVC、WebAPI和ASP.NETCORE,同样也能完美集成在WPF应用程序中,提供强大的数据验证功能。本文将深入探讨如何在C#......
  • nodejs带图标的二维码
    前言实现用nodejs生成二维码一、生成二维码先下载qrcode模块,可以使用npminstall--saveqrcode/**@paramstext文本数据@paramscallback回调函数*/vargetQrcode=function(text,callback){constoptions={width:256,height:256,......
  • nodejs实现文件上传
    前言随着前端的发展,本属于后端需要处理的一些功能模块也逐渐可以让前端实现。本篇大致记录一下文件上传功能。一、上传文件这里使用express+multer框架constpath=require('path')constfs=require('fs')constexpress=require('express');//4.18.2constmulter=r......
  • IntelliJ Idea使用技巧
    1.先设置成macOS上的VSCode快捷键映射。 2.帮助->更改内存设置->设置8192可以不怎么卡 3. 本地idea依赖proto文件生成的类飘红:1.安装protobuf插件,使用protobuf插件编译下2. 将proto目录标记为源代码根目录3.帮助中编辑Idea自定义属性......
  • python--pyQt 基础框架代码 pyside6
    importsysfromPySide6importQtWidgets,QtCore,QtGuifromPySide6.QtCoreimportQt,QRectfromPySide6.QtGuiimportQColor,QEnterEventfromPySide6.QtWidgetsimportQApplication,QDialog,QMainWindow,QGraphicsDropShadowEffectimportyiqi_uiclassMain......
  • Android如何通过按钮实现页面跳转方法
    Hello大家好!我是咕噜铁蛋!在Android应用开发中,页面跳转是一项基本且常见的功能。通过按钮实现页面跳转可以为用户提供更好的交互体验,使应用更加灵活和易用。本文将介绍AndroidStudio中如何通过按钮实现页面跳转的方法,帮助开发者轻松实现这一功能。一、前提准备我们需要具备以下几个......
  • k8s 报错: node(s) didn't match Pod's node affinity.
    前言k8s集群中,有pod出现了Affinity,使用kubectldescribepod命令,发现了报错2node(s)didn'tmatchPod'snodeaffinity.这是因为节点被打上了污点,导致了pod没有节点可以起来解决kubectlgetnodes-ojson|jq'.items[].spec'orkubectlgetnodes-oyaml找到......
  • Mygin实现中间件Middleware
    本篇是mygin的第六篇,参照gin框架,感兴趣的可以从Mygin第一篇开始看,Mygin从零开始完全手写,在实现的同时,带你一窥gin框架的核心原理实现。目的实现中间件Middleware在上一篇Mygin实现分组路由Group中,实现了路由分组,且同一分组的执行,会先执行Group,有一点点中间件的雏形了。但......
  • 封装Excel读取,导出(实体类集合List、DataTable、DataGridView、实体类集合和DataTable
     1、引入使用 #region读取excel///<summary>///根据Excel和Sheet返回DataTable///</summary>///<paramname="filePath">Excel文件地址</param>///<paramname="sheetIndex">She......
  • Blazor Hybrid应用将非wwwroot目录下的文件加入静态资源
    以Winfrom为例,创建一个Class继承BlazorWebView这个类,重写CreateFileProvider这个方法就行。保存后,用新控件替换原来的控件,WPF,MAUI同理,但是MAUI只有Windows平台能用下面的代码。其他平台会报错,找不到文件。publicclassCustomBlazorWebView:BlazorWebView{......