using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading; /// <summary>
/// ID 生成器
/// </summary>
public class IDFactory
{
#region Static Fields /// <summary>
/// The factory.
/// </summary>
private static IDFactory factory; #endregion #region Fields /// <summary>
/// The init millisecond.
/// </summary>
private readonly ulong InitMillisecond; /// <summary>
/// The m watch.
/// </summary>
private readonly Stopwatch mWatch = new Stopwatch(); /// <summary>
/// The m current millisecond.
/// </summary>
private ulong mCurrentMillisecond; /// <summary>
/// The m seed.
/// </summary>
private byte mSeed; #endregion #region Constructors and Destructors /// <summary>
/// Initializes a new instance of the <see cref="IDFactory" /> class.
/// </summary>
public IDFactory()
{
this.InitMillisecond = (ulong)(DateTime.Now - DateTime.Parse("2015-1-1")).TotalMilliseconds;
this.mWatch.Restart();
} static IDFactory()
{
LoadIPGroup();
} #endregion #region Public Properties /// <summary>
/// Gets or sets the group.
/// </summary>
public static byte Group { get; set; } #endregion #region Public Methods and Operators /// <summary>
/// 生成 ID 种子
/// </summary>
/// <returns>返回ID种子。参见<see cref="long" /></returns>
public static long Create()
{
if (factory == null)
{
factory = new IDFactory();
} return (long)factory.Next();
} /// <summary>
/// 获取IP
/// </summary>
public static void LoadIPGroup()
{
string hostName = Dns.GetHostName();
IPAddress[] addressList = Dns.GetHostAddresses(hostName);
foreach (IPAddress ip in addressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{ byte[] data = ip.GetAddressBytes();
uint value = BitConverter.ToUInt32(data, );
value = value << ;
value = value >> ;
Group = (byte)value;
break;
}
}
} /// <summary>
/// 生成ID种子
/// </summary>
/// <returns>
/// The <see cref="ulong" />.
/// </returns>
public ulong Next()
{
ulong result = ;
var slock = new SpinLock();
bool gotLock = false;
try
{
while (!gotLock)
{
slock.Enter(ref gotLock);
if (gotLock)
{
ulong cms = (ulong)this.mWatch.Elapsed.TotalMilliseconds + this.InitMillisecond;
if (cms != this.mCurrentMillisecond)
{
this.mSeed = ;
this.mCurrentMillisecond = cms;
} //result = ((ulong)this.Group << 58) | (this.mCurrentMillisecond << 8) | this.mSeed;
//result = ((ulong)Group << 9) | (this.mCurrentMillisecond << 17) | this.mSeed;
//result = this.mCurrentMillisecond * 1000000 + (ulong)Group * 1000 + this.mSeed;
result = this.mCurrentMillisecond * + (ulong)Group * + this.mSeed;
this.mSeed++;
}
}
}
finally
{
if (gotLock)
{
slock.Exit();
}
} return result;
} #endregion
}

ID 生成器的更多相关文章

  1. 分布式的Id生成器

    项目中需要一个分布式的Id生成器,twitter的Snowflake中这个既简单又高效,网上找的Java版本 package com.cqfc.id; import org.slf4j.Logger; ...

  2. 分布式ID生成器 zz

    简介 这个是根据twitter的snowflake来写的.这里有中文的介绍. 如上图所示,一个64位ID,除了最左边的符号位不用(固定为0,以保证生成的ID都是正数),还剩余63位可用. 下面的代码与 ...

  3. IOS ID生成器

    // // IdGenerator.m // Copyright (c) 2014年 青岛拓宇网络科技有限公司. All rights reserved. // #import "IdGen ...

  4. 分布式ID生成器

    最近会写一篇分布式的ID生成器的文章,先占位.借鉴Mongodb的ObjectId的生成: 4byte时间戳 + 3byte机器标识 + 2byte PID + 3byte自增id 简单代码: imp ...

  5. 游戏服务器ID生成器组件

    游戏服务器程序中,经常需要生成全局的唯一ID号,这个功能很常用,本文将介绍一种通用ID生成组件.游戏服务器程序中使用此组件的场景有: 创建角色时,为其分配唯一ID 创建物品时,每个物品需要唯一ID 创 ...

  6. ID生成器的一种可扩展实现方案

    ID生成器主要为了解决业务程序生成记录ID的场景,而一个好的ID生成器肯定要满足扩展性好.并发性好的特点,本文下面介绍一种满足上述特点的实现方案. 此方案的核心思想是:每次需要扩容机器时,将每个节点维 ...

  7. ID生成器详解

    概述 ID 生成器也叫发号器,它的主要目的就是"为一个分布式系统的数据object产生一个唯一的标识",但其实在一个真实的系统里可能也可以承担更多的作用.概括起来主要有以下几点: ...

  8. 业务系统需要什么样的ID生成器

    业务系统需要什么样的ID生成器 ID 生成器在微博我们一直叫发号器,微博就是用这样的号来存储,而我微博里讨论的时候也都是以发号器为标签.它的主要目的确如平常大家理解的“为一个分布式系统的数据objec ...

  9. twitter的ID生成器的snowFlake算法的自造版

    snowFlake算法在生成ID时特别高效,可参考:https://segmentfault.com/a/1190000011282426 SnowFlake算法生成id的结果是一个64bit大小的整 ...

  10. snowflake 分布式唯一ID生成器

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 摘要: 原文参考运维生存和开源中国上的代码整理 我的环境是pytho ...

随机推荐

  1. c#中的特性Attribute

    一:特性是什么?特性怎么创建怎么使用? 这一章节,我想谈谈c#的特性方面的知识,特性大家在工作开发中都很熟悉,比如我们经常见到的 1:key Display --EF 2:Import 3:HttpG ...

  2. 如何快速将一个list<a>集合中的部分字段值组合成新的的list<b>部分*

    有的时候,我们只需要从老数据中拿一部分数据作为新的绑定数据,比如说绑定下拉框的时候需要构造我们需要的数据格式可以采用以下的方法 public class SelectDataViewModel { p ...

  3. 【微服务No.3】AOP组件ASPectCore简单使用

    介绍: AspectCore是.NET标准的基于AOP的跨平台框架[github解释].主要支持:对方面拦截器,依赖注入集成,Web应用程序,数据验证等的核心支持. 使用实例: 首先安装dll: In ...

  4. Failed at the @ watch script 'cross-env NODE_ENV=development 使用cross-env解决跨平台设置NODE_ENV的问题

    今天在安装js依赖包时,根目录的package.json: { "private": true, "scripts": { "dev": & ...

  5. Java学习笔记之——final关键字

    最终的,最后的 可以修饰:变量.方法.类 (1) 修饰变量 不可改变,即常量,只能赋值一次,赋值之后不可改变 a. 修饰属性:private final int num; 要求属性要显式赋值 通常在属 ...

  6. 数据结构——基于java的链表实现(真正理解链表这种数据结构)

    原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10759599.html,否则将追究法律责任!!! 一.链表介绍 1.什么是链表? 链表是一 ...

  7. Hibernate入门(一)

    1.导包 导入Hibernate最基本的包(不要忘记导入数据库驱动包了!) 下载文件名为黄色框框中的名称的压缩包在对应路径下,有个required包下的所有包就是必备的基本包 2.建表 USE TES ...

  8. linux下的~/

    在linux里面,~/表示的是个人目录,例如你的账户是student,那么~/代表的是/home/student/

  9. springboot之scheduled任务调度

    springboot整合Scheduled可以方便的进行任务调度,话不多说,直接上代码 package com.rookie.bigdata; import org.springframework.b ...

  10. WORLD 快速线

    1,3个“-”     一条直线 2,3个“=”    一条双直线 3,3个“*”     一条虚线 4,3个“~”     一条波浪线 5, 3个“#”    一条隔行线