2013-03-03 33 views
0

我是個有點麻煩搞清楚如何做到與經銷商正確以下內容:NServiceBus經銷商只需2個步驟?有

  1. 創建它發送的命令,被工人分佈服務(分銷商)。如果我使用IWantToRunAtStartup實現啓動分銷商,則可以實現此行爲。見下文。
  2. 創建一個處理這些命令的服務(worker)。然後,這個工作人員將啓動X個擴展實例。
  3. 到目前爲止,這些都在同一臺機器上。

包含在NSB中的樣本有點難以理解,或者只是我:)。

例子,我有一個經銷商和工人:

經銷商:

class MessageCreator: IWantToRunAtStartup 
{ 
    public IBus Bus { get; set; } 

    public void Run() 
    { 
     Thread.Sleep(5000); //Allow workers to checkin 
     for (int i = 0; i < 1000; i++) 
     { 
      Bus.Send(new DoWorkForCustomerCommand { CustomerID = i }); 
     } 

    } 

    public void Stop() { } 
} 

...

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server 
{ 
    public void Init() 
    { 
     Configure.Instance.RunDistributor(); 
    } 
} 

的app.config

<configSections> 
    <section name="Logging" type="NServiceBus.Config.Logging, NServiceBus.Core" /> 
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, NServiceBus.Core" /> 
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" /> 
</configSections> 

<MessageForwardingInCaseOfFaultConfig ErrorQueue="error"/> 
<Logging Threshold="INFO" /> 
<UnicastBusConfig> 
    <MessageEndpointMappings> 
     <add Messages="Messages" Endpoint="Worker" /> 
    </MessageEndpointMappings> 
</UnicastBusConfig> 

工人:

public class MessageHandler: IHandleMessages<DoWorkForCustomerCommand > 
{ 
    public void Handle(DoWorkForCustomerCommand message) 
    { 
     Console.WriteLine("Handled customer with Id: " + message.CustomerID); 
    } 
} 

...

public class EndpointConfig : IConfigureThisEndpoint, AsA_Publisher 
{ 
    public void Init() 
    { 
     Configure.Instance.EnlistWithDistributor(); 
     // For some reason this: Configure.Instance.RunDistributor(); achieves the same thing. 
    } 
} 

的app.config

<configSections> 
    <section name="MessageForwardingInCaseOfFaultConfig" type="NServiceBus.Config.MessageForwardingInCaseOfFaultConfig, NServiceBus.Core" /> 
</configSections> 
<MessageForwardingInCaseOfFaultConfig ErrorQueue="error" /> 

此工程在我的機器上,並很好地分配到任意數量的工人,我開始,但我我不想錯過某些東西,ScaleOut示例似乎更復雜?

爲什麼我可以啓動員工作爲分銷商,然後看到工人的行爲,就好像它是一個工人時,實際上是作爲分銷商開始的?

如果我只是在worker app.config中添加一個隊列名稱/端點,這是否會在機器上運行?

回答

1

默認情況下,如果您使用RunDistributor(),則NSB將在同一進程中運行帶有工作節點的分配器。這就是爲什麼儘管有RunDistributor()配置,您仍然可以看到Worker。要禁用此功能,請改用RunDistributorWithNoWorkerOnItsEndpoint()。所有這些都可以通過更改配置在多臺機器上運行。

我可能會建議使用配置文件,因爲這會簡化配置。您可以使用NServiceBus.Distributor和NServicerBus.Worker配置文件。如果你沒有配置正確的配置,配置文件會給你更多的診斷信息。希望這可以幫助。

+0

謝謝你的答案。我實際上已經意識到,自從我發佈了一個問題,我必須在自己的進程中託管NSB,所以已經改變了很多領域。防爆。我不能再使用IWantToRunAtStartup,但必須使用完全不同的解決方案,請參閱我的其他帖子http://stackoverflow.com/questions/15255880/nservicebus-distributor-how-to-split-application。我仍然發現ScaleOut示例非常錯誤,因爲有更多的方式來實現分銷商。我認爲他們可以更多地展示分銷商如何實施:)。 – 2013-03-08 21:42:50