与Service Fabric中的两个或多个有状态服务共享队列 [英] Share queue with two or more stateful services within Service Fabric

查看:139
本文介绍了与Service Fabric中的两个或多个有状态服务共享队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to share a queue between 2 or more stateful services, or do I need to directly call it via tcp/http to put a message on its own internal queue?

例如;说我有第一个服务,该服务根据条件将订单放入队列:

For example; say I have my first service that puts an order on a queue based on a condition:

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();
            }
        }
    }
}

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

Then my second service reads from that queue and then continues the processing.

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,但是我没有看到任何有关如何工作或什至可以进行跨服务的示例?

I can't see much within the documentation on this, I can see that GetOrAddAsync method can take in a uri but I've seen no examples on how this works or if you can even do cross services?

其背后的想法是将处理分成单独的队列,以便在尝试重试消息时不会出现不一致的状态.

The idea behind this is to split up the processing on to separate queues so that we don't get in a inconsistent state when we try to re-try a message.

推荐答案

无法跨服务共享状态. 状态管理器在服务分区级别上起作用.

There's no way to share state across services. The statemanager acts on a service partition level.

您可以为此目的使用一个外部队列,例如Service Bus.

You could use an external queue for this purpose, like Service Bus.

您还可以使用事件驱动方法来反转控制.服务1将引发一个事件,服务2将使用该事件作为触发来继续进行处理.要处理的数据可以在事件内部,也可以存储在事件引用的其他位置.

You could also invert control, by using an Event Driven approach. Service 1 would raise an event, that Service 2 would use as a trigger to continue processing. The data to process could be inside the event, or data stored in another location, referenced to from the event.

这篇关于与Service Fabric中的两个或多个有状态服务共享队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆