2016-12-18 32 views
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,但我已經看到了這是如何工作,或者你甚至可以做跨服務沒有例子?

這背後的想法是將處理分割成單獨的隊列,以便在我們嘗試重新嘗試消息時不會遇到不一致的狀態。

回答

2

沒有辦法在服務之間共享狀態。 statemanager作用於服務分區級別。

您可以爲此使用外部隊列,例如Service Bus。

您也可以通過使用Event Driven方法來反轉控制。服務1將引發事件,服務2將用作觸發器以繼續處理。要處理的數據可能位於事件內部,或者存儲在其他位置的數據(引用該事件)。

+0

是的,它看起來像這樣,與基於命令的方法相比,很像pub/sub方式。謝謝澄清。 –

相關問題