2013-03-03 36 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