Azure Service Fabric 服务间通信

Azure Service Fabric inter service communication

我目前有一个由多个服务组成的 Service Fabric 应用程序。我想要实现的是一种排队机制 因此一个服务可以将消息发布到队列,而另一个服务可以从同一队列接收消息.

下面的不行(对于Listener服务,没有什么可以dequeue的):

PublisherService:

protected override async Task RunAsync(CancellationToken cancellationToken)
{
    var myQueue = await StateManager.GetOrAddAsync<IReliableQueue<string>>("fooQueue");
    while (true)
    {
        cancellationToken.ThrowIfCancellationRequested();
        using (var tx = this.StateManager.CreateTransaction())
        {
            // Put some message in the queue
            await myQueue.EnqueueAsync(tx, "Foobar");

            await tx.CommitAsync();
        }

        await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
    }
}

ListenerService:

protected override async Task RunAsync(CancellationToken cancellationToken)
{
    var myQueue = await StateManager.GetOrAddAsync<IReliableQueue<string>>("fooQueue");
    while (true)
    {
        cancellationToken.ThrowIfCancellationRequested();
        using (var tx = this.StateManager.CreateTransaction())
        {
            var result = await myQueue.TryDequeueAsync(tx);

            if (result.HasValue)
            {
                ServiceEventSource.Current.ServiceMessage(this.Context, "New message receieved: {0}", result.Value.ToString());
            }

            await tx.CommitAsync();
        }

        await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
    }
}

看起来队列的范围仅限于单个服务。这似乎不是文档中指定的限制。

所以我的问题是:

显然我可以使用 Azure 服务总线,但我不能,原因如下:

ReliableQueues 是服务本地的,因为它的目的是存储该特定服务的状态。该状态被复制到其他实例。它就像 .Net 中的普通 System.Collections.Generic.Queue<T>

对于低成本的解决方案,也许您可​​以使用 Azure Storage Queues。是的,它增加了依赖性,但它具有高可用性。这是一种权衡,只有您才能决定接受或不接受。

另一方面,跳出框框思考:

创建具有多个 ReliableQueues 的有状态服务并公开其他服务可以使用独立远程通信调用的方法,例如:

class QueuingService
{
    void AddToQueue<T>(string queuename, T input) { .. }
    void DeQueue(string queuename) { .. }
}

这当然会产生依赖性,但它具有 Service Fabric 提供的所有安全机制,而且不会花费太多。但是话又说回来,你正在自己构建一个穷人服务 bus/azure 存储队列。

关于文档,不,它并没有用很多话说明可靠队列与 1 项服务相关联,但这取决于您如何解释 this

Service Fabric offers a stateful programming model available to .NET developers via Reliable Collections. Specifically, Service Fabric provides reliable dictionary and reliable queue classes. When you use these classes, your state (my interpretation: The state of the service) is partitioned (for scalability), replicated (for availability), and transacted within a partition (for ACID semantics).

查看为此目的创建的 Priority Queue Service

如果您将故障处理重试模式添加到所有调用代码中,则调用之间不需要队列,请参阅 https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication

link 的相关部分在这里:

An exception handler is responsible for determining what action to take when an exception occurs. Exceptions are categorized into retryable and non retryable. Non retryable exceptions simply get rethrown back to the caller. retryable exceptions are further categorized into transient and non-transient. Transient exceptions are those that can simply be retried without re-resolving the service endpoint address. These will include transient network problems or service error responses other than those that indicate the service endpoint address does not exist. Non-transient exceptions are those that require the service endpoint address to be re-resolved. These include exceptions that indicate the service endpoint could not be reached, indicating the service has moved to a different node.