0

是否可以在 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,但我没有看到有关其工作原理的示例,或者您是否甚至可以进行跨服务?

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

4

1 回答 1

2

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

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

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

于 2016-12-20T09:11:03.987 回答