一、扩展修改(Snowflake.Core)
①、安装依赖包
Install-Package Snowflake.Core
②、扩展重写
1、扩展雪花Id,通过更改基数生成数字坐落于(6KM【千兆】),long超过(9KM【9007199254740991】)最大值会丢失精度。
public class SnowflakeWorker : IdWorker { readonly object _lock = new Object(); private long _lastTimestamp = -1L; private long _sequence = 0L; public SnowflakeWorker(long workerId, long datacenterId, long sequence = 0) : base(workerId, datacenterId, sequence) { } public override long NextId() { return base.NextId(); } /// <summary> /// 扩展NextId /// </summary> /// <returns></returns> /// <exception cref="Exception"></exception> public long ReNextId() { lock (_lock) { long num = TimeGen(); if (num < _lastTimestamp) { throw new Exception($"时间戳必须大于上一次生成ID的时间戳. 拒绝为{_lastTimestamp - num}毫秒生成id"); } if (_lastTimestamp == num) { _sequence = ((_sequence + 1) & 0xFFF); if (_sequence == 0L) { num = TilNextMillis(_lastTimestamp); } } else { _sequence = 0L; } _lastTimestamp = num; return ((num - Twepoch) << 14) | (DatacenterId << 12) | (WorkerId << 6) | _sequence; } } }
③、使用
SnowflakeWorker worker = new SnowflakeWorker(1, 1); Console.WriteLine(worker.ReNextId());
二、参考
②、mb5fe09d2d96fc4【Json格式化】
③、SqlSugar
标签:sequence,netcore,lastTimestamp,long,public,num,SnowflakeWorker,Id From: https://www.cnblogs.com/study10000/p/16876692.html