0
是否可以在兩個或多個有狀態服務之間共享隊列,還是需要通過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,但我已經看到了這是如何工作,或者你甚至可以做跨服務沒有例子?
這背後的想法是將處理分割成單獨的隊列,以便在我們嘗試重新嘗試消息時不會遇到不一致的狀態。
是的,它看起來像這樣,與基於命令的方法相比,很像pub/sub方式。謝謝澄清。 –