zoukankan      html  css  js  c++  java
  • 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()方法。


  • 相关阅读:
    基本操作——word中怎样同一页中放入多张图片
    计算机概念入门(二)
    计算机概念入门(一)
    关于黑苹果系统的耳机声音模糊不清问题
    开源项目
    尚硅谷官网资料导航
    this
    最棒的 JavaScript 学习指南(2018版)(转)
    前端文摘:深入解析浏览器的幕后工作原理(转)
    闭包,模块化
  • 原文地址:https://www.cnblogs.com/CharlesZHENG/p/8438051.html
Copyright © 2011-2022 走看看