• 一种基于Orleans的分布式Id生成方案


    基于Orleans的分布式Id生成方案,因Orleans的单实例、单线程模型,让这种实现变的简单,贴出一种实现,欢迎大家提出意见

    public interface ISequenceNoGenerator : Orleans.IGrainWithIntegerKey
    {
       Task<Immutable<string>> GetNext();
    }
    public class SequenceNoGenerator : Orleans.Grain, ISequenceNoGenerator
     {
         private const int MaxSeed = 99999;
         private int _seed = 0;
         private int _currentSecondCounter = 0;
         Task<Immutable<string>> ISequenceNoGenerator.GetNext()
         {
             var oldCounter = this._currentSecondCounter;
    
             while (true)
             {
                 this.UpdateCurrentTimestamp();
    
                 if (oldCounter != this._currentSecondCounter)
                     this._seed = 1;
                 else
                 {
                     ++this._seed;
                 }
    
                 if (this._seed > MaxSeed) Task.Delay(100);
                 else break;
             }
    
             var seq = DateTime.Now.ToString("yyyyMMdd") + this._currentSecondCounter.ToString() + this._seed.ToString();
             return Task.FromResult(seq.AsImmutable());
         }
    
         public override Task OnActivateAsync()
         {
             //延迟1秒启动,防止Activation在某个机器上崩溃后,在集群中其它host上启动时,sequenceNo在同一秒出现重复
              Task.Delay(1000);
    
             return base.OnActivateAsync();
         }
    
         private void UpdateCurrentTimestamp()
         {
             var currentTime = DateTime.Now;
             var currentDayStart = Convert.ToDateTime(currentTime.ToShortDateString());
             this._currentSecondCounter = (int)(new TimeSpan(currentTime.Ticks - currentDayStart.Ticks).TotalSeconds);
         }
     }
    关注开源 国内最早的Orleans群--174511582,欢迎大家加入
  • 相关阅读:
    svn服务器
    TCopyDataStruct 各个参数意义
    Com+连不上解决办法
    流的压缩与解压缩
    Config 文件的增删改查
    Assembly 操作
    redhat7 安装oracle11.2.4页面显示不全解决方法
    IEnumerator和IEnumerable详解
    [我的阿里云服务器] —— 安装LAMP
    设计模式之二抽象工厂设计模式
  • 原文地址:https://www.cnblogs.com/liwt/p/orleans-seq.html
Copyright © 2020-2023  润新知