99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

如何分析Actor-ES框架中的ESGrain與ESRepGrain

這篇文章給大家介紹如何分析Actor-ES框架中的ESGrain與ESRepGrain,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供平順網(wǎng)站建設(shè)、平順做網(wǎng)站、平順網(wǎng)站設(shè)計、平順網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、平順企業(yè)網(wǎng)站模板建站服務(wù),十年平順做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

ESGrain

生命周期

Ray中ESGrain繼承自Grain擴(kuò)展了Grain的生命周期。Grain的生命周期參加文檔附錄:1-Grain生命周期-譯注.md

ESGrain重寫了Grain的OnActivateAsync方法。ESGrain的初始化過程如下:

  • 初始化ESGrain中的State

    • 調(diào)用ReadSnapshotAsync()讀快照。

    • 如果沒有獲得快照,調(diào)用InitState()根據(jù)InitState()中代碼初始化ESGrain,InitState()是虛方法,可以被具體的ESGrain重寫,以自定義初始化。

  • 讀取事件庫,重放事件,獲得最新的State。

如何分析Actor-ES框架中的ESGrain與ESRepGrain


小技巧:在實際開發(fā)中,可以重寫InitState(),在其中根據(jù)關(guān)系型數(shù)據(jù)庫中的數(shù)據(jù)自定義state的初始化。


使用

State

ESGrain的數(shù)據(jù)存儲在State中,當(dāng)ESGrain被激活后State數(shù)據(jù)存儲在內(nèi)存中,持久化會存儲為快照。定義ESGrain時,需要定義State,實現(xiàn)IState接口,序列化默認(rèn)使用protocol buffer,State類要添加protocol buffer特性。IState接口定義的是State的基礎(chǔ)部分,即示例中的base部分,base之外的是當(dāng)前actor需要的要存儲的數(shù)據(jù)。

示例代碼:

[ProtoContract(ImplicitFields = ImplicitFields.AllFields)] public class AccountState : IState<string> {     #region base     public string StateId { get; set; }     public uint Version { get; set; }     public uint DoingVersion { get; set; }     public DateTime VersionTime { get; set; }     #endregion     public decimal Balance { get; set; } }

Event

ESGrain之間通過Event傳遞數(shù)據(jù),Event編寫請參考Event編寫.md

EventHandles

使用ESGrain引發(fā)事件,一般出于兩種考慮:1.傳遞數(shù)據(jù)到Handler;2.修改State中的數(shù)據(jù)。修改ESGrain中的數(shù)據(jù)通過EventHandle中的代碼實現(xiàn)。

使用:

    • 實現(xiàn)IEventHandle

    • 在Apply中實現(xiàn)定義要處理的事件。

      示例代碼:

public class AccountEventHandle : IEventHandle

{

  public void Apply(object state, IEvent evt)

  {

      if (state is AccountState actorState)

      {

          switch (evt)

          {

              case AmountAddEvent value: AmountAddEventHandle(actorState, value); break;

              case AmountTransferEvent value: AmountTransferEventHandle(actorState, value); break;

              default: break;

          }

      }

  }

  private void AmountTransferEventHandle(AccountState state, AmountTransferEvent evt)

  {

      state.Balance = evt.Balance;

  }

  private void AmountAddEventHandle(AccountState state, AmountAddEvent evt)

  {

      state.Balance = evt.Balance;

  }

}


    • State中的數(shù)據(jù)存儲在內(nèi)存中,大量的數(shù)據(jù)存在State中,在某種角度可以將State看做內(nèi)存數(shù)據(jù)庫。

    • Ray中,修改State的數(shù)據(jù)要通過EventHandle實現(xiàn)(只有一種方式)。

    ESGrain種類

    • Ray默認(rèn)提供了MongoESGrain和SqlGrain兩類。

      ESGrain<K, S, W>說明:

    • K:StateId的類型。

    • S:ESGrain的State。

    • W:MessageInfo。

完整ESGrain示例

編寫ESGrain時

  • 明確RabbitPub。

  • 明確MongoStorage。

  • 繼承MongoESGrain或SqlGrain。

  • 實現(xiàn)ESGrain接口。

  • 如果需要重寫OnActivateAsync。

  • 編寫感興趣的Actor方法

  • 如果需要發(fā)送事件:1.定義事件;2.編寫EventHandler。

[RabbitMQ.RabbitPub("Account", "account")]

[MongoStorage("Test", "Account")]

public sealed class Account : MongoESGrain<String, AccountState, IGrains.MessageInfo>, IAccount

{

    protected override string GrainId => this.GetPrimaryKeyString();

    static IEventHandle _eventHandle = new AccountEventHandle();

    protected override IEventHandle EventHandle => _eventHandle;

    public override Task OnActivateAsync()

    {

        return base.OnActivateAsync();

    }

    public Task Transfer(string toAccountId, decimal amount)

    {

        var evt = new AmountTransferEvent(toAccountId, amount, this.State.Balance - amount);

        return RaiseEvent(evt).AsTask();

    }

    public Task AddAmount(decimal amount, string uniqueId = null)

    {

        var evt = new AmountAddEvent(amount, this.State.Balance + amount);

        return RaiseEvent(evt, uniqueId: uniqueId).AsTask();

    }

    [AlwaysInterleave]

    public Task<decimal> GetBalance()

    {

        return Task.FromResult(this.State.Balance);

    }

}

ESRepGrain

ESGrain默認(rèn)是主Actor,當(dāng)單個Actor壓力過大時,可以實現(xiàn)該actor的副本actor,副本actor主要用來處理:1.讀的操作;2.其他非寫的異步操作。

主actor與副本actor之間保持同步的機(jī)制:

  1. 主actor引發(fā)事件,在CoreHandler里將消息傳遞給副本actor,在副本actor里面重放該事件。

  2. 主actor與副本actor持久化的是同一個快照庫、事件庫。也會從同一個庫里激活。

生命周期

與主actor類似。

使用

與ESGrain類似,對比如下:

ESGrainESRepGrain
明確RabbitPub不需要
明確MongoStorage明確MongoStorage
繼承MongoESGrain或SqlGrain繼承MongoESRepGrain或SqlRepGrain
實現(xiàn)ESGrain接口自定義的副本Actor接口
如果需要重寫OnActivateAsync如果需要重寫OnActivateAsync
編寫感興趣的Actor方法編寫感興趣的Actor方法
如果需要發(fā)送事件:1.定義事件;2.編寫EventHandler不會引發(fā)事件

示例

[MongoStorage("Test", "Account")]

public sealed class AccountRep : MongoESRepGrain<String, AccountState, MessageInfo>, IAccountRep

{

     protected override string GrainId => this.GetPrimaryKeyString();

     static IEventHandle _eventHandle = new AccountEventHandle();

     protected override IEventHandle EventHandle => _eventHandle;

     public Task<decimal> GetBalance()

     {

         return Task.FromResult(this.State.Balance);

     }

}

關(guān)于如何分析Actor-ES框架中的ESGrain與ESRepGrain就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

標(biāo)題名稱:如何分析Actor-ES框架中的ESGrain與ESRepGrain
分享URL:http://www.yijiale78.com/article10/pcsedo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化軟件開發(fā)App開發(fā)外貿(mào)建站標(biāo)簽優(yōu)化品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營