关于全局ID,雪花(snowflake)算法的说明
2016-12-04 13:25
309 查看
上次简单的说一下:http://www.cnblogs.com/dunitian/p/6041745.html#uid
C#版本的国外朋友已经封装了,大家可以去看看:https://github.com/ccollie/snowflake-net
强大的网友出来个简化版本:http://blog.csdn.net/***/article/details/*** (地址我就不贴了,对前辈需要最起码的尊敬)
一开始我用的是这个简化版本,后来发现有重复项。。。(demo:https://github.com/dunitian/TempCode/tree/master/2016-11-16/Twitter_Snowflake)
全局ID的激烈讨论:https://q.cnblogs.com/q/53552/
之后在外国大牛的基础上重写修改了部分内容(https://github.com/ccollie/snowflake-net),添加了一些注解等【支持Core】。现在是可以去Nuget直接下载使用的:Snowflake.Net
源码地址:https://github.com/dunitian/snowflake-net
测试用例:
测试代码:
可能有些人只关心以后怎么用?==》
IdWorker worker = new IdWorker(1, 1); //大并发的情况下,减少new的次数可以有效避免重复的可能
var id = worker.NextId();
有可能上面的减少new有些同志不太懂,(⊙o⊙)…,举个例子:
测试代码不变的情况下,改这么一句:
完整调用demo:(https://github.com/dunitian/snowflake-net/tree/master/Demo)
core:(https://github.com/dunitian/snowflake-net/tree/master/Demo.Core)
IdWorker.Init().NextId()
C#版本的国外朋友已经封装了,大家可以去看看:https://github.com/ccollie/snowflake-net
强大的网友出来个简化版本:http://blog.csdn.net/***/article/details/*** (地址我就不贴了,对前辈需要最起码的尊敬)
一开始我用的是这个简化版本,后来发现有重复项。。。(demo:https://github.com/dunitian/TempCode/tree/master/2016-11-16/Twitter_Snowflake)
全局ID的激烈讨论:https://q.cnblogs.com/q/53552/
之后在外国大牛的基础上重写修改了部分内容(https://github.com/ccollie/snowflake-net),添加了一些注解等【支持Core】。现在是可以去Nuget直接下载使用的:Snowflake.Net
源码地址:https://github.com/dunitian/snowflake-net
测试用例:
测试代码:
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Snowflake.Net; namespace Snowflake.ZConsole { class Program { private static int N = 2000000; private static HashSet<long> set = new HashSet<long>(); private static IdWorker worker = new IdWorker(1, 1); private static int taskCount = 0; static void Main(string[] args) { Task.Run(() => GetID()); Task.Run(() => GetID()); Task.Run(() => GetID()); Task.Run(() => Printf()); Console.ReadKey(); } private static void Printf() { while (taskCount != 3) { Console.WriteLine("..."); Thread.Sleep(1000); } Console.WriteLine(set.Count == N * taskCount); } private static object o = new object(); private static void GetID() { for (var i = 0; i < N; i++) { var id = worker.NextId(); lock (o) { if (set.Contains(id)) { Console.WriteLine("发现重复项 : {0}", id); } else { set.Add(id); } } } Console.WriteLine($"任务{++taskCount}完成"); } } }
可能有些人只关心以后怎么用?==》
IdWorker worker = new IdWorker(1, 1); //大并发的情况下,减少new的次数可以有效避免重复的可能
var id = worker.NextId();
有可能上面的减少new有些同志不太懂,(⊙o⊙)…,举个例子:
测试代码不变的情况下,改这么一句:
完整调用demo:(https://github.com/dunitian/snowflake-net/tree/master/Demo)
core:(https://github.com/dunitian/snowflake-net/tree/master/Demo.Core)
IdWorker.Init().NextId()
相关文章推荐
- 关于全局ID,雪花(snowflake)算法的说明
- 分布式全局ID的Twitter的Snowflake(雪花算法)
- 全局唯一ID生成常见的几种方式和twitter/snowflake(雪花算法)解析
- Twitter的雪花算法(snowflake)自增ID
- Twitter的雪花算法(snowflake)自增ID
- Twitter的分布式雪花算法 SnowFlake 每秒自增生成26个万个可排序的ID (Java版)
- Twitter的分布式自增ID雪花算法snowflake (Java版)
- Twitter的分布式雪花算法 SnowFlake 每秒自增生成26个万个可排序的ID (Java版)
- Twitter的分布式自增ID算法Snowflake的PHP实现,Snowflake PHP版本,高并发唯一id,全局唯一id,不重复id
- 【转】关于 asp.net 服务器控件几个 ID 的说明
- 关于几种压缩算法以及hadoop和hbase中的压缩配置说明
- Twitter的分布式自增ID算法Snowflake实现分析及其Java、Php和Python版
- Twitter-Snowflake,64位自增ID算法详解
- 关于WCF SessionId的说明
- 关于全局唯一ID生成方法
- Twitter-Snowflake,64位自增ID算法详解
- 全局唯一ID生成器(Snowflake ID组成)
- 关于几种压缩算法以及hadoop和hbase中的压缩配置说明
- 专访DeepID发明者孙祎:关于深度学习与人脸算法的深层思考
- 关于几种压缩算法以及hadoop和hbase中的压缩配置说明