我目前有一个由多个服务组成的 Service Fabric 应用程序。我想要实现的是一种排队机制,因此一个服务可以将消息发布到队列,而另一个服务可以从同一个队列接收消息。
以下不起作用(对于侦听器服务,没有什么可出队的):
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);
}
}
看起来队列的范围仅限于单个 Service。这似乎不是文档中指定的限制。
所以我的问题是:
- 这实际上是一些未记录的限制吗?
- 还是上面的代码有问题?
- 我怎样才能实现上述场景(一个服务将消息添加到队列中,另一个服务从同一个队列中检索消息)?
显然我可以使用 Azure 服务总线,但由于以下几个原因我不能:
- 在我实际的真实场景中,我将有几个队列(可变数量),因此需要按需创建服务总线队列(这并不是一个快速操作)
- 将依赖项添加到另一个 Azure 服务(因此增加了整个系统的故障概率)
- 花费更多
- 更复杂的部署
- 等等