【问题标题】:Share queue with two or more stateful services within Service Fabric与 Service Fabric 中的两个或多个有状态服务共享队列
【发布时间】:2017-05-03 14:53:49
【问题描述】:

是否可以在 2 个或多个有状态服务之间共享一个队列,或者我需要通过 tcp/http 直接调用它以将消息放入自己的内部队列中?

例如;假设我有我的第一个服务,它根据条件将订单放入队列:

public sealed class Service1 : StatefulService
{
    public Service1(StatefulServiceContext context, IReliableStateManagerReplica reliableStateManagerReplica)
        : base(context, reliableStateManagerReplica)
    { }

    protected override async Task RunAsync(CancellationToken cancellationToken)
    {
        var customerQueue = await this.StateManager.GetOrAddAsync<IReliableQueue<Order>>("orders");

        while (true)
        {
            cancellationToken.ThrowIfCancellationRequested();

            using (var tx = this.StateManager.CreateTransaction())
            {
                if (true /* some logic here */)
                {
                    await customerQueue.EnqueueAsync(tx, new Order());
                }

                await tx.CommitAsync();
            }
        }
    }
}

然后我的第二个服务从该队列中读取,然后继续处理。

public sealed class Service2 : StatefulService
{
    public Service2(StatefulServiceContext context, IReliableStateManagerReplica reliableStateManagerReplica)
        : base(context, reliableStateManagerReplica)
    { }

    protected override async Task RunAsync(CancellationToken cancellationToken)
    {
        var customerQueue = await this.StateManager.GetOrAddAsync<IReliableQueue<Order>>("orders");

        while (true)
        {
            cancellationToken.ThrowIfCancellationRequested();

            using (var tx = this.StateManager.CreateTransaction())
            {
                var value = await customerQueue.TryDequeueAsync(tx);
                if (value.HasValue)
                {
                    // Continue processing the order.
                }

                await tx.CommitAsync();
            }
        }
    }
}

我在文档中看不到太多关于此的内容,我可以看到 GetOrAddAsync 方法可以接受 uri,但我没有看到有关其工作原理的示例,或者您是否甚至可以进行跨服务?

这背后的想法是将处理拆分到单独的队列中,这样当我们尝试重试消息时就不会陷入不一致的状态。

【问题讨论】:

    标签: c# azure-service-fabric service-fabric-stateful


    【解决方案1】:

    无法跨服务共享状态。 statemanager 作用于服务分区级别。

    您可以为此目的使用外部队列,例如服务总线。

    您还可以使用Event Driven 方法反转控制。服务 1 将引发一个事件,服务 2 将使用该事件作为触发器继续处理。要处理的数据可能在事件内部,或者存储在另一个位置的数据,从事件中引用。

    【讨论】:

    • 是的,看起来就是这样,与基于命令的方法相比,非常像 pub/sub 方式。感谢您的澄清。
    猜你喜欢
    • 2017-07-25
    • 1970-01-01
    • 2017-10-08
    • 2017-06-22
    • 2017-11-11
    • 2017-01-31
    • 2019-05-25
    • 2016-08-03
    • 1970-01-01
    相关资源
    最近更新 更多