• 2.Ray-消息发布器与消息存储器


    消息发布器:

    Ray是基于Event Sourcing设计的ES/Actor框架,ESGrain状态(State)的修改、ESGrain之间的通信默认使用RabbitMQ通信。消息的发布器主要是RabbitPub+ESGrain

    RabbitPub特性

    RabbitPub特性是RabbitMQ消息发布器。

    RabbitSub特性用到的构造函数如下:

    public RabbitPubAttribute(string exchange = null, string queue = null, int queueCount = 1)
     {
         this.Exchange = exchange;
         this.Queue = queue;
         this.QueueCount = queueCount;
     }
    
    • exchange:RabbitMQ中的exchange名称。
    • queue:RabbitMQ中的queue名称。
    • queueCount:消息队列数。用于消息的负载均衡。

    使用:

    • 为对应的Actor添加RabbitMQ.RabbitPub特性
    • 继承MongoESGrain或SqlGrain
    • 在方法中使用实例化事件,并调用RaiseEvent发布事件。

    示例:

    [RabbitMQ.RabbitPub("Account", "account")]
    public sealed class Account : MongoESGrain<String, AccountState, IGrains.MessageInfo>, IAccount
     {
      ……
     }
    public Task Transfer(string toAccountId, decimal amount)
     {
         var evt = new AmountTransferEvent(toAccountId, amount, this.State.Balance - amount);
         return RaiseEvent(evt).AsTask();
     }
    

    RabbitPub可以单独使用,用于发布消息。


    消息存储器:

    消息的存储器用于持久化ESGrain的Event事件与State快照数据,需要的时候进行重放。Ray默认使用MongoDB存储事件和快照。

    使用: 为对应的Actor添加MongoStorage特性。

    public MongoStorageAttribute(string eventDatabase, string collection, bool sharding = false, int shardingDays = 90)
    {
        this.EventDataBase = eventDatabase;
        this.EventCollection = collection + "Event";
        this.SnapshotCollection = collection + "State";
        this.sharding = sharding;
        this.shardingDays = shardingDays;
        CreateCollectionIndex();//创建分表索引
        CreateStateIndex();//创建快照索引
    }
    
    • eventDatabase:事件的Database名称。
    • collection:事件的collection名称。
    • sharding:是否需要分表,默认值false。
    • shardingDays:分表时间间隔,默认值90天。

    示例:

    [RabbitMQ.RabbitPub("Account", "account")]
    [MongoStorage("Test", "Account")]//事件存储
    public sealed class Account : MongoESGrain<String, AccountState, IGrains.MessageInfo>, IAccount
    {
    ……
    }
    

    shardingDays分表时间间隔有个起始点,开始时间在MongoConfig中定义。具体使用参见Example.Ray.HostStartSilo()方法。


  • 相关阅读:
    区域赛
    kd树的创建和求最近邻
    Fliptile POJ
    第六周作业
    第四次作业
    第三次作业
    第二周作业
    第一次作业
    董雅洁 我的第0次作业
    21 UI_布局 之 线性布局 xml配置方式
  • 原文地址:https://www.cnblogs.com/CharlesZHENG/p/8438051.html
Copyright © 2020-2023  润新知