是否可以在 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,但我没有看到有关其工作原理的示例,或者您是否甚至可以进行跨服务?
这背后的想法是将处理拆分到单独的队列中,这样当我们尝试重试消息时就不会处于不一致的状态。